1. 是否允许为空,默认NULL,可设置NOT NULL,字段不允许为空,必须赋值
2. 字段是否有默认值,缺省的默认值是NULL,如果插入记录时不给字段赋值,此字段使用默认值
sex enum(‘male‘,‘female‘) not null default ‘male‘
age int unsigned NOT NULL default 20 必须为正值(无符号) 不允许为空 默认是20
3. 是否是key
主键 primary key
外键 foreign key
索引 (index,unique...)
二 not null与default
是否可空,null表示空,非字符串 not null - 不可空 null - 可空
默认值,创建列时可以指定默认值,当插入数据时如果未主动设置,则自动添加默认值
create table tb1(
nid int not null defalut 2,
num int not null
);
先说一点:在我们插入数据的时候,可以这么写insert into tb1(nid,num) values(1,‘chao’);就是在插入输入的时候,指定字段插入数据,如果我在只给num插入值,可以这样写insert into tb1(num) values(‘chao‘);还可以插入数据的时候,指定插入数据字段的顺序:把nid和num换个位置,但是对应插入的值也要换位置。注意:即便是你只给一个字段传值了,那么也是生成一整条记录,这条记录的其他字段的值如果可以为空,那么他们就都是null空值,如果不能为空,就会报错。
==================not null====================mysql> create table t1(id int); #id字段默认可以插入空
mysql> desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
mysql> insert into t1 values(); #可以插入空
mysql> create table t2(id int not null); #设置字段id不为空
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
mysql> insert into t2 values(); #不能插入空
ERROR 1364 (HY000): Field ‘id‘ doesn‘t have a default value
==================default====================
#设置id字段有默认值后,则无论id字段是null还是not null,都可以插入空,插入空默认填入default指定的默认值
mysql> create table t3(id int default 1);
mysql> alter table t3 modify id int not null default 1;
==================综合练习====================
mysql> create table student(
-> name varchar(20) not null,
-> age int(3) unsigned not null default 18,
-> sex enum(‘male‘,‘female‘) default ‘male‘,
-> hobby set(‘play‘,‘study‘,‘read‘,‘music‘) default ‘play,music‘
-> );
mysql> desc student;
+-------+------------------------------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------------------------+------+-----+------------+-------+
| name | varchar(20) | NO | | NULL | |
| age | int(3) unsigned | NO | | 18 | |
| sex | enum(‘male‘,‘female‘) | YES | | male | |
| hobby | set(‘play‘,‘study‘,‘read‘,‘music‘) | YES | | play,music | |
+-------+------------------------------------+------+-----+------------+-------+
mysql> insert into student(name) values(‘chao‘);
mysql> select * from student;
+------+-----+------+------------+
| name | age | sex | hobby |
+------+-----+------+------------+
| chao| 18 | male | play,music |
+------+-----+------+------------+
not null和default测试
1#不指定id,则自动增长 2create table student(
3 id int primary key auto_increment,
4 name varchar(20),
5 sex enum(‘male‘,‘female‘) default ‘male‘ 6);
7 8 mysql> desc student;
9 +-------+-----------------------+------+-----+---------+----------------+
10 | Field | Type | Null | Key | Default | Extra |
11 +-------+-----------------------+------+-----+---------+----------------+
12 | id | int(11) | NO | PRI | NULL | auto_increment |
13 | name | varchar(20) | YES | | NULL | |
14 | sex | enum(‘male‘,‘female‘) | YES | | male | |
15 +-------+-----------------------+------+-----+---------+----------------+
16 mysql> insert into student(name) values17 -> (‘egon‘),
18 -> (‘alex‘)
19 -> ;
2021 mysql> select * from student;
22 +----+------+------+
23 | id | name | sex |
24 +----+------+------+
25 | 1 | egon | male |
26 | 2 | alex | male |
27 +----+------+------+
282930#也可以指定id31 mysql> insert into student values(4,‘asb‘,‘female‘);
32 Query OK, 1 row affected (0.00 sec)
3334 mysql> insert into student values(7,‘wsb‘,‘female‘);
35 Query OK, 1 row affected (0.00 sec)
3637 mysql> select * from student;
38 +----+------+--------+
39 | id | name | sex |
40 +----+------+--------+
41 | 1 | egon | male |
42 | 2 | alex | male |
43 | 4 | asb | female |
44 | 7 | wsb | female |
45 +----+------+--------+
464748#对于自增的字段,在用delete删除后,再插入值,该字段仍按照删除前的位置继续增长49 mysql> delete from student;
50 Query OK, 4 rows affected (0.00 sec)
5152 mysql> select * from student;
53 Empty set (0.00 sec)
5455 mysql> insert into student(name) values(‘ysb‘);
56 mysql> select * from student;
57 +----+------+------+
58 | id | name | sex |
59 +----+------+------+
60 | 8 | ysb | male |
61 +----+------+------+
6263#应该用truncate清空表,比起delete一条一条地删除记录,truncate是直接清空表,在删除大表时用它64 mysql> truncate student;
65 Query OK, 0 rows affected (0.01 sec)
6667 mysql> insert into student(name) values(‘egon‘);
68 Query OK, 1 row affected (0.01 sec)
6970 mysql> select * from student;
71 +----+------+------+
72 | id | name | sex |
73 +----+------+------+
74 | 1 | egon | male |
75 +----+------+------+
76 row in set (0.00 sec)
7778 auto_increment测试
auto_increment测试
#在创建完表后,修改自增字段的起始值
mysql> create table student(
-> id int primary key auto_increment,
-> name varchar(20),
-> sex enum(‘male‘,‘female‘) default ‘male‘
-> );
mysql> alter table student auto_increment=3;
mysql> show create table student;
.......
ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
mysql> insert into student(name) values(‘egon‘);
Query OK, 1 row affected (0.01 sec)
mysql> select * from student;
+----+------+------+
| id | name | sex |
+----+------+------+
| 3 | egon | male |
+----+------+------+
row in set (0.00 sec)
mysql> show create table student;
.......
ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
#也可以创建表时指定auto_increment的初始值,注意初始值的设置为表选项,应该放到括号外create table student(
id int primary key auto_increment,
name varchar(20),
sex enum(‘male‘,‘female‘) default ‘male‘
)auto_increment=3;
#设置步长sqlserver:自增步长
基于表级别
create table t1(
id int。。。
)engine=innodb,auto_increment=2 步长=2 default charset=utf8
mysql自增的步长:
show session variables like ‘auto_inc%‘;
#基于会话级别
set session auth_increment_increment=2#修改会话级别的步长
#基于全局级别的
set global auth_increment_increment=2#修改全局级别的步长(所有会话都生效)
#!!!注意了注意了注意了!!!If the value of auto_increment_offset is greater than that of auto_increment_increment, the value of auto_increment_offset is ignored.
翻译:如果auto_increment_offset的值大于auto_increment_increment的值,则auto_increment_offset的值会被忽略 ,这相当于第一步步子就迈大了,扯着了蛋
比如:设置auto_increment_offset=3,auto_increment_increment=2
mysql> set global auto_increment_increment=5;
Query OK, 0 rows affected (0.00 sec)
mysql> set global auto_increment_offset=3;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like ‘auto_incre%‘; #需要退出重新登录
+--------------------------+-------+
| Variable_name | Value |
+--------------------------+-------+
| auto_increment_increment | 1 |
| auto_increment_offset | 1 |
+--------------------------+-------+
create table student(
id int primary key auto_increment,
name varchar(20),
sex enum(‘male‘,‘female‘) default ‘male‘
);
mysql> insert into student(name) values(‘egon1‘),(‘egon2‘),(‘egon3‘);
mysql> select * from student;
+----+-------+------+
| id | name | sex |
+----+-------+------+
| 3 | egon1 | male |
| 8 | egon2 | male |
| 13 | egon3 | male |
+----+-------+------+
了解内容:步长:auto_increment_increment 起始偏移量:auto_increment_offset
2.重建表,我们现在要解决的问题是:我们要达到一个在做某个表(被关联表)更新或者删除操作的时候,关联表的数据同步的进行更新和删除的效果,所以我们在建表的时候,可以加上两个功能:同步更新和同步删除:看看如何实现:在建立关联关系的时候,加上这两句: on delete cascade和 on update cascade
然后把我们之间的表和数据都插入进去:然后再进行更新删除操作:
然后我们再直接删除部门表里面的数据的时候,你看看结果:
成功了,并且员工表里面关联部门表id的数据也都删除了,是不是达到了我们刚才想要实现的效果呀
下面我们来看一下更新操作,我们之前说更新一个部门的id号,注意一个问题昂,我更新部门的名称,你说有影响吗?肯定没有啊,因为我员工表并不是关联的部门的名称字段,而是关联的部门的id字段,你改部门名称没关系,我通过你的id照样找到你,但是你如果改了id号,那么我员工表里面的id号和你不匹配了,我就没法找到你,所有当你直接更新部门的id的时候,我就给你报错了,大哥,你想改的是关联字段啊,考虑一下关联表的数据们的感受行不行。我们来看一下加上 on update cascade之后的效果:
=====================多对一=====================
create table press(
id int primary key auto_increment,
name varchar(20)
);
create table book(
id int primary key auto_increment,
name varchar(20),
press_id int not 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
(‘九阳神功‘,1),
(‘九阴真经‘,2),
(‘九阴白骨爪‘,2),
(‘独孤九剑‘,3),
(‘降龙十巴掌‘,2),
(‘葵花宝典‘,3)
#一定是student来foreign key表customer,这样就保证了:
#1 学生一定是一个客户,
#2 客户不一定是学生,但有可能成为一个学生
create table customer(
id int primary key auto_increment,
name varchar(20) not null,
qq varchar(10) not null,
phone char(16) not null
);
create table student(
id int primary key auto_increment,
class_name varchar(20) not null,
customer_id int unique, #该字段一定要是唯一的
foreign key(customer_id) references customer(id) #外键的字段一定要保证unique
on delete cascade
on update cascade
);
#增加客户
insert into customer(name,qq,phone) values
(‘李飞机‘,‘31811231‘,13811341220),
(‘王大炮‘,‘123123123‘,15213146809),
(‘守榴弹‘,‘283818181‘,1867141331),
(‘吴坦克‘,‘283818181‘,1851143312),
(‘赢火箭‘,‘888818181‘,1861243314),
(‘战地雷‘,‘112312312‘,18811431230)
;
#增加学生
insert into student(class_name,customer_id) values
(‘脱产3班‘,3),
(‘周末19期‘,4),
(‘周末19期‘,5)
;
一对一简单示例
例一:一个用户只有一个博客
用户表:
id name
1 egon
2 alex
3 wupeiqi
博客表
fk+unique
id url name_id
1 xxxx 1
2 yyyy 3
3 zzz 2
例二:一个管理员唯一对应一个用户
用户表:
id user password
1 egon xxxx
2 alex yyyy
管理员表:
fk+unique
id user_id password
1 1 xxxxx
2 2 yyyyy