表的完整性约束
二:not null与default
create table t1(
id int primary key auto_increment,
name varchar(16) not null,
sex enum('male','female') not null default 'male'
);
PRIMARY KEY (PK) 标识该字段为该表的主键,可以唯一的标识记录 唯一标识且不为空
FOREIGN KEY (FK) 标识该字段为该表的外键
NOT NULL 标识该字段不能为空
UNIQUE KEY (UK) 标识该字段的值是唯一的
AUTO_INCREMENT 标识该字段的值自动增长(整数类型,而且为主键)
DEFAULT 为该字段设置默认值
UNSIGNED 无符号
ZEROFILL 使用0填充
mysql> desc t1;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
+-------+-----------------------+------+-----+---------+----------------+
不为空且没有设默认值,传值会报错
mysql> insert into t1(name) values('egon'),('lxx'),('alex');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select*from t1;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | egon | male |
| 2 | lxx | male |
| 3 | alex | male |
+----+------+------+
三:unique key (索引)约束角度功能 约束指定的字段唯一,传入的值不能重复
egon命令行笔记
unique key
#针对单个字段的唯一
mysql> create database db2;
Query OK, 1 row affected (0.01 sec)
mysql> use db2;
Database changed
mysql> create table t1(
-> id int primary key auto_increment,
-> name varchar(16) not null,
-> sex enum('male','female') not null default 'male'
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> desc t1;
+-------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(16) | NO | | NULL | |
| sex | enum('male','female') | NO | | male | |
+-------+-----------------------+------+-----+---------+----------------+
3 rows in set (0.04 sec)
mysql> insert into t1(name) values('egon'),('lxx'),('alex');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select*from t1;
+----+------+------+
| id | name | sex |
+----+------+------+
| 1 | egon | male |
| 2 | lxx | male |
| 3 | alex | male |
+----+------+------+
3 rows in set (0.00 sec)
mysql> create table t2(x int unique);
Query OK, 0 rows affected (0.02 sec)
mysql> create table t3(
-> x int,
-> y varchar(5),
-> unique key(x)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x | int(11) | YES | UNI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.02 sec)
mysql> desc t3;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x | int(11) | YES | UNI | NULL | |
| y | varchar(5) | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql> show create table t2;
+-------+-----------------------------------------------------------------------
----------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
----------------------------------------+
| t2 | CREATE TABLE `t2` (
`x` int(11) DEFAULT NULL,
UNIQUE KEY `x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
----------------------------------------+
1 row in set (0.00 sec)
mysql> show create table t3;
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------+
| t3 | CREATE TABLE `t3` (
`x` int(11) DEFAULT NULL,
`y` varchar(5) DEFAULT NULL,
UNIQUE KEY `x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
-----------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> create table t4(
-> x int,
-> y varchar(5),
-> constraint uni_x unique key(x)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> show create table t4;
+-------+-----------------------------------------------------------------------
---------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
---------------------------------------------------------------------------+
| t4 | CREATE TABLE `t4` (
`x` int(11) DEFAULT NULL,
`y` varchar(5) DEFAULT NULL,
UNIQUE KEY `uni_x` (`x`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+-----------------------------------------------------------------------
---------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> desc t2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x | int(11) | YES | UNI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> insert into t2 values(null);
Query OK, 1 row affected (0.00 sec)
mysql> select*from t2;
+------+
| x |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> select*from t2;
+------+
| x |
+------+
| NULL |
+------+
1 row in set (0.00 sec)
mysql> insert into t2 values(null);
Query OK, 1 row affected (0.00 sec)
mysql> select*from t2;
+------+
| x |
+------+
| NULL |
| NULL |
+------+
2 rows in set (0.00 sec)
mysql> insert into t2 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> select*from t2;
+------+
| x |
+------+
| NULL |
| NULL |
| 1 |
+------+
3 rows in set (0.00 sec)
unique多列联合起来唯一
mysql> create table service(
-> ip varchar(15),
-> port int,
-> unique key(ip,port)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc service;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ip | varchar(15) | YES | MUL | NULL | |
| port | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)
mysql> insert into service values('1.1.1.1',3306);
Query OK, 1 row affected (0.00 sec)
mysql> select*from service;
+---------+------+
| ip | port |
+---------+------+
| 1.1.1.1 | 3306 |
+---------+------+
1 row in set (0.00 sec)
mysql> insert into service values('1.1.1.1',3306);
ERROR 1062 (23000): Duplicate entry '1.1.1.1-3306' for key 'ip'
mysql> insert into service values('1.1.1.1',3356);
Query OK, 1 row affected (0.00 sec)
mysql> insert into service values('1.1.1.2',3306);
Query OK, 1 row affected (0.00 sec)
mysql> select*from service;
+---------+------+
| ip | port |
+---------+------+
| 1.1.1.1 | 3306 |
| 1.1.1.1 | 3356 |
| 1.1.1.2 | 3306 |
+---------+------+
3 rows in set (0.00 sec)
四 primary key 提升搜索效率
站在约束角度看primary key=not null unique 相当于结合体
以后但凡建表,必须注意:
1、必须有且只有一个主键
2、通常是id字段被设置为主键
create table t5(
id int primary key auto_increment,
);
mysql> desc t5;
+-------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
+-------+---------+------+-----+---------+----------------+
1 row in set (0.01 sec)
mysql> insert into t5 values(null);
Query OK, 1 row affected (0.00 sec)
mysql> select*from t5;
+----+
| id |
+----+
| 1 |
+----+
1 row in set (0.00 sec)
mysql> create table t6(x int primary key);
Query OK, 0 rows affected (0.02 sec)
mysql> desc t6;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| x | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
1 row in set (0.03 sec)
mysql> insert into t6 values(null);
ERROR 1048 (23000): Column 'x' cannot be null
mysql> insert into t6 values(1);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t6 values(1);
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> select*from t6;
+---+
| x |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
#不能指定多个主键的情况
mysql> create table t7(x int primary key,y int primary key);
ERROR 1068 (42000): Multiple primary key defined
#未指定主键默认自定义主键
mysql> create table t7(x int,y varchar(5),z int not null unique);
Query OK, 0 rows affected (0.02 sec)
mysql> desc t7;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x | int(11) | YES | | NULL | |
| y | varchar(5) | YES | | NULL | |
| z | int(11) | NO | PRI | NULL | |
+-------+------------+------+-----+---------+-------+
只要是innodb存储引擎一定会要主键
mysql> create table t8(x int,y varchar(5),z int);
Query OK, 0 rows affected (0.03 sec)
mysql> create table t9(x int,y varchar(5),z int not null unique,m int not null u
nique);
Query OK, 0 rows affected (0.22 sec)
mysql> desc t9;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| x | int(11) | YES | | NULL | |
| y | varchar(5) | YES | | NULL | |
| z | int(11) | NO | PRI | NULL | |
| m | int(11) | NO | UNI | NULL | |
+-------+------------+------+-----+---------+-------+
4 rows in set (0.03 sec)
表的存储引擎
自己处理自己的数据用不同的文件来处理,文件分不同的类型,表也分不同的类型
表的类型有一个专有的名词,称之为存储引擎,为一个表指定了存储引擎,相当于创建了表的类型


transaction交易
rowlevel行级锁
四:primary key(索引)约束角度的功能
站在约束角度看primary key=not null unique
index key(索引)
目的是为了加速查询用的,不常用
存储引擎
五 foreign key:限制关联表某一个字段的值必是来自于被关联表的一个字段的 外键

foreign key注意:
1、被关联的字段必须是一个key,通常是id字段
2、创建表时:必须先建立被关联的表,才能建立关联表
create table dep(
id int primary key auto_increment,
dname varchar(20),
info varchar(50)
);
create table emp(
id int primary key auto_increment,
name varchar(15),
age int,
dep_id int,
foreign key(dep_id) references dep(id)
on update cascade
on delete cascade
);
先先建立被关联的表
# 3、插入记录时:必须先往被关联的表插入记录,才能往关联表中插入记录
insert into dep(dname,info) values
('IT','技术能力有限部门xxx'),
('Sale','文化程度不高'),
('HR','招不到人部门');
insert into emp(name,age,dep_id) values
('egon',18,1),
('alex',28,2),
('wsj',38,2),
('lxx',30,1),
('xiaohou',18,3);
egon上课笔记
先创建被关联的表,再创建关联的表
mysql> use sb2;
Database changed
mysql> create table dep(
-> id int primary key auto_increment,
-> dname varchar(20),
-> info varchar(50)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> create table emp(
-> id int primary key auto_increment,
-> name varchar(15),
-> age int,
-> dep_id int,
-> foreign key(dep_id) references dep(id)
-> on update cascade
-> on delete cascade
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> desc emp;
+--------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(15) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | MUL | NULL | |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)
mysql> desc dep;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| dname | varchar(20) | YES | | NULL | |
| info | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
因为外键的功能是限制dep_id的值不能乱写一定是来自于被关联的这张表的值
插入记录时:必须先往被关联的表插入记录,才能往关联表插入记录
mysql> insert into dep(dname,info) values
-> ('IT','技术能力有限部门xxx'),
-> ('Sale','文化程度不高'),
-> ('HR','招不到人部门');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into emp(name,age,dep_id) values
-> ('egon',18,1),
-> ('alex',28,2),
-> ('wsj',38,2),
-> ('lxx',30,1),
-> ('xiaohou',18,3);
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select*from dep;
+----+-------+---------------------+
| id | dname | info |
+----+-------+---------------------+
| 1 | IT | 技术能力有限部门xxx |
| 2 | Sale | 文化程度不高 |
| 3 | HR | 招不到人部门 |
+----+-------+---------------------+
3 rows in set (0.00 sec)
mysql> select*from emp;
+----+---------+------+--------+
| id | name | age | dep_id |
+----+---------+------+--------+
| 2 | egon | 18 | 1 |
| 3 | alex | 28 | 2 |
| 4 | wsj | 38 | 2 |
| 5 | lxx | 30 | 1 |
| 6 | xiaohou | 18 | 3 |
+----+---------+------+--------+
5 rows in set (0.00 sec)
mysql> delete from dep where id=2;
Query OK, 1 row affected (0.00 sec)
mysql> select*from dep;
+----+-------+---------------------+
| id | dname | info |
+----+-------+---------------------+
| 1 | IT | 技术能力有限部门xxx |
| 3 | HR | 招不到人部门 |
+----+-------+---------------------+
2 rows in set (0.00 sec)
mysql> select*from emp;
+----+---------+------+--------+
| id | name | age | dep_id |
+----+---------+------+--------+
| 2 | egon | 18 | 1 |
| 5 | lxx | 30 | 1 |
| 6 | xiaohou | 18 | 3 |
+----+---------+------+--------+
3 rows in set (0.00 sec)
mysql> drop table dep;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrai
nt fails
删除时应该先删除关联表,再删除被关联表
mysql> create table dep(
-> id int primary key auto_incremen
-> dname varchar(20),
-> info varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> create table emp(
-> id int primary key auto_incremen
-> name varchar(15),
-> age int,
-> dep_id int,
-> foreign key(dep_id) references d
-> on update cascade
-> on delete cascade
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> insert into dep(dname,info) valu
-> ('IT','技术能力有限部门xxx'),
-> ('Sale','文化程度不高'),
-> ('HR','招不到人部门');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> insert into emp(name,age,dep_id)
-> ('egon',18,1),
-> ('alex',28,2),
-> ('wsj',38,2),
-> ('lxx',30,1),
-> ('xiaohou',18,3);
Query OK, 5 rows affected (0.01 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql> select*from dep;
+----+-------+---------------------+
| id | dname | info |
+----+-------+---------------------+
| 1 | IT | 技术能力有限部门xxx |
| 2 | Sale | 文化程度不高 |
| 3 | HR | 招不到人部门 |
+----+-------+---------------------+
3 rows in set (0.00 sec)
mysql> select*from emp;
+----+---------+------+--------+
| id | name | age | dep_id |
+----+---------+------+--------+
| 1 | egon | 18 | 1 |
| 2 | alex | 28 | 2 |
| 3 | wsj | 38 | 2 |
| 4 | lxx | 30 | 1 |
| 5 | xiaohou | 18 | 3 |
+----+---------+------+--------+
5 rows in set (0.00 sec)
create table t12(x int)engine='myisam'; create table t13(x int)engine='innodb'; create table t14(x int)engine='memory'; create table t15(x int)engine='blackhole'; frm存表的结构 myd代表的是myisam存储引擎的数据文件 myi代表的是index的意思存放的是索引 ibd代表的是又存数据又存索引 客户端不存数据,存的是服务端

一对一

表之间的关系,多对一
表之间的关系,多对多

找两张表关系的窍门
emp dep
1; 先站在左表的角度;去找左表emp的多条记录能否对应对应右表dep的一条记录
翻译:多个员工能否属于一个部门
2:然后站在右表的角度;去找右表dep的多条记录能否对应左表emp的一条记录
翻译:多个部门能否拥有一名员工
多对一结果的判断
1、如果只有单向的多对一成立,那么最终的关系就是多对一
2、在emp表新增一个字段dep_id,该字段外键关联dep(id)
多对多:结果的判断
1、双向的多对一就是多对多
2、需要建立第三张表,又一个字段fk左表,一个字段fk右表
实战:
create table author(
id int primary key auto_increment,
name varchar(16),
age int);
create table book(
id int primary key auto_increment,
bname varchar(20),
price int);
create table author2book(
id int primary key auto_increment,
author_id int,
book_id int,
foreign key(author_id) references author(id) on update cascade on delete cascade,
foreign key(book_id) references book(id) on update cascade on delete cascade);
表其他相关操作
修改表
语法:
1. 修改表名
ALTER TABLE 表名
RENAME 新表名;
2. 增加字段
ALTER TABLE 表名
ADD 字段名 数据类型 [完整性约束条件…],
ADD 字段名 数据类型 [完整性约束条件…];
ALTER TABLE 表名
ADD 字段名 数据类型 [完整性约束条件…] FIRST;
ALTER TABLE 表名
ADD 字段名 数据类型 [完整性约束条件…] AFTER 字段名;
3. 删除字段
ALTER TABLE 表名
DROP 字段名;
4. 修改字段
ALTER TABLE 表名
MODIFY 字段名 数据类型 [完整性约束条件…];
ALTER TABLE 表名
CHANGE 旧字段名 新字段名 旧数据类型 [完整性约束条件…];
ALTER TABLE 表名
CHANGE 旧字段名 新字段名 新数据类型 [完整性约束条件…];
复制表
G使表有序排列