概念
在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据存储的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的。每一行代表一条唯一的记录,每一列代表记录中的一个域。
1、创建数据表
说明:数据表属于数据库,在创建数据表之前,应该使用语句“USE <数据库实例名>”指定操作的数据库实例。
USE cuixiaozhao;#使用数据库实例-cuixiaozhao;
1 CREATE TABLE <表名>( 2 字段名1,数据类型 [列级别的约束条件] [默认值], 3 字段名2,数据类型 [列级别的约束条件] [默认值], 4 字段名3,数据类型 [列级别的约束条件] [默认值], 5 ...... 6 [表级别的约束条件] 7 )ENGINE=InnoDB DEFAULT CHARSET=utf8;
举例说明
CREATE TABLE empoyee(
id INT(11),
name VARCAHR(20),
deptId INT(11),
salary FLOAT
)ENGINE=InnoDB DEFAULT CHARSET = utf8;
1 主键,又称主码,是表中一列或多列的组合。数据唯一、不允许为空。可结合外键来定义不同数据表之间的关系并且加快数据库查询速度。分为单字段主键和多字段联合主键。 2 create table tb1( 3 nid int not null auto_increment primary key, 4 num int null 5 )ENGINE=InnoDB DEFAULT CHARSET=utf8; 6 或 7 create table tb2( 8 nid int not null, 9 num int not null, 10 primary key(nid,num) 11 )ENGINE=InnoDB DEFAULT CHARSET=utf8;
1 外键,用来在两个表之间建立连接关系,可以是一列或者多列。一张表可以有一个或多个外键,作用保持数据的一致性和完整性。 2 主表(父表),即主键所在的表; 3 从表(子表),外键所在的表; 4 CREATE TABLE color ( 5 nid INT NOT NULL PRIMARY KEY, 6 NAME CHAR (20) NOT NULL 7 ) ENGINE = INNODB DEFAULT CHARSET = utf8; 8 9 CREATE TABLE fruit ( 10 nid INT NOT NULL PRIMARY KEY, 11 smt CHAR (32) NULL, 12 color_id INT NOT NULL, 13 CONSTRAINT fk_cc FOREIGN KEY (color_id) REFERENCES color (nid) 14 ) ENGINE = INNODB DEFAULT CHARSET = utf8;
1 非空约束,Not Null Constraint,指字段的值不能为空。 2 1、null #可为空 3 2、not null#不可为空 4 语法: 5 字段名 数据类型 NOT NULL
1 唯一性约束,Unique Constraint,要求该列唯一,可以为空,但只能出现一个空值。唯一约束可以确保一列或者几列不出现重复值。 2 语法: 3 字段名 数据类型 UNIQUE
1 默认约束,Default Constraint,指定某列的默认值。 2 语法: 3 字段名 数据类型 DEFAULT 默认值 4 如果插入一条新纪录时,没有为该字段赋值,那么系统会自动为这个字段赋值为设定的默认值。
1 在数据库应用中,希望在每次插入新纪录时,系统自动生成字段的主键值。可通过为表的主键增加AUTO_INCREMENT 关键字来实现。mysql中默认初始值为1,一条记录自动加1,一个表只能有一个字段使用AUTO_INCREMENT 约束,且该字段必须为主键的一部分。AUTO_INCREMENT 的字段类型可以是任何整数类型(TINYINT\SMALLINT\INT\BIGINT等) 2 语法: 3 字段名 数据类型 AUTO_INCREMENT
注意:外键约束PREIGN KEY 不能跨MySQL存储引擎使用!因为外键约束是用来保证数据的参照完整性,如果之间需要关联外键,却指定了不同的存储引擎,这些表之间是不能创建外键约束的。
2、查看数据表结构
- 查看表基本结构语句DESCRIBE DESCRIBE表名或者简写为DESC 表名;
1 ysql> DESC tb_emp1; 2 +--------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +--------+-------------+------+-----+---------+-------+ 5 | id | int(11) | NO | PRI | NULL | | 6 | name | varchar(25) | YES | | NULL | | 7 | deptId | int(11) | YES | | NULL | | 8 | salary | float | YES | | NULL | | 9 +--------+-------------+------+-----+---------+-------+ 10 4 rows in set (0.01 sec) 11 12 mysql> DESCRIBE tb_emp1; 13 +--------+-------------+------+-----+---------+-------+ 14 | Field | Type | Null | Key | Default | Extra | 15 +--------+-------------+------+-----+---------+-------+ 16 | id | int(11) | NO | PRI | NULL | | 17 | name | varchar(25) | YES | | NULL | | 18 | deptId | int(11) | YES | | NULL | | 19 | salary | float | YES | | NULL | | 20 +--------+-------------+------+-----+---------+-------+ 21 4 rows in set (0.00 sec) 22 23 mysql>
- 查看详细结构语句SHOW CREATE TABLE SHOW CREATE TABLE 表名;
1 mysql> SHOW CREATE TABLE tb_emp1; 2 +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 3 | Table | Create Table | 4 +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 5 | tb_emp1 | CREATE TABLE `tb_emp1` ( 6 `id` int(11) NOT NULL, 7 `name` varchar(25) DEFAULT NULL, 8 `deptId` int(11) DEFAULT NULL, 9 `salary` float DEFAULT NULL, 10 PRIMARY KEY (`id`) 11 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 12 +---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 13 1 row in set (0.00 sec) 14 15 mysql>
3、删除数据表
DROP TABLE 表名
- 删除没有被关联的表
语法:DROP TABLE [IF EXISTS] 表1,表2,表3,表n;#加上IF EXISTS 参数,如果表不存在,SQL语句可以顺利执行,但是会发生警告(warnig)
- 删除被其他表关联的主表
1、可以先删除关联的子表,再删除父表,但同时删除了两张表;
2、删除关联表的外键约束,在删除父表,保留了子表;
4、清空表
- DELETE FROM 表名;
- TRUNCATE TABLE 表名;
5、修改表
修改表指的是修改数据库中已存在的数据表结构。MySQL中使用ALTER TABLE 语句修改表。包含:需改表名、字段名、字段的数据类型、增加和删除字段、修改字段的排列位置、更改表的存储引擎、删除表的外键约束!
1、修改表名
ALTER TABLE <旧表名> RENAME [TO] <新表名>;#其中,TO为可选参数;
1 mysql> SHOW DATABASES; 2 +--------------------+ 3 | Database | 4 +--------------------+ 5 | information_schema | 6 | cuixiaozhao | 7 | mysql | 8 | performance_schema | 9 | sys | 10 +--------------------+ 11 5 rows in set (0.00 sec) 12 13 mysql> USE cuixiaozhao; 14 Reading table information for completion of table and column names 15 You can turn off this feature to get a quicker startup with -A 16 17 Database changed 18 mysql> SHOW TABLES; 19 +-----------------------+ 20 | Tables_in_cuixiaozhao | 21 +-----------------------+ 22 | tb_cxz | 23 +-----------------------+ 24 1 row in set (0.00 sec) 25 26 mysql> DESC tb_cxz; 27 +--------+-------------+------+-----+---------+-------+ 28 | Field | Type | Null | Key | Default | Extra | 29 +--------+-------------+------+-----+---------+-------+ 30 | id | int(11) | NO | PRI | NULL | | 31 | name | varchar(25) | YES | | NULL | | 32 | deptId | int(11) | YES | | NULL | | 33 | salary | float | YES | | NULL | | 34 +--------+-------------+------+-----+---------+-------+ 35 4 rows in set (0.00 sec) 36 mysql> SHOW CREATE TABLE tb_cxz; 37 +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 38 | Table | Create Table | 39 +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 40 | tb_cxz | CREATE TABLE `tb_cxz` ( 41 `id` int(11) NOT NULL, 42 `name` varchar(25) DEFAULT NULL, 43 `deptId` int(11) DEFAULT NULL, 44 `salary` float DEFAULT NULL, 45 PRIMARY KEY (`id`) 46 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 47 +--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 48 1 row in set (0.00 sec) 49 50 mysql> ALTER TABLE tb_cxz RENAME TO tb_cxs; 51 Query OK, 0 rows affected (0.00 sec) 52 53 mysql> SHOW TABLES; 54 +-----------------------+ 55 | Tables_in_cuixiaozhao | 56 +-----------------------+ 57 | tb_cxs | 58 +-----------------------+ 59 1 row in set (0.00 sec) 60 61 mysql>
注意:修改表名并不修改表的结构;
2、修改字段的数据类型
ALTER TABLE <表名> MODIFY <字段名> <数据类型>;
1 mysql> DESC tb_cxs; 2 +--------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +--------+-------------+------+-----+---------+-------+ 5 | id | int(11) | NO | PRI | NULL | | 6 | name | varchar(25) | YES | | NULL | | 7 | deptId | int(11) | YES | | NULL | | 8 | salary | float | YES | | NULL | | 9 +--------+-------------+------+-----+---------+-------+ 10 4 rows in set (0.00 sec) 11 12 mysql> ALTER TABLE tb_cxs MODIFY name VARCHAR(30); 13 Query OK, 0 rows affected (0.00 sec) 14 Records: 0 Duplicates: 0 Warnings: 0 15 16 mysql> DESC tb_cxs; 17 +--------+-------------+------+-----+---------+-------+ 18 | Field | Type | Null | Key | Default | Extra | 19 +--------+-------------+------+-----+---------+-------+ 20 | id | int(11) | NO | PRI | NULL | | 21 | name | varchar(30) | YES | | NULL | | 22 | deptId | int(11) | YES | | NULL | | 23 | salary | float | YES | | NULL | | 24 +--------+-------------+------+-----+---------+-------+ 25 4 rows in set (0.00 sec) 26 27 mysql>
3、修改字段名
ALTER TABLE <表名> CHANGE <旧字段名> <新字段名> <新数据类型>;
1 修改字段名,数据类型保持不变; 2 mysql> DESC tb_cxs; 3 +--------+-------------+------+-----+---------+-------+ 4 | Field | Type | Null | Key | Default | Extra | 5 +--------+-------------+------+-----+---------+-------+ 6 | id | int(11) | NO | PRI | NULL | | 7 | name | varchar(30) | YES | | NULL | | 8 | deptId | int(11) | YES | | NULL | | 9 | salary | float | YES | | NULL | | 10 +--------+-------------+------+-----+---------+-------+ 11 4 rows in set (0.00 sec) 12 13 mysql> ALTER TABLE tb_cxs CHANGE salary pay float; 14 Query OK, 0 rows affected (0.01 sec) 15 Records: 0 Duplicates: 0 Warnings: 0 16 17 mysql> DESC tb_cxs; 18 +--------+-------------+------+-----+---------+-------+ 19 | Field | Type | Null | Key | Default | Extra | 20 +--------+-------------+------+-----+---------+-------+ 21 | id | int(11) | NO | PRI | NULL | | 22 | name | varchar(30) | YES | | NULL | | 23 | deptId | int(11) | YES | | NULL | | 24 | pay | float | YES | | NULL | | 25 +--------+-------------+------+-----+---------+-------+ 26 4 rows in set (0.00 sec) 27 28 即修改字段名称,同时修改数据类型; 29 mysql> DESC tb_cxs; 30 +--------+-------------+------+-----+---------+-------+ 31 | Field | Type | Null | Key | Default | Extra | 32 +--------+-------------+------+-----+---------+-------+ 33 | id | int(11) | NO | PRI | NULL | | 34 | name | varchar(30) | YES | | NULL | | 35 | deptId | int(11) | YES | | NULL | | 36 | salary | float | YES | | NULL | | 37 +--------+-------------+------+-----+---------+-------+ 38 4 rows in set (0.00 sec) 39 40 mysql> ALTER TABLE tb_cxs CHANGE salary pay INT(10); 41 Query OK, 0 rows affected (0.04 sec) 42 Records: 0 Duplicates: 0 Warnings: 0 43 44 mysql>
小结:CHANGE也可以只修改数据类型,实现和MODIFY同样的效果,方法是:将SQL语句中的“新字段名”和“旧字段名”设置为相同的名称,只改变“数据类型”!
但是,由于不同的数据在机器中存储的方式及长度并不相同,修改数据类型可能会影响到数据表中已有的数据记录,因此,当数据表中已经存在数据时,不要轻易修改数据类型;
4、添加字段
ALTER TABLE <表名> <新字段名> <数据类型> [约束条件] [ FIRST | AFTER 已存在的字段名 ];#FIRST 与AFTER为可选参数,如果没有,默认将新字段添加至最后列;
- 添加无完整性约束条件的字段 ALTER TABLE tb_cxs ADD column1 VARCHAR(20);
- 添加有完整性约束条件的字段 ALTER TABLE tb_cxs ADD column1 VARCHAR(20) NOT NULL;
- 在表中的第一列添加字段 ALTER TABLE tb_cxs ADD column1 VARCHAR(20) NOT NULL FIRST;
- 在表中的最后一列添加字段 ALTER TABLE tb_cxs ADD column1 VARCHAR(20) NOT NULL;
- 在表的指定列之后添加字段(即为在表的指定列之前添加字段,是一个相对概念)ALTER TABLE tb_cxs ADD column1 VARCHAR(20) NOT NULL AFTER name;
1 mysql> DESC tb_cxs; 2 +--------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +--------+-------------+------+-----+---------+-------+ 5 | id | int(11) | NO | PRI | NULL | | 6 | name | varchar(30) | YES | | NULL | | 7 | deptId | int(11) | YES | | NULL | | 8 | pay | int(10) | YES | | NULL | | 9 +--------+-------------+------+-----+---------+-------+ 10 4 rows in set (0.00 sec) 11 12 mysql> ALTER TABLE tb_cxs ADD column1 VARCHAR(20) NOT NULL; 13 Query OK, 0 rows affected (0.03 sec) 14 Records: 0 Duplicates: 0 Warnings: 0 15 16 mysql> DESC tb_cxs; 17 +---------+-------------+------+-----+---------+-------+ 18 | Field | Type | Null | Key | Default | Extra | 19 +---------+-------------+------+-----+---------+-------+ 20 | id | int(11) | NO | PRI | NULL | | 21 | name | varchar(30) | YES | | NULL | | 22 | deptId | int(11) | YES | | NULL | | 23 | pay | int(10) | YES | | NULL | | 24 | column1 | varchar(20) | NO | | NULL | | 25 +---------+-------------+------+-----+---------+-------+ 26 5 rows in set (0.00 sec) 27 mysql> ALTER TABLE tb_cxs ADD column0 INT(11) FIRST; 28 Query OK, 0 rows affected (0.02 sec) 29 Records: 0 Duplicates: 0 Warnings: 0 30 31 mysql> DESC tb_cxs; 32 +---------+-------------+------+-----+---------+-------+ 33 | Field | Type | Null | Key | Default | Extra | 34 +---------+-------------+------+-----+---------+-------+ 35 | column0 | int(11) | YES | | NULL | | 36 | id | int(11) | NO | PRI | NULL | | 37 | name | varchar(30) | YES | | NULL | | 38 | deptId | int(11) | YES | | NULL | | 39 | pay | int(10) | YES | | NULL | | 40 | column1 | varchar(20) | NO | | NULL | | 41 +---------+-------------+------+-----+---------+-------+ 42 6 rows in set (0.00 sec) 43 44 mysql> ALTER TABLE tb_cxs ADD column2 INT(11) AFTER name; 45 Query OK, 0 rows affected (0.03 sec) 46 Records: 0 Duplicates: 0 Warnings: 0 47 48 mysql> DESC tb_cxs; 49 +---------+-------------+------+-----+---------+-------+ 50 | Field | Type | Null | Key | Default | Extra | 51 +---------+-------------+------+-----+---------+-------+ 52 | column0 | int(11) | YES | | NULL | | 53 | id | int(11) | NO | PRI | NULL | | 54 | name | varchar(30) | YES | | NULL | | 55 | column2 | int(11) | YES | | NULL | | 56 | deptId | int(11) | YES | | NULL | | 57 | pay | int(10) | YES | | NULL | | 58 | column1 | varchar(20) | NO | | NULL | | 59 +---------+-------------+------+-----+---------+-------+ 60 7 rows in set (0.00 sec)
5、删除字段
删除字段是将数据表中的某个字段从表中移除的过程。
ALTER TABLE <表名> DROP <字段名>;
1 mysql> DESC tb_cxs; 2 +---------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +---------+-------------+------+-----+---------+-------+ 5 | column0 | int(11) | YES | | NULL | | 6 | id | int(11) | NO | PRI | NULL | | 7 | name | varchar(30) | YES | | NULL | | 8 | column2 | int(11) | YES | | NULL | | 9 | deptId | int(11) | YES | | NULL | | 10 | pay | int(10) | YES | | NULL | | 11 | column1 | varchar(20) | NO | | NULL | | 12 +---------+-------------+------+-----+---------+-------+ 13 7 rows in set (0.00 sec) 14 mysql> ALTER TABLE tb_cxs DROP column1; 15 Query OK, 0 rows affected (0.03 sec) 16 Records: 0 Duplicates: 0 Warnings: 0 17 18 mysql> DESC tb_cxs; 19 +---------+-------------+------+-----+---------+-------+ 20 | Field | Type | Null | Key | Default | Extra | 21 +---------+-------------+------+-----+---------+-------+ 22 | column0 | int(11) | YES | | NULL | | 23 | id | int(11) | NO | PRI | NULL | | 24 | name | varchar(30) | YES | | NULL | | 25 | column2 | int(11) | YES | | NULL | | 26 | deptId | int(11) | YES | | NULL | | 27 | pay | int(10) | YES | | NULL | | 28 +---------+-------------+------+-----+---------+-------+ 29 6 rows in set (0.00 sec) 30 31 mysql>
6、修改字段的排列位置
修改字段的相对排列位置。
ALTER TABLE <表名> MODIFY <字段1> <数据类型> FIRST | AFTER < 字段2>;
- 修改字段为表的第一个字段
- 修改字段到表的指定列之后
1 mysql> DESC tb_cxs; 2 +---------+-------------+------+-----+---------+-------+ 3 | Field | Type | Null | Key | Default | Extra | 4 +---------+-------------+------+-----+---------+-------+ 5 | column0 | int(11) | YES | | NULL | | 6 | id | int(11) | NO | PRI | NULL | | 7 | name | varchar(30) | YES | | NULL | | 8 | column2 | int(11) | YES | | NULL | | 9 | deptId | int(11) | YES | | NULL | | 10 | pay | int(10) | YES | | NULL | | 11 +---------+-------------+------+-----+---------+-------+ 12 6 rows in set (0.00 sec) 13 mysql> ALTER TABLE tb_cxs MODIFY pay INT(10) FIRST; 14 Query OK, 0 rows affected (0.03 sec) 15 Records: 0 Duplicates: 0 Warnings: 0 16 17 mysql> DESC tb_cxs; 18 +---------+-------------+------+-----+---------+-------+ 19 | Field | Type | Null | Key | Default | Extra | 20 +---------+-------------+------+-----+---------+-------+ 21 | pay | int(10) | YES | | NULL | | 22 | column0 | int(11) | YES | | NULL | | 23 | id | int(11) | NO | PRI | NULL | | 24 | name | varchar(30) | YES | | NULL | | 25 | column2 | int(11) | YES | | NULL | | 26 | deptId | int(11) | YES | | NULL | | 27 +---------+-------------+------+-----+---------+-------+ 28 6 rows in set (0.00 sec) 29 30 mysql> ALTER TABLE tb_cxs MODIFY pay INT(10) AFTER name; 31 Query OK, 0 rows affected (0.03 sec) 32 Records: 0 Duplicates: 0 Warnings: 0 33 34 mysql> DESC tb_cxs; 35 +---------+-------------+------+-----+---------+-------+ 36 | Field | Type | Null | Key | Default | Extra | 37 +---------+-------------+------+-----+---------+-------+ 38 | column0 | int(11) | YES | | NULL | | 39 | id | int(11) | NO | PRI | NULL | | 40 | name | varchar(30) | YES | | NULL | | 41 | pay | int(10) | YES | | NULL | | 42 | column2 | int(11) | YES | | NULL | | 43 | deptId | int(11) | YES | | NULL | | 44 +---------+-------------+------+-----+---------+-------+ 45 6 rows in set (0.00 sec) 46 47 mysql>
7、更改表的存储引擎
存储引擎是MySQL中的数据存储在文件或者内存中时采用的不同技术实现;其中,主要的存储引擎有:MyISAM、InnoDB、MEMORY(HEAP)、BDB、FEDERATED等。
ALTER TABLE <表名> ENGINE=<更改后的存储引擎名称>;
1 mysql> SHOW CREATE TABLE tb_cxs; 2 +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 3 | Table | Create Table | 4 +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 5 | tb_cxs | CREATE TABLE `tb_cxs` ( 6 `column0` int(11) DEFAULT NULL, 7 `id` int(11) NOT NULL, 8 `name` varchar(30) DEFAULT NULL, 9 `pay` int(10) DEFAULT NULL, 10 `column2` int(11) DEFAULT NULL, 11 `deptId` int(11) DEFAULT NULL, 12 PRIMARY KEY (`id`) 13 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 14 +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 15 1 row in set (0.00 sec) 16 17 mysql> ALTER TABLE tb_cxs ENGINE=MyISAM; 18 Query OK, 0 rows affected (0.01 sec) 19 Records: 0 Duplicates: 0 Warnings: 0 20 21 mysql> SHOW CREATE TABLE tb_cxs; 22 +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 23 | Table | Create Table | 24 +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 25 | tb_cxs | CREATE TABLE `tb_cxs` ( 26 `column0` int(11) DEFAULT NULL, 27 `id` int(11) NOT NULL, 28 `name` varchar(30) DEFAULT NULL, 29 `pay` int(10) DEFAULT NULL, 30 `column2` int(11) DEFAULT NULL, 31 `deptId` int(11) DEFAULT NULL, 32 PRIMARY KEY (`id`) 33 ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | 34 +--------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 35 1 row in set (0.00 sec) 36 37 mysql>
8、删除表的外键约束
对于数据库表中定义的外键,不在需要时可以将其删除。外键一旦删除,将解除主从表之间的关联关系。
ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>;
1 mysql> SHOW CREATE TABLE tb_emp9; 2 +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 3 | Table | Create Table | 4 +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 5 | tb_emp9 | CREATE TABLE `tb_emp9` ( 6 `id` int(11) NOT NULL, 7 `name` varchar(25) DEFAULT NULL, 8 `deptId` int(11) DEFAULT NULL, 9 `salary` float DEFAULT NULL, 10 PRIMARY KEY (`id`), 11 KEY `fk_emp_dept` (`deptId`), 12 CONSTRAINT `fk_emp_dept` FOREIGN KEY (`deptId`) REFERENCES `tb_cxs` (`id`) 13 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 14 +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 15 1 row in set (0.00 sec) 16 17 mysql> ALTER TABLE tb_emp9 DROP FOREIGN KEY fk_emp_dept; 18 Query OK, 0 rows affected (0.00 sec) 19 Records: 0 Duplicates: 0 Warnings: 0 20 21 mysql> SHOW CREATE TABLE tb_emp9; 22 +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 23 | Table | Create Table | 24 +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 25 | tb_emp9 | CREATE TABLE `tb_emp9` ( 26 `id` int(11) NOT NULL, 27 `name` varchar(25) DEFAULT NULL, 28 `deptId` int(11) DEFAULT NULL, 29 `salary` float DEFAULT NULL, 30 PRIMARY KEY (`id`), 31 KEY `fk_emp_dept` (`deptId`) 32 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 | 33 +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 34 1 row in set (0.00 sec) 35 36 mysql>
9、修改默认值
修改默认值:ALTER TABLE <表名> ALTER <字段名> SET DEFAULT 1000;
删除默认值:ALTER TABLE <表名> ALTER <字段名> DROP DEFAULT;
10、增加、删除主键
添加主键:
ALTER TABLE <表名>
ADD PRIMARY KEY(字段名);
删除主键:
ALTER TABLE <表名>
ADD PRIMARY KEY;#此处不需要指定字段名,因为主键默认只有一个;
ALTER TABLE <表名>
MODIFY <字段名> <字段类型> DROP PRIMARY KEY;