MySQL学习笔记-约束以及修改数据表

时间:2022-09-21 13:07:50
MySQL学习笔记-约束以及修改数据表
  1. 约束:
  2. 按功能划为:
  3. NOT NULL , PRIMARY KEY , UNIQUE KEY , DEFAULT , FOREIGN KEY
  4. 按数据列的数目划为:
  5. 表级约束,列级约束
  6. 修改数据表:
  7. 针对字段的操作:添加 / 删除字段、修改列定义、 修改列名称等
  8. 针对约束的操作:添加 / 删除各种约束
  9. 针对数据表的操作:数据表更名(两种方式)
  *** 约束保证数据的完整性和一致性*** 约束分为表级约束和列级约束*** 约束类型包括:NOT NULL(非空约束)PRIMARY KEY(主键约束)UNIQUE KEY(唯一约束)DEFAULT(默认约束)FOREIGN KEY(外键约束) 1.外键约束的要求解析 *** 父表和子表必须使用相同的存储引擎,而且禁止使用临时表。*** 数据表的存储引擎只能为InnoDB。*** 外键列和参照列必须具有相似的数据类型。其中数字的长度或是否有符号位必须相同;而字符的长度则可以不同。       主键是默认自带索引的  而外键列创建时一般参照的是带有主键那一列  因此如果外键列没有创建索引的话就会被MYSQL根据参照列的索引创建一个索引  *** 外键列是不可以以一个没有索引的列作为参照列的1.参照列必须要创建一个索引(如果用的是主键默认自带索引  所以不用创建)  2.外键列随意  如果不创建索引会被创建【1】编辑数据表的默认存储引擎,配置文件/etc/my.cnf

 

1 [mysqld]
2 default-storage-engine=INNODB

 

【2】 创建省份数据表,查看存储引擎MySQL学习笔记-约束以及修改数据表
mysql> USE test;
mysql
> CREATE TABLE provinces(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> pname VARCHAR(20) NOT NULL
-> );
mysql
> SHOW CREATE TABLE provinces;
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb0 | CREATE TABLE `provinces` (
`id`
smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`pname`
varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE
=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------+
MySQL学习笔记-约束以及修改数据表

 

【3】创建用户数据表,其中外键列必须和参照列必须有相似的数据类型MySQL学习笔记-约束以及修改数据表
mysql> CREATE TABLE users(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(10) NOT NULL,
-> pid SMALLINT UNSIGNED,
-> FOREIGN KEY (pid) REFERENCES provinces (id)
-> );
MySQL学习笔记-约束以及修改数据表

 

 
  • 注意:其中有外键列的users表为子表,有参照列id列provinces为父表
  • 创建主键时自动创建索引,查看父表自动创建的1个索引
MySQL学习笔记-约束以及修改数据表
mysql> SHOW INDEXES FROM provinces\G;
*************************** 1. row ***************************
Table: provinces
Non_unique:
0
Key_name:
PRIMARY
Seq_in_index:
1
Column_name: id
Collation: A
Cardinality:
0
Sub_part:
NULL
Packed:
NULL
Null:
Index_type: BTREE
Comment:
1 row in set (0.00 sec)
MySQL学习笔记-约束以及修改数据表

 

  • 若外键列没有索引,自动创建:
MySQL学习笔记-约束以及修改数据表
mysql> SHOW INDEXES FROM users\G;    
*************************** 1. row ***************************
Table: users
Non_unique:
0
Key_name:
PRIMARY
Seq_in_index:
1
Column_name: id
Collation: A
Cardinality:
0
Sub_part:
NULL
Packed:
NULL
Null:
Index_type: BTREE
Comment:
*************************** 2. row ***************************
Table: users
Non_unique:
1
Key_name: pid
Seq_in_index:
1
Column_name: pid
Collation: A
Cardinality:
0
Sub_part:
NULL
Packed:
NULL
Null: YES
Index_type: BTREE
Comment:
2 rows in set (0.00 sec)
MySQL学习笔记-约束以及修改数据表

 

2.外键约束的参照操作*** cascade :从父表删除或更新且自动删除或更新子表中匹配的行*** set null :从父表删除或更新行,并设置子表中的外键列为NULL。如果使用该选项,必须保证子表列没有指定NOT NULL*** restrict :拒绝对父表的删除或更新操作*** no action :标准的SQL的关键词,在MySQL中与restrict相同CASCADE例: MySQL学习笔记-约束以及修改数据表
mysql> CREATE TABLE provinces(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> pname VARCHAR(20) NOT NULL
-> );

mysql
> CREATE TABLE users1(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(10) NOT NULL,
-> pid SMALLINT UNSIGNED,
-> FOREIGN KEY (pid) REFERENCES provinces (id) ON DELETE CASCADE
-> );

mysql
> INSERT provinces(pname) VALUES('Tom');
mysql
> INSERT provinces(pname) VALUES('John');
mysql
> INSERT provinces(pname) VALUES('Driver');

mysql
> INSERT users1(username,pid) VALUES('Huang',2);
mysql
> INSERT users1(username,pid) VALUES('Li',3);
mysql
> INSERT users1(username,pid) VALUES('Pan',3);
mysql
> INSERT users1(username,pid) VALUES('He',1);
mysql
> INSERT users1(username,pid) VALUES('Long',2);
mysql
> SELECT * FROM users1;
+----+----------+------+
| id | username | pid |
+----+----------+------+
| 1 | Huang | 2 |
| 2 | Li | 3 |
| 5 | Pan | 3 |
| 6 | He | 1 |
| 7 | Long | 2 |
+----+----------+------+
mysql> SELECT * FROM provinces;
+----+--------+
| id | pname |
+----+--------+
| 1 | Tom |
| 2 | John |
| 3 | Driver |
+----+--------+

mysql
> DELETE FROM provinces WHERE id=3;
mysql
> SELECT * FROM provinces;
+----+-------+
| id | pname |
+----+-------+
| 1 | Tom |
| 2 | John |
+----+-------+
mysql> SELECT * FROM users1;
+----+----------+------+
| id | username | pid |
+----+----------+------+
| 1 | Huang | 2 |
| 6 | He | 1 |
| 7 | Long | 2 |
+----+----------+------+
MySQL学习笔记-约束以及修改数据表

 

3.表级约束和列级约束*** 对一个数据列建立的约束,称为列级约束*** 对多个数据列建立的约束,称为表级约束*** 列级约束既可以在列定义时声明,也可以在列定义后声明*** 表级约束只能在列定以后声明4.修改数据表添加单列
ALTER TABLE tbl_name(数据表名称) ADD [COLUMN] col_name(列名) column_definition(列定义) [FIRST(插入列到最前面)|AFTER(插入列到指定列的后方) col_name(列名)(如果不写FIRST和AFTER,则插入到最后一列)]

 

  • 例:
MySQL学习笔记-约束以及修改数据表
mysql> SHOW COLUMNS FROM users1;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(10) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
+----------+----------------------+------+-----+---------+----------------+
MySQL学习笔记-约束以及修改数据表

 

  • 添加age列到最后一列:
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users1 ADD age TINYINT UNSIGNED NOT NULL DEFAULT 10;
mysql
> SHOW COLUMNS FROM users1;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(10) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | 10 | |
+----------+----------------------+------+-----+---------+----------------+
MySQL学习笔记-约束以及修改数据表

 

  • 添加password列到username列的后面:
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users1 ADD password VARCHAR(32) NOT NULL AFTER username; 
mysql
> SHOW COLUMNS FROM users1;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(10) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | 10 | |
+----------+----------------------+------+-----+---------+----------------+
MySQL学习笔记-约束以及修改数据表

 

  • 添加truename列到第一列:
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users1 ADD truename VARCHAR(32) NOT NULL FIRST;
mysql
> SHOW COLUMNS FROM users1;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| truename | varchar(32) | NO | | NULL | |
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(10) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | 10 | |
+----------+----------------------+------+-----+---------+----------------+
MySQL学习笔记-约束以及修改数据表

 

删除列:
ALTER TABLE tbl_name(数据表名称) DROP [COLUMN] col_name(列名)

 

  • 例,删除truename列:
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users1 DROP truename;
mysql
> SHOW COLUMNS FROM users1;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(10) | NO | | NULL | |
| password | varchar(32) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| age | tinyint(3) unsigned | NO | | 10 | |
+----------+----------------------+------+-----+---------+----------------+
MySQL学习笔记-约束以及修改数据表

 

  • 同时删除age和password列,(也可同时增加删除)用逗号分割:
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users1 DROP age,DROP password;
mysql
> SHOW COLUMNS FROM users1;
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(10) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
+----------+----------------------+------+-----+---------+----------------+
MySQL学习笔记-约束以及修改数据表

 

添加 / 删除主键约束:
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)       //添加

ALTER TABLE tbl_name DROP PRIMARY KEY //删除

 

添加 / 删除唯一约束:
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name,...)    //添加

ALTER TABLE tbl_name DROP {INDEX|KEY} index_name //删除

 

添加 / 删除外键约束:
ALTER TABLE tbl_name ADD [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name,...) reference_definition    //添加

ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol //删除

 

添加 / 删除默认约束:
ALTER TABLE tbl_name ALTER [COLUMN] col_name {SET DEFAULT literal|DROP DEFAULT}    //添加
mysql
> ALTER TABLE tbl_name ALTER age DROP DEFAULT; //删除

 

  • 添加主键约束:创建users2数据表,增加id列,设置id列为主键:
MySQL学习笔记-约束以及修改数据表
mysql> CREATE TABLE users2(
-> username VARCHAR(20) NOT NULL,
-> pid SMALLINT UNSIGNED
-> );
mysql
> ALTER TABLE users2 ADD id SMALLINT UNSIGNED;
mysql
> ALTER TABLE users2 ADD CONSTRAINT pk_users2_id PRIMARY KEY (id);
mysql
> SHOW COLUMNS FROM users2;
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20) | NO | | NULL | |
| pid | smallint(5) unsigned | YES | | NULL | |
| id | smallint(5) unsigned | NO | PRI | 0 | |
+----------+----------------------+------+-----+---------+-------+
MySQL学习笔记-约束以及修改数据表

 

  • 删除主键约束:
mysql> ALTER TABLE users2 DROP PRIMARY KEY;

 

  •  添加唯一约束:添加username列为唯一约束:
mysql> ALTER TABLE users2 ADD CONSTRAINT OK_users2_username UNIQUE KEY (username);

 

  • 删除唯一约束:
MySQL学习笔记-约束以及修改数据表
mysql> SHOW INDEXES FROM users2\G;
*************************** 1. row ***************************
Table: users2
Non_unique:
0
Key_name: OK_users2_username
Seq_in_index:
1
Column_name: username
Collation: A
Cardinality:
0
Sub_part:
NULL
Packed:
NULL
Null:
Index_type: BTREE
Comment:
*************************** 2. row ***************************
Table: users2
Non_unique:
1
Key_name: pid
Seq_in_index:
1
Column_name: pid
Collation: A
Cardinality:
0
Sub_part:
NULL
Packed:
NULL
Null: YES
Index_type: BTREE
Comment:
2 rows in set (0.00 sec)

mysql
> ALTER TABLE users2 DROP INDEX OK_users2_username;
mysql
> SHOW INDEXES FROM users2\G;
*************************** 1. row ***************************
Table: users2
Non_unique:
1
Key_name: pid
Seq_in_index:
1
Column_name: pid
Collation: A
Cardinality:
0
Sub_part:
NULL
Packed:
NULL
Null: YES
Index_type: BTREE
Comment:
1 row in set (0.00 sec)
MySQL学习笔记-约束以及修改数据表

 

  • 添加外键约束:添加pid列为外键列,provincse数据表中的id为参照列:
mysql> ALTER TABLE users2 ADD FOREIGN KEY (pid) REFERENCES provinces (id);

 

  • 删除外键约束:
MySQL学习笔记-约束以及修改数据表
mysql> SHOW CREATE TABLE users2; 
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users2 | CREATE TABLE `users2` (
`username`
varchar(20) NOT NULL,
`pid`
smallint(5) unsigned DEFAULT NULL,
`id`
smallint(5) unsigned NOT NULL DEFAULT '0',
`age`
tinyint(4) NOT NULL,
KEY `pid` (`pid`),
CONSTRAINT `users2_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `provinces` (`id`)
) ENGINE
=InnoDB DEFAULT CHARSET=utf8 |
+--------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

mysql
> ALTER TABLE users2 DROP FOREIGN KEY users2_ibfk_1;
MySQL学习笔记-约束以及修改数据表

 

  • 添加默认约束:添加age列,设置age列默认15
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users2 ADD age TINYINT NOT NULL;
mysql
> SHOW COLUMNS FROM users2;
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| id | smallint(5) unsigned | NO | PRI | 0 | |
| age | tinyint(4) | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
mysql> ALTER TABLE users2 ALTER age SET DEFAULT 15;
mysql
> SHOW COLUMNS FROM users2;
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| id | smallint(5) unsigned | NO | PRI | 0 | |
| age | tinyint(4) | NO | | 15 | |
+----------+----------------------+------+-----+---------+-------+
MySQL学习笔记-约束以及修改数据表

 

  • 删除默认值:
MySQL学习笔记-约束以及修改数据表
mysql> ALTER TABLE users2 ALTER age DROP DEFAULT;
mysql
> SHOW COLUMNS FROM users2;
+----------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(20) | NO | UNI | NULL | |
| pid | smallint(5) unsigned | YES | MUL | NULL | |
| id | smallint(5) unsigned | NO | PRI | 0 | |
| age | tinyint(4) | NO | | NULL | |
+----------+----------------------+------+-----+---------+-------+
MySQL学习笔记-约束以及修改数据表

 

修改列定义:
ALTER TABLE tbl_name MODIFY [COLUMN] col_name column_definition [FIRST|AFTER col_name]

 

修改列名称(不能随便更改):
ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_dufinition [FIRST|AFTER col_name]

 

修改数据表名字(不能随便更改):
  • 方法一:
ALTER TABLE old_tbl_name RENAME new_tbl_name 

 

  方法二:

RENAME TABLE old_tbl_name TO new_tbl_name [,tbl_name2 TO new_tbl_name2]...