一、数据表创建
创建数据库、数据表、插入数据
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| user |
+--------------------+
5 rows in set (0.00 sec)
mysql> create database shuju;
Query OK, 1 row affected (0.00 sec)
mysql> use shuju;
Database changed
mysql> create table t1 (id int(11),name varchar(25),deptid int(11),salary float);
Query OK, 0 rows affected (0.11 sec)
mysql> show tables;
+-----------------+
| Tables_in_shuju |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
mysql> insert into t1 (id,name,deptid,salary) values (1,'zhangsan','1','10000');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+----------+--------+--------+
| id | name | deptid | salary |
+------+----------+--------+--------+
| 1 | zhangsan | 1 | 10000 |
+------+----------+--------+--------+
1 row in set (0.00 sec)
二、约束策略
2.1 主键约束
PS:主键约束要求主键列的数据唯一,并且不允许为空。
2.1.1 单字段主键
mysql> create table t2
-> (
-> id int (11) primary key,
-> name varchar(25),
-> deptid int(11),
-> salary float
-> );
Query OK, 0 rows affected (0.36 sec)
2.1.2 尾端指定主键
PS:在定义完所有列之后指定主键
mysql> create table t3
-> (
-> id int(11),
-> name varchar(25),
-> deptid int (11),
-> salary float,
-> primary key (id)
-> );
Query OK, 0 rows affected (0.01 sec)
2.1.3 多字段联合主键
PS:在定义完列表之后统一指定主键
mysql> create table t4
-> (
-> id int(11),
-> name varchar(25),
-> deptid int (11),
-> salary float,
-> primary key (id,name,deptid)
-> );
Query OK, 0 rows affected (0.00 sec)
2.2 外键约束
PS:外键用来在两个表数据之间建立连接,它可以是一列或者多列
2.2.1 创建外键约束
mysql> create table tb_t1
-> (
-> id int(11) primary key,
-> name varchar(22) not null,
-> location varchar(50)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> create table t5
-> (
-> id int (11) primary key,
-> name varchar (25),
-> deptid int(11),
-> salary float,
-> constraint tb_t1_t5 foreign key(deptid) references tb_t1(id)
-> );
Query OK, 0 rows affected (0.00 sec)
2.2.2 删除表的外键约束
mysql> create table test
-> (
-> id int (11) primary key,
-> name varchar(25),
-> deptid int(11),
-> salary float,
-> constraint fk_test_tb_t1 foreign key (deptid) references tb_t1(id)
-> );
Query OK, 0 rows affected (0.01 sec)
mysql> show create table test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int(11) NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_test_tb_t1` (`deptid`),
CONSTRAINT `fk_test_tb_t1` FOREIGN KEY (`deptid`) REFERENCES `tb_t1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> alter table test drop foreign key fk_test_tb_t1;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
`id` int(11) NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `fk_test_tb_t1` (`deptid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
2.3 非空约束
PS:非空约束指字段的值不能为空
mysql> create table t6
-> (
-> id int(11) primary key ,
-> name varchar(25) not null,
-> deptid int (11),
-> salary float
-> );
Query OK, 0 rows affected (0.00 sec)
2.4 默认约束
PS:默认约束指定某列的默认值。
mysql> create table t7
-> (
-> id int(11) primary key,
-> name varchar (25) not null,
-> deptid int (11) default 1111,
-> salary float,
-> info varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
验证:
mysql> insert into t7(id,name,salary,info) values(1,'zhansan','12','112');
Query OK, 1 row affected (0.00 sec)
mysql> select * from t7;
+----+---------+--------+--------+------+
| id | name | deptid | salary | info |
+----+---------+--------+--------+------+
| 1 | zhansan | 1111 | 12 | 112 |
+----+---------+--------+--------+------+
1 row in set (0.00 sec)
2.5 设置表的属性值自动增加
2.5.1 创建表
mysql> create table t8
-> (
-> id int(11) primary key auto_increment,
-> name varchar(25) not null,
-> deptid int (11),
-> salary float
-> );
Query OK, 0 rows affected (0.00 sec)
2.5.2 插入数据
mysql> insert into t8
-> (name,salary)
-> values('lucy',1000),('wangwu',1200),('zhangsan',1500);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
2.5.3 查看
mysql> select * from t8;
+----+----------+--------+--------+
| id | name | deptid | salary |
+----+----------+--------+--------+
| 1 | lucy | NULL | 1000 |
| 2 | wangwu | NULL | 1200 |
| 3 | zhangsan | NULL | 1500 |
+----+----------+--------+--------+
3 rows in set (0.00 sec)
2.6 唯一约束
- unique:独特的,独一无二。
mysql> create table tb_t2
-> (
-> id int (11) primary key,
-> name varchar(22),
-> location varchar(50),
-> constraint sth unique(name)
-> );
Query OK, 0 rows affected (0.00 sec)
三 、查看数据表结构
3.1 查看表的基本结构
mysql> describe t1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
或者
mysql> desc t1;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(25) | YES | | NULL | |
| deptid | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
3.2 查看表详细结构
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(11) DEFAULT NULL,
`name` varchar(25) DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
四、修改数据表
4.1 修改表名
mysql> show tables;
+-----------------+
| Tables_in_shuju |
+-----------------+
| t1 |
| t2 |
| t3 |
| t4 |
| t5 |
| t6 |
| t7 |
| t8 |
| tb_t1 |
| tb_t2 |
+-----------------+
10 rows in set (0.00 sec)
mysql> alter table t3 rename tb_t3;
Query OK, 0 rows affected (0.01 sec)
mysql> show tables like 'tb_t3';
+-------------------------+
| Tables_in_shuju (tb_t3) |
+-------------------------+
| tb_t3 |
+-------------------------+
1 row in set (0.00 sec)
4.2 修改字段的数据类型
查看表
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(22) | NO | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
修改字段的数据类型
mysql> alter table tb_t1 modify name varchar(30);
Query OK, 0 rows affected (0.11 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| location | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.3 修改字段名
- change :替换为
mysql> alter table tb_t1 change location local varchar (50);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)
4.4 添加字段
- add:添加
- first:首端,第一个
- after:最后,或者什么什么的后面一个
mysql> alter table tb_t1 add columen1 varchar (12) not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
| columen1 | varchar(12) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
或者
mysql> alter table tb_t1 add column2 int(11) first;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| column2 | int(11) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
| columen1 | varchar(12) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
或者
mysql> alter table tb_t1 add column3 int(11) after name;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| column2 | int(11) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
| columen1 | varchar(12) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)
4.5 删除字段
mysql> alter table tb_t1 drop column2 ;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
| columen1 | varchar(12) | NO | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
4.6 修改字段的排列位置
mysql> alter table tb_t1 modify columen1 varchar(12) first;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| columen1 | varchar(12) | YES | | NULL | |
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
或者
mysql> alter table tb_t1 modify columen1 varchar(12) after local;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc tb_t1;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | varchar(30) | YES | | NULL | |
| column3 | int(11) | YES | | NULL | |
| local | varchar(50) | YES | | NULL | |
| columen1 | varchar(12) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
4.7 更改表的存储引擎
mysql> show create table tb_t3\G
*************************** 1. row ***************************
Table: tb_t3
Create Table: CREATE TABLE `tb_t3` (
`id` int(11) NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> alter table tb_t3 engine=myisam;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table tb_t3\G
*************************** 1. row ***************************
Table: tb_t3
Create Table: CREATE TABLE `tb_t3` (
`id` int(11) NOT NULL,
`name` varchar(25) DEFAULT NULL,
`deptid` int(11) DEFAULT NULL,
`salary` float DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
五、删除数据表
5.1 删除没有被关联的表
mysql> show tables;
+-----------------+
| Tables_in_shuju |
+-----------------+
| t1 |
| t2 |
| t4 |
| t5 |
| t6 |
| t7 |
| t8 |
| tb_t1 |
| tb_t2 |
| tb_t3 |
| test |
+-----------------+
11 rows in set (0.00 sec)
mysql> drop table if exists tb_t2;
Query OK, 0 rows affected (0.00 sec)
5.2 删除被其他表关联的主表
5.2.1 创建表
mysql> create table tb_t4
-> (
-> id int(11) primary key,
-> name varchar(22),
-> location varchar(50)
-> );
Query OK, 0 rows affected (0.01 sec)
5.2.2 创建关联表
mysql> create table tb_tb
-> (
-> id int (11) primary key,
-> name varchar(25),
-> deptid int(11),
-> salary float,
-> constraint tb_tb_t4 foreign key(deptid) references tb_t4(id)
-> );
Query OK, 0 rows affected (0.00 sec)
5.2.3 删除父表(tb_t4)
mysql> drop table tb_t4;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
PS:可以看到在有外键约束时,主表不能直接删除
5.2.4 解除外键约束
mysql> alter table tb_tb drop foreign key tb_tb_t4;
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
5.2.5 删除表
mysql> drop table tb_t4;
Query OK, 0 rows affected (0.01 sec)