-- 创建数据库
create database firstdb;
-- 使用数据库
use firstdb;
-- 创建数据表
create table pet(
name varchar(20),
owner varchar(20),
species varchar(20),
sex varchar(4),
birth DATE,
death DATE
);
-- 插入数据
insert into pet
VALUES('旺财','周星星','狗','公','1998-08-20',NULL);
insert into pet
VALUES('旺旺','周小星','狗','母','1997-08-20',NULL);
insert into pet
VALUES('汪汪','周大星','狗','公','1996-08-20',NULL);
insert into pet
VALUES('望望','星星周','狗','母','1995-08-20',NULL);
insert into pet
VALUES('大黄','周星驰','狗','公','1994-08-20',NULL);
--查看数据
select * from pet;
-- 删除数据
delete from pet where name='望望';
-- 修改数据
update pet set name='旺旺财' where name='旺财';
主键约束 primary key
-- 它能确定表中唯一的数据;使得数据不重复且不为空
mysql> create table user(
-> id int primary key,
-> name varchar(20));
插入一条id为1的张三
INSERT INTO user VALUES(1,'张三');
mysql> select * from user;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
+----+--------+
--再插入一条id为1的张三就会报错
mysql> INSERT INTO user VALUES(2,'张三');
ERROR 1062 (23000): Duplicate entry '1' for key 'user.PRIMARY'
--插入一条id为2的张三则不会报错
mysql> INSERT INTO user VALUES(2,'张三');
Query OK, 1 row affected (0.06 sec)
mysql> select * from user;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 张三 |
+----+--------+
2 rows in set (0.00 sec)
-- 主键为null则报错
mysql> INSERT INTO user VALUES(NULL,'张三');
ERROR 1048 (23000): Column 'id' cannot be null
联合主键
CREATE TABLE user2(
id int,
name varchar(20),
password varchar(20),
primary key(id,name)
);
-- 只要两个主键加起来不相等,则不会报错
mysql> INSERT INTO user2 VALUES(1,'张三','123');
Query OK, 1 row affected (0.05 sec)
mysql> INSERT INTO user2 VALUES(2,'张三','123');
Query OK, 1 row affected (0.05 sec)
mysql> select * from user2;
+----+--------+----------+
| id | name | password |
+----+--------+----------+
| 1 | 张三 | 123 |
| 2 | 张三 | 123 |
+----+--------+----------+
-- 只要任一主键为空,则会报错
mysql> INSERT INTO user2 VALUES(null,'李四','123');
ERROR 1048 (23000): Column 'id' cannot be null
自增约束 auto_increment
-- 与主键搭配使用 插入数据时不插入主键数据,系统会自动为你添加一个值
CREATE TABLE user3(
id int primary key AUTO_INCREMENT,
name varchar(20)
);
-- 第一次插入
mysql> INSERT INTO user3 (name) VALUES('张三');
Query OK, 1 row affected (0.12 sec)
mysql> select * from user3;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
+----+--------+
1 row in set (0.00 sec)
-- 第二次插入
mysql> INSERT INTO user3 (name) VALUES('张三');
Query OK, 1 row affected (0.07 sec)
mysql> select * from user3;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
| 2 | 张三 |
+----+--------+
插入主键 (不常见)
CREATE TABLE user4(
id int,
name varchar(20)
);
mysql> ALTER TABLE user4 add primary key(id);
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0
--使用modify也可以添加主键 (modify修改数值类型)
--ALTER TABLE user4 MODIFY id int primary key;
mysql> DESC user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
删除主键(不常见)
mysql> ALTER TABLE user4 drop primary key;
Query OK, 0 rows affected (0.67 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> DESC user4;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | NO | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
唯一约束 unique
-- primary key = unique + not null
-- unique 使用方法和 primary key 几乎相同(删除的方法不相同),但是使用情景不同.
-- unique 可定义多个,primary key 原则上只定义一个.
CREATE TABLE user5(
id int,
name varchar(20) unique
);
mysql> desc user5;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
-- 删除唯一约束 注意↓↓
mysql> ALTER TABLE user5 drop index name;
Query OK, 0 rows affected (0.37 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user5;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
-- 添加唯一约束 注意↓↓
mysql> alter table user5 add unique(name);
Query OK, 0 rows affected (0.60 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc user5;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
非空约束 not null
-- 即不允许为空,约束后,约束的值插入为空则报错
CREATE TABLE user6(
id int,
name varchar(20) not null
);
mysql> desc user6;
--↓↓↓NULL中YES为允许,NO为不允许
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
mysql> INSERT INTO user6 (id) VALUES('1');
ERROR 1364 (HY000): Field 'name' does not have a default value;
默认约束 default
-- 当你未给 添加了默认约束的值 添加值时,系统会给你添加设置的默认值
CREATE TABLE user7(
id int,
name varchar(20) default '无名',
age varchar(10)
);
mysql>desc user7;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| name | varchar(20) | YES | | 无名 | |
| age | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
mysql> INSERT INTO user7 (id, age) VALUES(1,18);
Query OK, 1 row affected (0.08 sec)
mysql> SELECT * FROM user7;
+------+--------+------+
| id | name | age |
+------+--------+------+
| 1 | 无名 | 18 |
+------+--------+------+
1 row in set (0.00 sec)
外键约束
-- 父表与子表相联系,在子表中约束,子表中 约束后的值 只能使用 父表中有的值
CREATE TABLE classes(
id int primary key,
name varchar(20)
);
CREATE TABLE students(
id int primary key,
name varchar(20),
class_id int,
foreign key(class_id) references classes(id) --该表的class_id连接了classes表中的id值
);
mysql> desc students;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int | NO | PRI | NULL | |
| name | varchar(20) | YES | | NULL | |
| class_id | int | YES | MUL | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
INSERT INTO classes VALUES(1,'一班');
INSERT INTO classes VALUES(2,'二班');
INSERT INTO classes VALUES(3,'三班');
INSERT INTO classes VALUES(4,'四班');
mysql> select * from classes;
+----+--------+
| id | name |
+----+--------+
| 1 | 一班 |
| 2 | 二班 |
| 3 | 三班 |
| 4 | 四班 |
+----+--------+
4 rows in set (0.00 sec)
INSERT INTO students VALUES(1001,'张三',1);
INSERT INTO students VALUES(2001,'张三',2);
INSERT INTO students VALUES(3001,'张三',3);
INSERT INTO students VALUES(4001,'张三',4);
mysql> select * from students;
+------+--------+----------+
| id | name | class_id |
+------+--------+----------+
| 1001 | 张三 | 1 |
| 2001 | 张三 | 2 |
| 3001 | 张三 | 3 |
| 4001 | 张三 | 4 |
+------+--------+----------+
4 rows in set (0.00 sec)
-- students的class_id插入了一个父表中id不存在的值→报错
mysql> INSERT INTO students VALUES(5001,'张三',5);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`firstdb`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
-- 父表被绑定后 无法删除已经被使用的值
mysql> delete from classes where id=1;
ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`firstdb`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))