04_MySQL数据表的基本操作

大纲:
1.创建数据表
1.1创建表的语法形式
1.2主键约束
1.3外键约束
1.4非空约束
1.5 唯一性约束
1.6默认约束
1.7设置表的属性值自动增加
2.查看数据表结构
2.1查看表基本结构语句
2.2查看详细表结构
3.修改数据表
3.1 修改表名
3.2修改字段的数据类型
3.3修改字段名
3.4添加字段
3.5删除字段
3.6 修改字段的排序位置
3.7更改表存储引擎
3.8删除表的外键约束
4.删除数据表
4.1 删除没有被关联的表
4.2 删除被其他表关联的主表

1.创建数据表

创建数据表的过程是规定数据列的属性的过程,同时也是实施数据完整性约束的过程。

1.1创建表的语法形式

数据表属于数据库,在创建数据表之前,应该使用语句use <数据库名>指定操作是从那个数据库中进行,没有选择数据库,会报错。

语法:
CREATE TABLE <表名> ( 字段名1, 数据类型 [列级别约束条件] [默认值], 字段名2, 数据类型 [列级
别约束条件] [默认值]... ... ):

列如:

mysql> create table tb_emp1 (
    -> id int(11),
    -> name varchar(25),
    -> deptid int(11),
    -> salary float
    -> );
Query OK, 0 rows affected (0.02 sec)

1.2主键约束

主键约束要求键列的数据唯一,并且不允许为空。
1)单字段主键

语法: 字段名 数据类型 PRIMARY KEY [默认值]

列如:

mysql> create table tb_emp2 (
    -> id int(11) primary key,
    -> name varchar(25),
    -> deptid int,
    -> salary float
    -> );
Query OK, 0 rows affected (0.00 sec)

2)在定义完成所有列之后指定主键

语法:[CONSTRAINT <约束名>] PRIMARY KEY [字段名]

列如:

mysql> create table tb_emp3 ( 
    -> id int,
    -> name varchar(25),
    -> deptid int(11),
    -> salary float,
    -> primary key(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

3)多字段联合主键

语法:PRIMARY KEY [字段1,字段2,....]

列如:

mysql> create  table tb_emp4 (
    -> id int,
    -> name varchar(25),
    -> deptid int,
    -> salary float,
    -> primary key(name,deptid)
    -> );
Query OK, 0 rows affected (0.01 sec)
//name,deptid字段,均为主键

//插入数据验证

mysql> insert  into tb_emp4(name,deptid) values ('zhangsan','10');
Query OK, 1 row affected (0.01 sec)

mysql> insert  into tb_emp4(id,deptid) values ('1','10'); //主键不允许为空重复,所以会报错
ERROR 1364 (HY000): Field 'name' doesn't have a default value

mysql> insert  into tb_emp4(name,deptid) values ('zhuang','10');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tb_emp4;
+------+----------+--------+--------+
| id   | name     | deptid | salary |
+------+----------+--------+--------+
| NULL | zhangsan |     10 |   NULL |
| NULL | zhuang   |     10 |   NULL |
+------+----------+--------+--------+
2 rows in set (0.00 sec)

1.3 外键约束

外键用来在两个表数据之间建立连接,它可以是一列或者多列

语法:[CONSTRAINT<外键名>] FOREIGN KEY [字段名1,字段名2...] REFERENCES<主表名> 主键列1[主键列2...]

列如:

mysql> create table tb_dept1 (
    -> id int primary key,
    -> name varchar(22) not null,
    -> location varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)


mysql> create table tb_dept2 (
    -> id int primary key,
    -> name varchar(25),
    -> deptid int(11),
    -> salary float,
    -> CONSTRAINT fk_emp_dept1 FOREIGN KEY(deptid) REFERENCES tb_dept1(id)
    -> );

//查看外键

mysql> show create table tb_dept2\G
*************************** 1. row ***************************
       Table: tb_dept2
Create Table: CREATE TABLE `tb_dept2` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptid` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_emp_dept1` (`deptid`),
  CONSTRAINT `fk_emp_dept1` FOREIGN KEY (`deptid`) REFERENCES `tb_dept1` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

1.4 非空约束

非空约束指字段的值不能为空。

语法:字段名 数据类型 not null

例如:

mysql> create table tb_emp6
    -> (
    -> id int primary key,
    -> name varchar(25) not null,
    -> deptid int,
    -> salary float
    -> );
Query OK, 0 rows affected (0.00 sec)

1.5 唯一性约束

语法: [CONSTRATIN <约束名>] UNIQUE (<字段名>

列如:

mysql> create table tb_dept3
    -> (
    -> id int primary key,
    -> name varchar(22),
    -> location varchar(50),
    -> constraint sth unique(name)
    -> );
Query OK, 0 rows affected (0.01 sec)

1.6默认约束

默认约束指定某列的默认值

语法:字段名 数据类型 DEFAULT 默认值

列如:

mysql> create table tb_emp7
    -> (
    -> id int primary key,
    -> name varchar(25) not null,
    -> deptid int(11) default 1111,
    -> salary float,
    -> info varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)

//插入数据验证

mysql> insert into tb_emp7(id,name) values ('1','zhangsan');
Query OK, 1 row affected (0.00 sec)

mysql> select * from tb_emp7;
 
+----+----------+--------+--------+------+
| id | name     | deptid | salary | info |
+----+----------+--------+--------+------+
|  1 | zhangsan |   1111 |   NULL | NULL |
+----+----------+--------+--------+------+
1 row in set (0.00 sec)

1.7设置表的属性值自动增加

列如:

mysql> create table tb_emp8 (
    -> id int(11) primary key auto_increment,
    -> name varchar(25) not null,
    -> deptid int(11),
    -> salary float
    -> );
Query OK, 0 rows affected (0.00 sec)

//插入数据验证

mysql> insert into tb_emp8(name,salary) values ('zhangsan','10000'),('lisi','2000'),('wangwwu','30000');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from tb_emp8;
+----+----------+--------+--------+
| id | name     | deptid | salary |
+----+----------+--------+--------+
|  1 | zhangsan |   NULL |  10000 |
|  2 | lisi     |   NULL |   2000 |
|  3 | wangwu   |   NULL |  30000 |
+----+----------+--------+--------+
3 rows in set (0.00 sec)

2.查看数据表结构

2.1查看表基本结构语句

语法:DESCRIBE 表名; 或 DESC 表名;

列如:

mysql> describe tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(22) | NO   |     | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

// 或者

mysql> desc tb_dept1;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| name     | varchar(22) | NO   |     | NULL    |       |
| location | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

2.2查看详细表结构

语法:SHOW CREATE TABLE <表名\G>

列如:

mysql> show create table tb_emp1\G
*************************** 1. row ***************************
       Table: tb_emp1
Create Table: CREATE TABLE `tb_emp1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptid` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

3.修改数据表

3.1 修改表名

语法:ALTER TABLE <旧表名> RENAME [TO] <新表名>.

例如:

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| tb_dept1         |
| tb_dept2         |
| tb_dept3         |
| tb_emp1          |
| tb_emp2          |
| tb_emp3          |
| tb_emp4          |
| tb_emp6          |
| tb_emp7          |
| tb_emp8          |
| test             |
+------------------+
11 rows in set (0.00 sec)

mysql> alter table tb_dept1 rename tb_dept111;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| tb_dept111       |
| tb_dept2         |
| tb_dept3         |
| tb_emp1          |
| tb_emp2          |
| tb_emp3          |
| tb_emp4          |
| tb_emp6          |
| tb_emp7          |
| tb_emp8          |
| test             |
+------------------+
11 rows in set (0.00 sec)

3.2修改字段的数据类型

语法:ALTER TABLE <表名> MODIFY <字段名> <数据类型>

列如:

mysql> desc tb_dept2;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(25) | YES  |     | NULL    |       |
| deptid | int(11)     | YES  | MUL | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table tb_dept2 modify name varchar(30);
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(30) | YES  |     | NULL    |       |
| deptid | int(11)     | YES  | MUL | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3.3修改字段名

语法:ALTER TABLE <表名> CHANGE<旧字段名><新字段名> <新数据类型>

列如:

mysql> desc tb_emp1;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| nnn    | varchar(50) | YES  |     | NULL    |       |
| deptid | int(11)     | YES  |     | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table tb_emp1 change nnn name varchar(20);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_emp1;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(20) | YES  |     | NULL    |       |
| deptid | int(11)     | YES  |     | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

3.4添加字段

语法: ALTER TABLE <表名> ADD <新字段名><数据类型> [约束条件] [FIRST|AFTER 已存在字段名]

例如:
//在表最后添加字段

mysql> desc tb_dept2;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(30) | YES  |     | NULL    |       |
| deptid | int(11)     | YES  | MUL | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table tb_dept2 add column1 varchar(20) not null;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| deptid  | int(11)     | YES  | MUL | NULL    |       |
| salary  | float       | YES  |     | NULL    |       |
| column1 | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

//表的第一行添加内容first

mysql> alter table tb_dept2 add column2 int(11) first;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| column2 | int(11)     | YES  |     | NULL    |       |
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| deptid  | int(11)     | YES  | MUL | NULL    |       |
| salary  | float       | YES  |     | NULL    |       |
| column1 | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec

//在name字段前面添加新字段after

mysql> alter table tb_dept2 add column3 int after name;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| column2 | int(11)     | YES  |     | NULL    |       |
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| column3 | int(11)     | YES  |     | NULL    |       |
| deptid  | int(11)     | YES  | MUL | NULL    |       |
| salary  | float       | YES  |     | NULL    |       |
| column1 | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

3.5删除字段

语法:ALTER TABLE <表名> DROP <字段名>

列如:

mysql> alter table tb_dept2 drop column2;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| column3 | int(11)     | YES  |     | NULL    |       |
| deptid  | int(11)     | YES  | MUL | NULL    |       |
| salary  | float       | YES  |     | NULL    |       |
| column1 | varchar(20) | NO   |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

3.6 修改字段的排序位置

语法:ALTER TABLE <表名> MODIFY <字段名> <数据类型> FIRST | AFTER <字段2> 

列如:
//将 column1排序到第一行

mysql> alter table tb_dept2 modify column1 varchar(12) first;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| column1 | varchar(12) | YES  |     | NULL    |       |
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| column3 | int(11)     | YES  |     | NULL    |       |
| deptid  | int(11)     | YES  | MUL | NULL    |       |
| salary  | float       | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

//将column1排序到name下

mysql> alter table tb_dept2 modify column1 varchar(12) after name;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc tb_dept2;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(30) | YES  |     | NULL    |       |
| column1 | varchar(12) | YES  |     | NULL    |       |
| column3 | int(11)     | YES  |     | NULL    |       |
| deptid  | int(11)     | YES  | MUL | NULL    |       |
| salary  | float       | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
6 rows in set (0.00 sec)

3.7更改表存储引擎

语法:ALTER TABLE <表名> ENGINE=<更改后的存储引擎>

列如:

mysql> show create table tb_emp2\G
*************************** 1. row ***************************
       Table: tb_emp2
Create Table: CREATE TABLE `tb_emp2` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptid` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> alter table tb_emp2 engine=myisam;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table tb_emp2\G
*************************** 1. row ***************************
       Table: tb_emp2
Create Table: CREATE TABLE `tb_emp2` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptid` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

3.8删除表的外键约束

语法:ALTER TABLE <表名> DROP FOREIGN KEY <外键约束名>

例如:

mysql> create table t01
    -> (
    -> id int primary key,
    -> name varchar(20),
    -> location varchar(30)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql> create table t02
    -> (
    -> id int primary key,
    -> name varchar(25),
    -> deptid int(11),
    -> salary float,
    -> CONSTRAINT fk_empp FOREIGN KEY(deptid) REFERENCES t01(id)
    -> );
Query OK, 0 rows affected (0.03 sec)

//查看并验证

mysql> show create table t02\G
*************************** 1. row ***************************
       Table: t02
Create Table: CREATE TABLE `t02` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptid` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_empp` (`deptid`),
  CONSTRAINT `fk_empp` FOREIGN KEY (`deptid`) REFERENCES `t01` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> alter table t02 drop foreign key fk_empp;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table t02\G
*************************** 1. row ***************************
       Table: t02
Create Table: CREATE TABLE `t02` (
  `id` int(11) NOT NULL,
  `name` varchar(25) DEFAULT NULL,
  `deptid` int(11) DEFAULT NULL,
  `salary` float DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fk_empp` (`deptid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

4.删除数据表

4.1 删除没有被关联的表

语法:DROP TABLE [IF EXISTS]表1,表2...

列如:

mysql> drop table if exists tb_dept2;
Query OK, 0 rows affected (0.00 sec)

4.2 删除被其他表关联的主表

//先创建表tb_t1

mysql> drop table if exists tb_dept2;
Query OK, 0 rows affected (0.00 sec)

mysql> create table tb_t1
    -> (
    -> id int(11) primary key,
    -> name varchar(50)
    -> );
Query OK, 0 rows affected (0.01 sec)

//创建tb_emp

mysql> create table tb_emp
    -> (
    -> id int(11) primary key,
    -> name varchar(25),
    -> deptid int(11),
    -> salary float,
    ->  CONSTRAINT fk_emp_dept01 FOREIGN KEY(deptid) REFERENCES tb_t1(id)
    -> );
Query OK, 0 rows affected (0.01 sec)

//直接删除父表tb_t1

mysql> drop table tb_t1;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

可以看到在外键约束时,主表不能直接删除。
//解除关联子表tb_t1的外键约束

mysql> alter table tb_emp drop foreign key fk_emp_dept01;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

//解除后就可以被删除了

mysql> drop table tb_t1;
Query OK, 0 rows affected (0.01 sec)

猜你喜欢

转载自blog.csdn.net/weixin_45310323/article/details/110078100