时间:2021-07-01 10:21:17 帮助过:20人阅读
1 ============单列做主键=============== 2 #方法一:not null+unique 3 create table department1( 4 id int not null unique, #主键 5 name varchar(20) not null unique, 6 comment varchar(100) 7 ); 8 9 mysql> desc department1; 10 +---------+--------------+------+-----+---------+-------+ 11 | Field | Type | Null | Key | Default | Extra | 12 +---------+--------------+------+-----+---------+-------+ 13 | id | int(11) | NO | PRI | NULL | | 14 | name | varchar(20) | NO | UNI | NULL | | 15 | comment | varchar(100) | YES | | NULL | | 16 +---------+--------------+------+-----+---------+-------+ 17 rows in set (0.01 sec) 18 19 #方法二:在某一个字段后用primary key 20 create table department2( 21 id int primary key, #主键 22 name varchar(20), 23 comment varchar(100) 24 ); 25 26 mysql> desc department2; 27 +---------+--------------+------+-----+---------+-------+ 28 | Field | Type | Null | Key | Default | Extra | 29 +---------+--------------+------+-----+---------+-------+ 30 | id | int(11) | NO | PRI | NULL | | 31 | name | varchar(20) | YES | | NULL | | 32 | comment | varchar(100) | YES | | NULL | | 33 +---------+--------------+------+-----+---------+-------+ 34 rows in set (0.00 sec) 35 36 #方法三:在所有字段后单独定义primary key 37 create table department3( 38 id int, 39 name varchar(20), 40 comment varchar(100), 41 constraint pk_name primary key(id); #创建主键并为其命名pk_name 42 43 mysql> desc department3; 44 +---------+--------------+------+-----+---------+-------+ 45 | Field | Type | Null | Key | Default | Extra | 46 +---------+--------------+------+-----+---------+-------+ 47 | id | int(11) | NO | PRI | NULL | | 48 | name | varchar(20) | YES | | NULL | | 49 | comment | varchar(100) | YES | | NULL | | 50 +---------+--------------+------+-----+---------+-------+ 51 rows in set (0.01 sec) 52 53 单列主键
==================多列做主键================
create table service(
ip varchar(15),
port char(5),
service_name varchar(10) not null,
primary key(ip,port)
);
mysql> desc service;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| ip | varchar(15) | NO | PRI | NULL | |
| port | char(5) | NO | PRI | NULL | |
| service_name | varchar(10) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
rows in set (0.00 sec)
mysql> insert into service values
-> (‘172.16.45.10‘,‘3306‘,‘mysqld‘),
-> (‘172.16.45.11‘,‘3306‘,‘mariadb‘)
-> ;
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> insert into service values (‘172.16.45.10‘,‘3306‘,‘nginx‘);
ERROR 1062 (23000): Duplicate entry ‘172.16.45.10-3306‘ for key ‘PRIMARY‘
多列主键
1 ==================多列做主键================ 2 create table service( 3 ip varchar(15), 4 port char(5), 5 service_name varchar(10) not null, 6 primary key(ip,port) 7 ); 8 9 10 mysql> desc service; 11 +--------------+-------------+------+-----+---------+-------+ 12 | Field | Type | Null | Key | Default | Extra | 13 +--------------+-------------+------+-----+---------+-------+ 14 | ip | varchar(15) | NO | PRI | NULL | | 15 | port | char(5) | NO | PRI | NULL | | 16 | service_name | varchar(10) | NO | | NULL | | 17 +--------------+-------------+------+-----+---------+-------+ 18 rows in set (0.00 sec) 19 20 mysql> insert into service values 21 -> (‘172.16.45.10‘,‘3306‘,‘mysqld‘), 22 -> (‘172.16.45.11‘,‘3306‘,‘mariadb‘) 23 -> ; 24 Query OK, 2 rows affected (0.00 sec) 25 Records: 2 Duplicates: 0 Warnings: 0 26 27 mysql> insert into service values (‘172.16.45.10‘,‘3306‘,‘nginx‘); 28 ERROR 1062 (23000): Duplicate entry ‘172.16.45.10-3306‘ for key ‘PRIMARY‘
五、auto_increment (自增约束)
步长increment与起始偏移量offset:auto_increment_increment,auto_increment_offset
3.--------偏移量:auto_increment_offset--------- ==============没有设置偏移量的时候 create table dep( id int primary key auto_increment, name char(10) ); insert into dep(name) values(‘IT‘),(‘HR‘),(‘EFO‘); select * from dep; ================设置自增的时候以10开头 create table dep1( id int primary key auto_increment, name char(10) )auto_increment = 10; insert into dep1(name) values(‘IT‘),(‘HR‘),(‘EFO‘); select * from dep1; ===============auto_increment_increment:自增步长 create table dep3( id int primary key auto_increment, name char(10) ); 会话:通过客户端连到服务端(一次链接称为一次会话) set session auto_increment_increment = 2; #会话级,只对当前会话有效 set global auto_increment_increment=2; #全局,对所有的会话都有效 insert into dep3(name) values(‘IT‘),(‘HR‘),(‘SALE‘),(‘Boss‘); -----------查看变量---------- show variables like ‘%auto_in%‘;#查看变量。只要包含auto_in就都查出来了 =========auto_increment_offset:偏移量+auto_increment_increment:步长=========== 注意:如果auto_increment_offset的值大于auto_increment_increment的值, 则auto_increment_offset的值会被忽略 set session auto_increment_offset=2; set session auto_increment_increment=3; show variables like ‘%auto_in%‘; create table dep4( id int primary key auto_increment, name char(10) ); insert into dep4(name) values(‘IT‘),(‘HR‘),(‘SALE‘),(‘Boss‘);
六、foreign key (外键约束)
员工信息表有三个字段:工号 姓名 部门
公司有3个部门,但是有1个亿的员工,那意味着部门这个字段需要重复存储,部门名字越长,越浪费
解决方法:
我们完全可以定义一个部门表
然后让员工信息表关联该表,如何关联,即foreign key
如下图简单的表示了一下员工表与部门表的关系,即员工表的(dep_id)要关联部门表的id字段
多对一(一个表多条记录的某一字段关联另一张表的唯一一个字段):员工有部门,部门又有好多信息,所以 分开建了一张部门表,部门表的id 和员工表里面 的dep_id相关联。(dep_id要关联部门表的id字段 (注意:1.先建被关联的表, 2.被关联的字段必须唯一 3.先给被关联的表插入记录 ) 先建张部门表(被关联表) create table dep( id int not null unique, #id int primary key auto_increment, name varchar(50), comment varchar(100) ); 再建张员工表(关联表) create table emp_info( id int primary key auto_increment, name varchar(20), dep_id int, constraint FK_depid_id foreign key(dep_id) references dep(id) #references :关联 on delete cascade #关联的表删了,被关联的表也删了 on update cascade #关联的表修改了,被关联的表也修改了 ); #先给被关联的表初始化记录 insert into dep values (1,‘欧德博爱技术有限事业部‘,‘说的好...‘), (2,‘艾利克斯人力资源部‘,‘招不到人‘), (3,‘销售部‘,‘卖不出东西‘); insert into emp_info values (1,‘egon‘,1), (2,‘alex1‘,2), (3,‘alex2‘,2), (4,‘alex3‘,2), (5,‘李坦克‘,3), (6,‘刘飞机‘,3), (7,‘张火箭‘,3), (8,‘林子弹‘,3), (9,‘加特林‘,3); #修改 update dep set id =301 where id = 2; select * from dep; delect * from em_info; 如果部门解散了,员工也就走吧,就是部门表没了, 员工表也就没有了。
运行结果如下图:
查看创建的表
修改id=301
查看被关联表和关联表
MySQL数据库学习【第五篇】完整性约束
标签:ons 就是 pos ble efault records 多列 填充 人力