说明
有关表的CHECK约束问题:
在MySQL 8.0.16之前,CREATE TABLE只允许以下有限版本的表约束语法,这些语法被解析和忽略:CHECK
要实现CHECK,离散型数据使用enum,连续性数据使用触发器
表的操作涉及数据类型以及表的约束条件和列的约束
使用数据库
mysql> use stu;Database changed
创建表简单版
mysql> create table student(-> id int(30),-> name varchar(30),-> age int-> );Query OK, 0 rows affected (0.03 sec)
创建表复杂版
mysql> create table student_1(-> id int(20) auto_increment primary key,-> name varchar(10) not null,-> phone varchar(20) comment 'phone',-> address varchar(20) default 'NULL'-> )engine=innodb;Query OK, 0 rows affected (0.02 sec)
查看创建的表
mysql> show create table student_1;+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table |+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| student_1 | CREATE TABLE `student_1` (`id` int(20) NOT NULL AUTO_INCREMENT,`name` varchar(10) NOT NULL,`phone` varchar(20) DEFAULT NULL COMMENT 'phone',`address` varchar(20) DEFAULT 'NULL',PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 |+-----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
查看表结构
mysql> desc students;+-----------+-------------+------+-----+---------+----------------+| Field | Type | Null | Key | Default | Extra |+-----------+-------------+------+-----+---------+----------------+| id | int(20) | NO | PRI | NULL | auto_increment || name | varchar(10) | NO | | NULL | || tel_phone | varchar(13) | YES | | NULL | || address | varchar(20) | YES | | NULL | |+-----------+-------------+------+-----+---------+----------------+4 rows in set (0.00 sec)
修改表名
mysql> alter table student_1 rename to students;Query OK, 0 rows affected (0.01 sec)
修改表中字段
mysql> alter table students change phone tel_phone int(11);Query OK, 0 rows affected (0.03 sec)Records: 0 Duplicates: 0 Warnings: 0mysql> alter table students modify tel_phone varchar(13);Query OK, 0 rows affected (0.04 sec)Records: 0 Duplicates: 0 Warnings: 0
删除表的字段
mysql> alter table students drop gender;Query OK, 0 rows affected (0.04 sec)Records: 0 Duplicates: 0 Warnings: 0
