mysql建表约束
--mysql建表约束
--主键约束
它能够唯一确定一张表中的内容,也就是我们通过某个字段添加约束,就可以是的该字段唯一(不重复)且不为空。
create table user(
id int primary key,
name varchar(20)
);
--联合主键
只要联合的主键加起来不重复即可
create table user1(
id int,
name varchar(20),
password varchar(20),
primary key(id,name)
);
insert into user1 values(1,'张三','123');
insert into user1 values(2,'李四','123');
insert into user1 values(3,'张三','123');
--自增约束
--auto_increment
create table user2(
id int primary key auto_increment,
name varchar(20)
);
insert into user2 (name)values('张三');
insert into user2 (name)values('李四');
mysql> insert into user2 (name) values('张三');
Query OK, 1 row affected (0.03 sec)
mysql> insert into user2 (name) values('李四');
Query OK, 1 row affected (0.03 sec)
mysql> select * from user2;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 李四 |
+----+--------+
会对id进行一个自动排序
2 rows in set (0.00 sec)
--如果说在创建表时忘记创建主键约束,如何添加主键约束
create table user3(
id int,
name varchar(20)
);
desc user3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--修改表结构
alter table user3 add primary key(id);
desc user3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--删除表结构
alter table user3 drop primary key;
desc user3;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
可以会看到user3中没有了primary key
--使用modify修改字段,添加约束
--外建约束
--涉及到两个表:父表,字表
--班级
create table classes(
id int primary key,
name varchar(20)
);
--学生表
create table student(
id int primary key,
name varchar(20),
class_id int,
foreign key(class_id) references classes(id)
);
mysql> desc classes;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> desc student;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| class_id | int(11) | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
insert into classes values(1,'一班');
insert into classes values(2,'二班');
insert into classes values(3,'三班');
insert into classes values(4,'四班');
insert into student values(1,'张三',1);
insert into student values(2,'李四',2);
insert into student values(3,'王五',3);
insert into student values(4,'赵流',4);
insert into student values(5,'赵流',5);
mysql> insert into student values(5,'赵流',5);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
--主表没有的元素,副表不可以进行引用
--主表中记录被副表引用的元素不能被删除
delete from classes where id = 4;
mysql> delete from classes where id = 4;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`student`, CONSTRAINT `student_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
mysql> select * from classes;
+----+--------+
| id | name |
+----+--------+
| 1 | 一班 |
| 2 | 二班 |
| 3 | 三班 |
| 4 | 四班 |
+----+--------+
4 rows in set (0.00 sec)
mysql> select * from student;
+----+--------+----------+
| id | name | class_id |
+----+--------+----------+
| 1 | 张三 | 1 |
| 2 | 李四 | 2 |
| 3 | 王五 | 3 |
| 4 | 赵流 | 4 |
+----+--------+----------+
4 rows in set (0.00 sec)
mysql>
--唯一约束
--约束修饰的字段的值不能重复
create table user5(
id int,
name varchar(20),
primary key(id,name),:w
unique(id)
);
mysql> desc user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | UNI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
desc user5;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(20) | NO | PRI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
insert into user5 values(1,'张三');
insert into user5 values(2,'李四');
insert into user5 values(3,'王五');
mysql> select * from user5;
+----+--------+
| id | name |/
+----+--------+
| 1 | 张三 |
| 2 | 李四 |
| 3 | 王五 |
+----+--------+
3 rows in set (0.00 sec)
--如何删除唯一约束
alter table user5 drop index name;
--非空约束
--修饰的字段不能为空
create table user6(
id int,
name varchar(20) not null
);
desc user6;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--默认约束
--就是当我们插入字段时,如果没有传值,就会使用默认值
create table user7(
id int,
name varchar(20),
age int Default 10
);
mysql> desc user7;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| age | int(11) | YES | | 10 | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
insert into user7 (id,name)values(1,'张三');
mysql> select * from user7;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | 张三 | 10 |
+------+--------+------+
1 row in set (0.00 sec)
mysql建表约束的更多相关文章
- 基于表的数据字典构造MySQL建表语句
表的数据字典格式如下: 如果手动写MySQL建表语句,确认麻烦,还不能保证书写一定正确. 写了个Perl脚本,可快速构造MySQL脚本语句. 脚本如下: #!/usr/bin/perl use str ...
- 三种常用的MySQL建表语句(转)
MySQL建表语句是最基础的SQL语句之一,下面就为您介绍最常用的三种MySQL建表语句,如果您对MySQL建表语句方面感兴趣的话,不妨一看. 1.最简单的: CREATE TABLE t1( ...
- MySQL 建库、建用户及建表事项
1,MySQL建库语句比较简单,一句话: create database tppamltest3 2,创建用户及授权: insert into mysql.user(Host,User,Passwor ...
- MySQL 建表字段长度的限制
脑补,varchar(N),N指的是最大字符数,不是字节数. 先上测试说明: 在MySQL建表时,遇到一个奇怪的现象: root@localhost : test 10:30:54>CREA ...
- mysql建表出现Timestamp错误
mysql建表时如果有两个或以上的字段为Timestamp,那么可能会出现如下错误: Incorrect table definition; there can be only one TIMESTA ...
- MySql 建表出现的问题:[ERR] 1064 - You have an error in your SQL syntax; check the manual.......
使用 MySql 建表出现的问题 在使用 Navicat Premium 运行 sql 语句进行建表时,MySQL 报错如下: 建表语句: DROP DATABASE IF EXISTS javawe ...
- (转载)用C#实现MySQL建库及建表
最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...
- MySQL 建表语句 create table 中的列定义
MySQL 建表语句 create table 中的列定义: column_definition: data_type [NOT NULL | NULL] [DEFAULT default_value ...
- Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven)
Spring 笔记 -06- 从 MySQL 建库到 登录验证数据库信息(maven) 本篇和 Spring 没有什么关系,只是学习 Spring,必备一些知识,所以放在这里了. 本篇内容: (1)M ...
随机推荐
- CNN可视化技术总结(三)--类可视化
CNN可视化技术总结(一)-特征图可视化 CNN可视化技术总结(二)--卷积核可视化 导言: 前面我们介绍了两种可视化方法,特征图可视化和卷积核可视化,这两种方法在论文中都比较常见,这两种更多的是用于 ...
- 链接脚本再探和VMA与LMA
链接脚本简单描述 连接脚本的描述都是以节(section)的单位的,网上也有很多描述链接脚本语法的好文章,再不济还有官方的说明文档可以用来学习,其实主要就是对编译构建的整个过程有了深入的理解后就能对链 ...
- php 配置主机虚拟目录(使用虚拟域名访问 127.0.0.1) 一点也不好使?????
php 配置主机虚拟目录(使用虚拟域名访问 127.0.0.1)steps:1>打开目录 D:\xwamp\bin\apache\apache2.4.9\conf 修改文件 httpd ...
- how to input special symbol in macOS
how to input special symbol in macOS 如何在 macOS 中输入特殊符号 1024 ≈ 1000 2^10 == 1024 约等于 1000, 方便用来表示 Opt ...
- Code Book All In One
Code Book All In One Jupyter Notebook Jupyter Lab https://jupyter.org/ Storybook https://storybook.j ...
- illustrating javascript prototype & prototype chain
illustrating javascript prototype & prototype chain 图解 js 原型和原型链 proto & prototype func; // ...
- c++ x86_x64挂钩函数 传递寄存器表
https://github.com/januwA/GameCheat #include "pch.h" #include <iostream> #include &l ...
- 【目标检测】用Fast R-CNN训练自己的数据集超详细全过程
目录: 一.环境准备 二.训练步骤 三.测试过程 四.计算mAP 寒假在家下载了Fast R-CNN的源码进行学习,于是使用自己的数据集对这个算法进行实验,下面介绍训练的全过程. 一.环境准备 我这里 ...
- CSS中Position属性static、absolute、fixed、relative
在html中网页可以看成一个立体的空间,一个完整的页面是由很多个页面堆积形成的,如下图所示 CSS中Position属性有四个可选值,它们分别是:static.absolute.fixed.rel ...
- 【Notes_8】现代图形学入门——几何(基本表示方法、曲线与曲面)
几何 几何表示 隐式表示 不给出点的坐标,给数学表达式 优点 可以很容易找到点与几何之间的关系 缺点 找某特定的点很难 更多的隐式表示方法 Constructive Solid Geometry .D ...