第三章 约束以及修改数据表

一、约束

(一)约束

  1. 约束保证数据的完整性和一致性。
  2. 约束分为表级约束和列级约束。
  3. 约束类型包括
  • NOT NULL(非空约束)
  • PRIMARY KEY(主键约束)
  • UNIQUE KEY(唯一约束)
  • DEFAULT(默认约束)
  • FOREIGN KEY(外键约束)

(二)FOREIGN KEY

  • 保证数据的一致性,完整性。
  • 实现一对一或一对多关系。

(三)外键约束的要求

  1. 父表和子表必须使用相同的存储引擎,而且禁止使用临时表。
  2. 数据表的存储引擎只能为InnoDB。
  3. 外键列和参照列必须具有相似的数据类型。其中数字的长度或是否有符号位必须相同;而字符的长度则可以不同。
  4. 外键列和参照列必须创建索引。如果外键列不存在索引的话,MySQL将自动创建索引。

example:

CREATE TABLE provinces (
  id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  pname VARCHAR(20) NOT NULL
);

CREATE TABLE users (
  id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(20) NOT NULL,
  pid SMALLINT UNSIGNED,
  FOREIGN KEY (pid) REFERENCES provinces (id)
);

查看索引

SHOW INDEX FROM provinces;
SHOW INDEX FROM provinces\G; --网格

(四)外键约束的参照操作

  1. CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行
  2. SET NULL:从父表删除或更新行,并设置子表中的外键列为NULL。如果使用该选项,必须保证子表没有指定NOT NULL
  3. RESTRICT:拒绝对父表进行更新操作
  4. NO ACTION:标准SQL关键字,在MySQL中与RESTRICT相同

example:

CREATE TABLE users1 (
  id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
  username VARCHAR(20) NOT NULL,
  pid SMALLINT UNSIGNED,
  FOREIGN KEY (pid) REFERENCES provinces (id) ON DELETE CASCADE
);
INSERT provinces (pname) VALUES('A');
INSERT provinces (pname) VALUES('B');
INSERT provinces (pname) VALUES('C');
/*Result
+----+-------+
| id | pname |
+----+-------+
|  1 | A     |
|  2 | B     |
|  3 | C     |
+----+-------+
*/
INSERT users1(username, pid) VALUES('Tom', 3);
INSERT users1(username, pid) VALUES('John', 7);
INSERT users1(username, pid) VALUES('John', 3);
INSERT users1(username, pid) VALUES('Rose', 7);
/*Result
+----+----------+------+
| id | username | pid  |
+----+----------+------+
|  1 | Tom      |    3 |
|  3 | John     |    1 |
|  4 | Rose     |    3 |
+----+----------+------+
*/
DELETE FROM provinces WHERE id = 3;
SELECT * FROM provinces;
/*Result
+----+-------+
| id | pname |
+----+-------+
|  1 | A     |
|  2 | B     |
+----+-------+
*/
SELECT * FROM users1;
/*Result
+----+----------+------+
| id | username | pid  |
+----+----------+------+
|  3 | John     |    1 |
+----+----------+------+
*/

(五)约束分为表级约束和列级约束

  • 对一个数据列建立的约束,称为列级约束。
  • 对多个数据列建立的约束,称为表级约束。
  • 列级约束既可以在列定义时表明,也可以在列定义后声明。表级约束只能在列定义后声明。

(六)修改数据表

添加单列

ALTER TABLE tbl_name ADD [COLUMN] col_name column_definition [FIRST| AFTER col_name]

example

/*Start
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
*/
ALTER TABLE users1 ADD age TINYINT UNSIGNED NOT NULL DEFAULT 10;
ALTER TABLE users1 ADD password VARCHAR(32) NOT NULL AFTER username;
ALTER TABLE users1 ADD truename VARCHAR(20) NOT NULL FIRST;
/*Result
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| truename | varchar(20)          | NO   |     | NULL    |                |
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   |     | NULL    |                |
| password | varchar(32)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
| age      | tinyint(3) unsigned  | NO   |     | 10      |                |
+----------+----------------------+------+-----+---------+----------------+
*/

添加多列

ALTER TABLE tbl_name ADD [COLUMN] (col_name column_definition,...)

删除列

ALTER TABLE tbl_name DROP [COLUMN] col_name;

example:

ALTER TABLE users1 DROP truename; --删除一列
ALTER TABLE users1 DROP password, DROP age;--删除多列
/*Result:
+----------+----------------------+------+-----+---------+----------------+
| Field    | Type                 | Null | Key | Default | Extra          |
+----------+----------------------+------+-----+---------+----------------+
| id       | smallint(5) unsigned | NO   | PRI | NULL    | auto_increment |
| username | varchar(20)          | NO   |     | NULL    |                |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |                |
+----------+----------------------+------+-----+---------+----------------+
*/

添加主键约束

ALTER TABLE tbl_name ADD [CONSTRAINT[symbol]] PRIMARY KEY [index_type](index_col_name,...)

example:

CREATE TABLE users2 (
  username VARCHAR(10) NOT NULL,
  pid SMALLINT UNSIGNED
);
ALTER TABLE users2 ADD id SMALLINT UNSIGNED;
ALTER TABLE users2 ADD CONSTRAINT PK_users2_id PRIMARY KEY(id);
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   |     | NULL    |       |
| pid      | smallint(5) unsigned | YES  |     | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

删除主键约束

ALTER TABLE tbl_name DROP PRIMARY KEY;

example:

/*
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   | UNI | NULL    |       |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/
ALTER TABLE users2 DROP PRIMARY KEY;
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   | PRI | NULL    |       |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id       | smallint(5) unsigned | NO   |     | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

添加唯一约束

ALTER TABLE tbl_name ADD [CONSTRAINT[symbol]] UNIQUE [INDEX|KEY] [index_name] [index_type] (index_col_name, ...)

example:

ALTER TABLE users2 ADD UNIQUE (username);
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   | UNI | NULL    |       |
| pid      | smallint(5) unsigned | YES  |     | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

删除唯一约束

ALTER TABLE tbl_name DROP {INDEX| KEY} index_name;

example:

SHOW INDEXES FROM users2;
ALTER TABLE users2 DROP INDEX username;
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   |     | NULL    |       |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id       | smallint(5) unsigned | NO   |     | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

添加外键约束

ALTER TABLE tbl_name ADD [CONSTRAINT[symbol]] FOREIGN KEY [index_name](index_col_name, ...)reference_definition

example:

ALTER TABLE users2 ADD FOREIGN KEY (pid) REFERENCES provinces (id);
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   | UNI | NULL    |       |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

删除外键约束

ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;

example

SHOW CREATE user2;
ALTER TABLE users2 DROP FOREIGN KEY users2_ibfk_1;

添加/删除默认约束

ALTER TABLE tbl_name ALTER [COLUMN] col_name {SET DEFAULT literal| DROP DEFAULT}

example:

ALTER TABLE users2 ADD age TINYINT UNSIGNED NOT NULL;
ALTER TABLE users2 ALTER age SET DEFAULT 15; --添加
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   | UNI | NULL    |       |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | 15      |       |
+----------+----------------------+------+-----+---------+-------+
*/
ALTER TABLE users2 ALTER age DROP DEFAULT; --删除
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| username | varchar(10)          | NO   | UNI | NULL    |       |
| pid      | smallint(5) unsigned | YES  | MUL | NULL    |       |
| id       | smallint(5) unsigned | NO   | PRI | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

修改列定义

ALTER TABLE tbl_name MODIFY[COLUMN] col_name column_definition [FIRST| AFTER col_name]
ALTER TABLE users2 MODIFY id SMALLINT NOT NULL FIRST;
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id       | smallint(6)          | NO   |     | NULL    |       |
| username | varchar(10)          | NO   |     | NULL    |       |
| pid      | smallint(5) unsigned | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/
ALTER TABLE users2 MODIFY id TINYINT NOT NULL;
/*Result
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| id       | tinyint(4)           | NO   |     | NULL    |       |
| username | varchar(10)          | NO   |     | NULL    |       |
| pid      | smallint(5) unsigned | YES  |     | NULL    |       |
| age      | tinyint(3) unsigned  | NO   |     | NULL    |       |
+----------+----------------------+------+-----+---------+-------+
*/

修改列名称

ALTER TABLE tbl_name CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST| AFTER col_name]

example:

ALTER TABLE users2 CHANGE pid p_id TINYINT UNSIGNED NOT NULL;
/*Result
+----------+---------------------+------+-----+---------+-------+
| Field    | Type                | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+-------+
| id       | tinyint(4)          | NO   |     | NULL    |       |
| username | varchar(10)         | NO   |     | NULL    |       |
| p_id     | tinyint(3) unsigned | NO   |     | NULL    |       |
| age      | tinyint(3) unsigned | NO   |     | NULL    |       |
+----------+---------------------+------+-----+---------+-------+
*/

数据表更名

  1. 方法一
ALTER TABLE tbl_name RENAME [TO|AS] new_tbl_name
  1. 方法二
RENAME TABLE tbl_name TO new_tbl_name [, tbl_name2 TO new_tbl_name2]...

example:

ALTER TABLE users2 RENAME users3;
RENAME TABLE users3 TO users2;

你可能感兴趣的:(第三章 约束以及修改数据表)