Mysql的基础操作
#创建数据库
mysql> create database `abc`;
Query OK, 1 row affected (0.14 sec)
#进入该数据库
mysql> use `abc`;
Database changed
#创建表
# primary key 是主键的意思,auto_increment是自增;
mysql> create table `tab1`(
-> `id` int primary key auto_increment,
-> `name` varchar(20)
-> );
Query OK, 0 rows affected (0.21 sec)
#向表里插入数据
mysql> insert into `tab1`(`id`,`name`)
-> values(1,'鸣人'),
-> (2,'佐助'),
-> (3,'小樱'),
-> (0,'卡卡西')
-> ;
Query OK, 4 rows affected (0.17 sec)
Records: 4 Duplicates: 0 Warnings: 0
#简单查看
show databases;
show tables;
select database();#查看当前在那个数据库;
#查看表内容
mysql> select * from `tab1`;
+----+-----------+
| id | name |
+----+-----------+
| 1 | 鸣人 |
| 2 | 佐助 |
| 3 | 小樱 |
| 4 | 卡卡西 |
+----+-----------+
4 rows in set (0.00 sec)
#查看表结构
mysql> desc `tab1`;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
#给表填入数据
mysql> insert into `tab1_parent`
-> values(1,'风'),
-> (2,'火'),
-> (3,'雷'),
-> (4,'电'),
-> (5,'土')
-> ;
#删除数据
mysql> delete from `tab1`
-> where `name` is null;
Query OK, 4 rows affected (0.08 sec)
#更新数据
mysql> update `tab1_parent`
-> set `id`=0
-> where `id`=1
-> ;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#添加列
mysql> alter table `tab1`
-> add `gender` varchar(6)
-> ;
Query OK, 0 rows affected (0.17 sec)
Records: 0 Duplicates: 0 Warnings: 0
#删除列
mysql> alter table `tab1`
-> drop `gender`
-> ;
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
#更改列的数据类型
mysql> alter table `tab1`
-> modify `name` varchar(10)
-> ;
Query OK, 4 rows affected (0.19 sec)
Records: 4 Duplicates: 0 Warnings: 0
外键约束FOREIGN KEY
外键约束FOREIGN KEY
,保持数据一致性,完整性实现一对一或一对多关系。
外键约束的要求:
数据表的存储引擎只能为InnoDB
外键列和参照列数据类型一致
外键必须关联到键上面去
#查看当前所在那个数据库
mysql> select database();
+------------+
| database() |
+------------+
| mydb |
+------------+
1 row in set (0.00 sec)
#外键要求非空且唯一,主键满足非空且唯一
mysql> create table `dep`(
-> `id` int primary key,
-> `name` varchar(20) not null
-> );
Query OK, 0 rows affected (0.16 sec)
#创建表时添加外键约束
mysql> create table `stu`(
-> `id` int primary key auto_increment,
-> `name` varchar(20) not null,
-> `s_id` int unique key,
-> foreign key(`s_id`) references `dep`(`id`)
-> );
Query OK, 0 rows affected (0.16 sec)
#为以创建的表添加外键
mysql> alter table `stu1`
-> add constraint `stu_ibfk_1`
-> foreign key(`s_id`) references `dep`(`id`)
-> ;
Query OK, 0 rows affected (0.38 sec)
Records: 0 Duplicates: 0 Warnings: 0
#查看表详情
mysql> show create table `stu`\G
*************************** 1. row ***************************
Table: stu
Create Table: CREATE TABLE `stu` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`s_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `s_id` (`s_id`),
CONSTRAINT `stu_ibfk_1` FOREIGN KEY (`s_id`) REFERENCES `dep` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.08 sec)
#删除外键
mysql> alter table `stu`
-> drop foreign key `stu_ibfk_1`;
Query OK, 0 rows affected (0.94 sec)
Records: 0 Duplicates: 0 Warnings: 0
一对多关系
举例,学校中一个学院可以有很多的学生,而一个学生只属于某一个学院(通常情况下),学院与学生之间的关系就是一对多的关系,通过外键关联来实现这种关系。
#例:
#创建学院表:
mysql> CREATE TABLE `department`(
-> `id` INT PRIMARY KEY AUTO_INCREMENT,
-> `name` VARCHAR(15) NOT NULL
-> );
Query OK, 0 rows affected (0.61 sec)
#创建学生表:
mysql> CREATE TABLE `student`(
-> `id` INT PRIMARY KEY AUTO_INCREMENT,
-> `name` VARCHAR(20) NOT NULL,
-> `dept_id` INT,
-> FOREIGN KEY (`dept_id`) REFERENCES `department`(`id`)
-> );
Query OK, 0 rows affected (0.51 sec)
#插入数据
mysql> INSERT INTO `department`(`name`)
-> VALUES('A'),
-> ('B')
-> ;
Query OK, 2 rows affected (0.10 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> INSERT INTO `student`(`name`,`dept_id`)
-> VALUES('s1',1),
-> ('s2',2),
-> ('s3',2)
-> ;
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0my
一对一关系
举例,学生表中有学号、姓名、学院,但学生还有些比如电话,家庭住址等比较私密的信息,这些信息不会放在学生表当中,会新建一个学生的详细信息表来存放。这时的学生表和学生的详细信息表两者的关系就是一对一的关系,因为一个学生只有一条详细信息。用外键加唯一键的方式来实现这种关系。也可以外键加主键(包含唯一约束)。
#例:
#学生表:
mysql> DESC `student`;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
| dept_id | int(11) | YES | MUL | NULL | |
+---------+-------------+------+-----+---------+----------------+
3 rows in set (0.06 sec)
#建立详细学生表1111:外键+主键
mysql> CREATE TABLE `student_details`(
-> `id` INT PRIMARY KEY,
-> `age` INT,
-> `gender` CHAR(1),
-> FOREIGN KEY (`id`) REFERENCES `student`(`id`)
-> );
Query OK, 0 rows affected (0.67 sec)
#建立详细学生表2222: 外键+唯一键
mysql> CREATE TABLE `student_details`(
-> `id` INT PRIMARY KEY AUTO_INCREMENT,
-> `age` INT,
-> `gender` CHAR(1),
-> `s_id` INT UNIQUE KEY,
-> FOREIGN KEY (`s_id`) REFERENCES `student`(`s_id`)
-> );
Query OK, 0 rows affected (0.02 sec)
多对多关系
举例,学生要报名选修课,一个学生可以报名多门课程,一个课程有很多的学生报名,那么学生表和课程表两者就形成了多对多关系。对于多对多关系,需要创建第三张关系表,关系表中通过外键加主键的形式实现这种关系。
#例:
#建立课程表:
mysql> CREATE TABLE `course`(
-> `id` INT PRIMARY KEY AUTO_INCREMENT,
-> `name` VARCHAR(20) NOT NULL,
-> );
Query OK, 0 rows affected (1.18 sec)
#学生与课程多对多关系表
#(需要第三张表,还要建立一个组合主键)
mysql> CREATE TABLE `select`(
-> `s_id` INT,
-> `crs_id` INT,
-> PRIMARY KEY (`s_id`,`crs_id`),
-> FOREIGN KEY (`s_id`) REFERENCES `student` (`id`),
-> FOREIGN KEY (`crs_id`) REFERENCES `course` (`id`)
-> );
Query OK, 0 rows affected (0.50 sec)
外键约束的参照操作:
1.CASCADE从父表删除或更新时自动删除或更新子表中的匹配行
2.SET NULL从父表删除或更新行时,设置子表中的外键列为NULL。
如果使用该选项,必须保证子表列没有指定NOT NULL
3.RESTRICT拒绝对父表的删除或更新操作
4.NO ACTION标准的SQL关键字,在mysql中与RESTRICT作用相同
#先删除外键
mysql> alter table `student`
-> drop foreign key `student_ibfk_1`;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
#查看表详细信息
mysql> show create table `student`\G
*************************** 1. row ***************************
Table: student
Create Table: CREATE TABLE `student` (
`s_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`d_id` int(11) DEFAULT NULL,
PRIMARY KEY (`s_id`),
KEY `student_ibfk_1` (`d_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
#添加外键约束和同步更新
mysql> alter table `student`
-> add constraint `student_ibfk_1`
-> foreign key(`s_id`) references `deparment`(`d_id`)
-> on update cascade;
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0