数据表的基本操作与表的约束

Posted p1035

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据表的基本操作与表的约束相关的知识,希望对你有一定的参考价值。

1、创建数据表
mysql> CREATE TABLE tb_grade
-> (
-> id INT(20),
-> name CHAR(20),
-> grade FLOAT
-> )
-> ;
Query OK, 0 rows affected

为了验证数据表是否创建成功,需要使用SHOW TABLES语句进行查看
mysql> SHOW TABLES;
+------------------+
| Tables_in_itcast |
+------------------+
| tb_grade |
+------------------+
1 row in set

2、使用SHOW CREATE TABLE查看数据表(查看数据表的方法一)mysql> SHOW CREATE TABLE tb_grade;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_grade | CREATE TABLE `tb_grade` (
`id` int(11) DEFAULT NULL,
`name` varchar(20) DEFAULT NULL,
`grade` float DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=gbk |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set

使用DESCRIBE语句查看数据表(方法二)(可以缩写为DESC)
mysql> DESCRIBE tb_grade;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
3 rows in set

NULL:表示该列是否可以存储NULL值。
Key:表示该列是否已经编制索引。
Default:表示该列是否有默认值。
Extra:表示获取到的与给定列相关的附加信息。

3、修改表名(ALTER TABLE 旧表名 RENAME [TO] 新表名;)
mysql> ALTER TABLE tb_grade RENAME TO grade;
Query OK, 0 rows affected

为了检测表名是否修改正确,再次使用SHOW TABLES语句查看数据库中的所有表
mysql> SHOW TABLES;
+------------------+
| Tables_in_itcast |
+------------------+
| grade |
+------------------+
1 row in set

4、修改字段名(ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新数据类型;)mysql> ALTER TABLE grade CHANGE name username VARCHAR(20);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set


5、修改字段的数据类型(ALTER TABLE 表名 MODIFY 字段名 数据类型; )mysql> ALTER TABLE grade MODIFY id INT(20);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

6、添加字段(ALTER TABLE 表名 ADD新字段名 数据类型 [约束条件][FIRST|AFTER 已存在字段名] )
mysql> ALTER TABLE grade ADD age INT(10);
Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
| age | int(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
4 rows in set

7、删除字段(ALTER TABLE 表名 DROP 字段名; )
mysql> ALTER TABLE grade DROP age;Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0

查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(20) | YES | | NULL | |
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

8、修改字段的排列位置(ALTER TABLE表名 MODIFY 字段名1 数据类型 FIRST|AFTER 字段名2 )

(1)
mysql> ALTER TABLE grade MODIFY username VARCHAR(20) FIRST;Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| id | int(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

(2)
mysql> ALTER TABLE grade MODIFY id INT(20) AFTER grade;Query OK, 0 rows affected
Records: 0 Duplicates: 0 Warnings: 0
查看后:
mysql> DESC grade;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| username | varchar(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
| id | int(20) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
3 rows in set

9、删除数据表(DROP TABLE 表名;)
mysql> DROP TABLE grade;
Query OK, 0 rows affected


10、表的约束
约束条件 说明
PRIMARY KEY 主键约束,用于唯一标识对应的记录
FOREIGN KEY 外键约束
NOT NULL 非空约束
UNIQUE 唯一性约束
DEFAULT 默认值约束,用于设置字段的默认值


多字段主键指的是多个字段组合而成的主键(PRIMARY KEY (字段名1,字段名2,……字段名n) )
mysql> CREATE TABLE exam_1
->
-> (
-> stu_id INT,
-> course_id INT,
-> grade FLOAT,
-> PRIMARY KEY(stu_id,course_id)
-> );
Query OK, 0 rows affected

mysql> DESC exam_1;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| stu_id | int(11) | NO | PRI | 0 | |
| course_id | int(11) | NO | PRI | 0 | |
| grade | float | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
3 rows in set

使用AUTO_INCREMENT设置表字段值自动增加(字段名 数据类型 AUTO_INCREMENT; )
mysql> CREATE TABLE exam_2
-> (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> stu_id INT UNIQUE,
-> grade FLOAT DEFAULT 0
-> );
Query OK, 0 rows affected


mysql> DESC exam_2;
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(11) | YES | UNI | NULL | |
| grade | float | YES | | 0 | |
+--------+---------+------+-----+---------+----------------+
3 rows in set

mysql> CREATE TABLE exam_3
-> (
-> id INT PRIMARY KEY AUTO_INCREMENT,
-> stu_id INT UNIQUE,
-> grade FLOAT
-> );
Query OK, 0 rows affected

mysql> DESC exam_3;
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(11) | YES | UNI | NULL | |
| grade | float | YES | | NULL | |
+--------+---------+------+-----+---------+----------------+
3 rows in set

以上是关于数据表的基本操作与表的约束的主要内容,如果未能解决你的问题,请参考以下文章

SQL语句汇总——数据库与表的操作以及创建约束

MySQL-表的删除与表数据操作

MySQL-表的删除与表数据操作

[MySQL]数据库的约束与表的设计

python 41 数据类型与表的完整性约束

SQL表与表之间建立外键约束之后,怎么建立连级更新和删除?