<div ID="cnblogs_post_body">
<div >
约束条件与数据类型的宽度一样,都是可选参数
作用:用于保证数据的完整性和一致性
主要分为:
UNSIGNED 无符号
ZEROFILL 使用0填充
<div ><span ><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);">
<div ><span ><a title="复制代码" onclick="copyCnblogsCode(this)" href="javascript:void(0);">
说明:
12,) null default 203
二 not null与default
是否可空,null表示空,非字符串
not null - 不可空
null - 可空
默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nID int not null defalut 2,
num int not null
)
MysqL><span > desc t2;
+-------+---------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| ID | int(11) | NO | | NulL | |
+-------+---------+------+-----+---------+-------+<span >
MysqL> insert into t2 values(); <span >#<span >不能插入空
ERROR 1364 (HY000): FIEld <span >'<span >ID<span >' doesn<span >'<span >t have a default value
==================default====================
<span >#<span >设置ID字段有默认值后,则无论ID字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
MysqL> create table t3(ID int default 1<span >);
MysqL> alter table t3 modify ID int <span >not null default 1<span >;
==================综合练习====================<span >
MysqL><span > create table student(
-> name varchar(20) <span >not<span > null,-> age int(3) unsigned <span >not null default 18<span >,-> sex enum(<span >'<span >male<span >',<span >'<span >female<span >') default <span >'<span >male<span >'<span >,-> hobby set(<span >'<span >play<span >',<span >'<span >study<span >',<span >'<span >read<span >',<span >'<span >music<span >') default <span >'<span >play,music<span >'
-><span > );
MysqL><span > desc student;
+-------+------------------------------------+------+-----+------------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+-------+------------------------------------+------+-----+------------+-------+
| name | varchar(20) | NO | | NulL | |
| age | int(3) unsigned | NO | | 18 | |
| sex | enum(<span >'<span >male<span >',<span >'<span >female<span >') | YES | | male | |
| hobby | set(<span >'<span >play<span >',<span >'<span >music<span >') | YES | | play,music | |
+-------+------------------------------------+------+-----+------------+-------+<span >
MysqL> insert into student(name) values(<span >'<span >egon<span >'<span >);
MysqL> select * <span >from<span > student;
+------+-----+------+------------+
| name | age | sex | hobby |
+------+-----+------+------------+
| egon | 18 | male | play,music |
+------+-----+------+------------+
create table department2(
ID int,name varchar(20<span >),comment varchar(100<span >),constraint uk_name unique(name)
);
MysqL
> insert into department1 values(1,<span >'<span >IT<span >',<span >'<span >技术<span >'<span >);query OK,1 row affected (0.00<span > sec)
MysqL> insert into department1 values(1,<span >'<span >技术<span >'<span >);
ERROR 1062 (23000): Duplicate entry <span >'<span >IT<span >' <span >for key <span >'<span >name<span >'MysqL> create table t1(ID int 0.02MysqL><span > desc t1;
+-------+---------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NulL | |
+-------+---------+------+-----+---------+-------+
1 row <span >in set (0.00 sec)四 primary key
primary key字段的值不为空且唯一
一个表中可以:
单列做主键
多列做主键(复合主键)
但一个表内只能有一个主键primary key
============单列做主键=============== null unique,name varchar(20) 100MysqL><span > desc department1;+---------+--------------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NulL | |
| name | varchar(20) | NO | UNI | NulL | |
| comment | varchar(100) | YES | | NulL | |
+---------+--------------+------+-----+---------+-------+<span >
rows <span >in set (0.01<span > sec)<span >#<span >方法二:在某一个字段后用primary key
<span >create table department2(
ID int primary key,<span >#<span >主键
name varchar(20<span >),comment varchar(100<span >)
);
MysqL><span > desc department2;
+---------+--------------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NulL | |
| name | varchar(20) | YES | | NulL | |
| comment | varchar(100) | YES | | NulL | |
+---------+--------------+------+-----+---------+-------+<span >
rows <span >in set (0.00<span > sec)
<span >#<span >方法三:在所有字段后单独定义primary key
<span >create table department3(
ID int,constraint pk_name primary key(ID); <span >#<span >创建主键并为其命名pk_name
<span >
MysqL><span > desc department3;
+---------+--------------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| ID | int(11) | NO | PRI | NulL | |
| name | varchar(20) | YES | | NulL | |
| comment | varchar(100) | YES | | NulL | |
+---------+--------------+------+-----+---------+-------+<span >
rows <span >in set (0.01 sec)
+--------------+-------------+------+-----+---------+-------+
| FIEld | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ip | varchar(15) | NO | PRI | NulL | |
| port | char(5) | NO | PRI | NulL | |
| service_name | varchar(10) | NO | | NulL | |
+--------------+-------------+------+-----+---------+-------+
3 rows <span >in set (0.00<span > sec)
MysqL
><span > insert into service values-> (<span >'<span >172.16.45.10<span >',<span >'<span >3306<span >',<span >'<span >MysqLd<span >'<span >),-> (<span >'<span >172.16.45.11<span >',<span >'<span >mariadb<span >'<span >)
-><span > ;
query OK,2 rows affected (0.00<span > sec)
Records: 2<span > Duplicates: 0 Warnings: 0
MysqL> insert into service values (<span >'<span >172.16.45.10<span >',<span >'<span >Nginx<span >'<span >);
ERROR 1062 (23000): Duplicate entry <span >'<span >172.16.45.10-3306<span >' <span >for key <span >'<span >PRIMARY<span >'
约束字段为自动增长,被约束的字段必须同时被key约束
,) default MysqL><span > desc student;+-------+-----------------------+------+-----+---------+----------------+
| FIEld | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+----------------+
| ID | int(11) | NO | PRI | NulL | auto_increment |
| name | varchar(20) | YES | | NulL | |
| sex | enum(<span >'<span >male<span >',<span >'<span >female<span >') | YES | | male | |
+-------+-----------------------+------+-----+---------+----------------+<span >
MysqL><span > insert into student(name) values
-> (<span >'<span >egon<span >'<span >),-> (<span >'<span >alex<span >'<span >)
-><span > ;
MysqL> select * <span >from<span > student;
+----+------+------+
| ID | name | sex |
+----+------+------+
| 1 | egon | male |
| 2 | alex | male |
+----+------+------+
<span >#<span >也可以指定ID
MysqL> insert into student values(4,<span >'<span >asb<span >',<span >'<span >female<span >'<span >);
query OK,1 row affected (0.00<span > sec)
MysqL> insert into student values(7,<span >'<span >wsb<span >',1 row affected (0.00<span > sec)
MysqL> select * <span >from<span > student;
+----+------+--------+
| ID | name | sex |
+----+------+--------+
| 1 | egon | male |
| 2 | alex | male |
| 4 | asb | female |
| 7 | wsb | female |
+----+------+--------+
<span >#<span >对于自增的字段,在用delete删除后,再插入值,该字段仍按照删除前的位置继续增长
MysqL> delete <span >from<span > student;
query OK,4 rows affected (0.00<span > sec)
MysqL> select * <span >from<span > student;
Empty set (0.00<span > sec)
MysqL> insert into student(name) values(<span >'<span >ysb<span >'<span >);
MysqL> select * <span >from<span > student;
+----+------+------+
| ID | name | sex |
+----+------+------+
| 8 | ysb | male |
+----+------+------+
<span >#<span >应该用truncate清空表,比起delete一条一条地删除记录,truncate是直接清空表,在删除大表时用它
MysqL><span > truncate student;
query OK,0 rows affected (0.01<span > sec)
MysqL> insert into student(name) values(<span >'<span >egon<span >'<span >);
query OK,1 row affected (0.01<span > sec)
MysqL> select * <span >from<span > student;
+----+------+------+
| ID | name | sex |
+----+------+------+
| 1 | egon | male |
+----+------+------+
1 row <span >in set (0.00 sec)
员工信息表有三个字段:工号 姓名 部门
公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费
解决方法:
我们完全可以定义一个部门表
然后让员工信息表关联该表,如何关联,即foreign key
@H_665_419@20) =<span >#<span >dpt_ID外键,关联父表(department主键ID),同步更新,同步删除<span >create table employee(
ID int primary key,name varchar(20) <span >not<span > null,dpt_ID int,constraint fk_name foreign key(dpt_ID)
references department(ID)
on delete cascade
on update cascade
)engine=<span >innodb;<span >#<span >先往父表department中插入记录
<span >insert into department values
(1,<span >'<span >欧德博爱技术有限事业部<span >'<span >),(2,<span >'<span >艾利克斯人力资源部<span >'<span >),(3,<span >'<span >销售部<span >'<span >);
<span >#<span >再往子表employee中插入记录
<span >insert into employee values
(1,<span >'<span >egon<span >',1<span >),<span >'<span >alex1<span >',2<span >),<span >'<span >alex2<span >',(4,<span >'<span >alex3<span >',(5,<span >'<span >李坦克<span >',3<span >),(6,<span >'<span >刘飞机<span >',(7,<span >'<span >张火箭<span >',(8,<span >'<span >林子d<span >',(9,<span >'<span >加特林<span >',3<span >)
;
<span >#<span >删父表department,子表employee中对应的记录跟着删
MysqL> delete <span >from department where ID=3<span >;
MysqL> select * <span >from<span > employee;
+----+-------+--------+
| ID | name | dpt_ID |
+----+-------+--------+
| 1 | egon | 1 |
| 2 | alex1 | 2 |
| 3 | alex2 | 2 |
| 4 | alex3 | 2 |
+----+-------+--------+
<span >#<span >更新父表department,子表employee中对应的记录跟着改
MysqL> update department set ID=22222 where ID=2<span >;
MysqL> select * <span >from<span > employee;
+----+-------+--------+
| ID | name | dpt_ID |
+----+-------+--------+
| 1 | egon | 1 |
| 3 | alex2 | 22222 |
| 4 | alex3 | 22222 |
| 5 | alex1 | 22222 |
+----+-------+--------+
多对多:
表1的多条记录可以对应表2的一条记录
表2的多条记录也可以对应表1的一条记录
一对一:
表1的一条记录唯一对应表2的一条记录,反之亦然
分析时,我们先从按照上面的基本原理去套,然后再翻译成真实的意义,就很好理解了
三张表:出版社,作者信息,书
一对多(或多对一):一个出版社可以出版多本书
关联方式:foreign key
=====================多对一=====================20create table book(ID int primary key autoincrement,pressID int <span >not<span > null,foreign key(press_ID) references press(ID)
on delete cascade
on update cascade
);
insert into press(name) values
(
;
insert into book(name,press_ID) values
(<span >'<span >九阳神功<span >',(<span >'<span >九阴真经<span >',(<span >'<span >九阴白骨爪<span >',(<span >'<span >独孤九剑<span >',(<span >'<span >降龙十巴掌<span >',(<span >'<span >葵花宝典<span >',3<span >)
;
多对多:一个作者可以写多本书,一本书也可以有多个作者,双向的一对多,即多对多
关联方式:foreign key+一张新的表
=====================多对多=====================20<span >#<span >这张表就存放作者表与书表的关系,即查询二者的关系查这表就可以了<span >create table author2book(
ID int <span >not<span > null unique autoincrement,authorID int <span >not<span > null,book_ID int <span >not<span > null,constraint fkauthor foreign key(authorID) references author(ID)
on delete cascade
on update cascade,constraint fkbook foreign key(bookID) references book(ID)
on delete cascade
on update cascade,primary key(author_ID,book_ID)
);
<span >#<span >插入四个作者,ID依次排开
insert into author(name) values(<span >'<span >egon<span >'),(<span >'<span >alex<span >'),(<span >'<span >yuanhao<span >'),(<span >'<span >wpq<span >'<span >);
<span >#<span >每个作者与自己的代表作如下
1<span > egon:
1<span > 九阳神功
2<span > 九阴真经
3<span > 九阴白骨爪
4<span > 独孤九剑
5<span > 降龙十巴掌
6<span > 葵花宝典
2<span > alex:
1<span > 九阳神功
6<span > 葵花宝典
3<span > yuanhao:
4<span > 独孤九剑
5<span > 降龙十巴掌
6<span > 葵花宝典
4<span > wpq:
1<span > 九阳神功
insert into author2book(author_ID,book_ID) values
(1,(1,4<span >),5<span >),6<span >),1<span >)
;
#两张表:学生表和客户表
一对一:一个学生是一个客户,一个客户有可能变成一个学校,即一对一的关系
关联方式:foreign key+unique
create table customer(ID int primary key auto_increment,name varchar(20) <span >not<span > null
);
create table student(
ID int primary key autoincrement,classname varchar(
foreign key(customer_ID) references customer(ID) <span >#<span >外键的字段一定要保证unique
<span >on delete cascade
on update cascade
);
<span >#<span >增加客户
<span >insert into customer(name) values
(<span >'<span >李飞机<span >'<span >),(<span >'<span >王大炮<span >'<span >),(<span >'<span >守榴d<span >'<span >),(<span >'<span >吴坦克<span >'<span >),(<span >'<span >赢火箭<span >'<span >),(<span >'<span >战地雷<span >'<span >)
;
<span >#<span >增加学生
<span >insert into student(name,customer_ID) values
(<span >'<span >李飞机<span >',(<span >'<span >王大炮<span >',2<span >)
;
练习:账号信息表,用户组,主机表,主机组
20) 50) insert into user(username,password) values(<span >'<span >root<span >',<span >'<span >123<span >'<span >),(<span >'<span >egon<span >',<span >'<span >456<span >'<span >),(<span >'<span >alex<span >',<span >'<span >alex3714<span >'<span >)
;
<span >#<span >用户组表
<span >create table usergroup(
ID int primary key auto_increment,groupname varchar(20) <span >not<span > null unique
);
insert into usergroup(groupname) values
(<span >'<span >IT<span >'<span >),(<span >'<span >Sale<span >'<span >),(<span >'<span >Finance<span >'<span >),(<span >'<span >boss<span >'<span >)
;
<span >#<span >主机表
<span >create table host(
ID int primary key auto_increment,ip char(15) <span >not null unique default <span >'<span >127.0.0.1<span >'<span >
);
insert into host(ip) values
(<span >'<span >172.16.45.2<span >'<span >),(<span >'<span >172.16.31.10<span >'<span >),(<span >'<span >172.16.45.3<span >'<span >),(<span >'<span >172.16.31.11<span >'<span >),(<span >'<span >172.10.45.3<span >'<span >),(<span >'<span >172.10.45.4<span >'<span >),(<span >'<span >172.10.45.5<span >'<span >),(<span >'<span >192.168.1.20<span >'<span >),(<span >'<span >192.168.1.21<span >'<span >),(<span >'<span >192.168.1.22<span >'<span >),(<span >'<span >192.168.2.23<span >'<span >),(<span >'<span >192.168.2.223<span >'<span >),(<span >'<span >192.168.2.24<span >'<span >),(<span >'<span >192.168.3.22<span >'<span >),(<span >'<span >192.168.3.23<span >'<span >),(<span >'<span >192.168.3.24<span >'<span >)
;
<span >#<span >业务线表
<span >create table business(
ID int primary key auto_increment,business varchar(20) <span >not<span > null unique
);
insert into business(business) values
(<span >'<span >轻松贷<span >'<span >),(<span >'<span >随便花<span >'<span >),(<span >'<span >大富翁<span >'<span >),(<span >'<span >穷一生<span >'<span >)
;
<span >#<span >建关系:user与usergroup
<span >
create table user2usergroup(
ID int <span >not<span > null unique autoincrement,userID int <span >not<span > null,group_ID int <span >not<span > null,primary key(user_ID,group_ID),foreign key(user_ID) references user(ID),foreign key(group_ID) references usergroup(ID)
);
insert into user2usergroup(user_ID,group_ID) values
(1,4<span >)
;
<span >#<span >建关系:host与business
<span >
create table host2business(
ID int <span >not<span > null unique autoincrement,hostID int <span >not<span > null,business_ID int <span >not<span > null,primary key(host_ID,business_ID),foreign key(host_ID) references host(ID),foreign key(business_ID) references business(ID)
);
insert into host2business(host_ID,business_ID) values
(1,4<span >)
;
<span >#<span >建关系:user与host
<span >
create table user2host(
ID int <span >not<span > null unique autoincrement,hostID),foreign key(host_ID) references host(ID)
);
insert into user2host(user_ID,host_ID) values
(1,7<span >),8<span >),9<span >),10<span >),11<span >),12<span >),13<span >),14<span >),15<span >),16<span >),12<span >)
;
以上是内存溢出为你收集整理的约束全部内容,希望文章能够帮你解决约束所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)