码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-10-25 23:35:08      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:表的基本操作   query   修改字段   操作   修改表   增加   warnings   prim   char   

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

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

标签:表的基本操作   query   修改字段   操作   修改表   增加   warnings   prim   char   

原文地址:https://www.cnblogs.com/p1035/p/11741243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!