DDL语句
DDL 是数据定义语言的缩写,简单来说,就是对数据库内部的对象进行创建、删除、修改的操作语言。
黄色标注的是关键字
创建数据库
CREATE DATABASE dbname;
删除数据库
DROP DATABASE dbname;
查看所有数据库
SHOW DATABASES;
选择要操作的数据库
USE dbname;
查看所有表
SHOW TABLES;
创建数据库表
CREATE TABLE table_name(
字段1 字段类型 ,
字段2 字段类型 ,
字段3 字段类型 ,
字段4 字段类型
);
查看表结构
desc table_name
查看创建表的SQL语句
show create table table_name \G
删除表
drop table table_name;
修改表结构、更新表中字段类型
alter table 'table_name' modify '要修改字段' '新类型';
新增表中字段
alter table 'table_name' add column '字段名' '类型';
删除表中字段
alter table 'table_name' drop column '字段名';
修改表中字段名
alter table 'table_name' change '旧字段名' '新字段名' '类型';
*************************************************************************************
change 和 modify 的区别
当我们需要对列(字段名)重命名时,可以使用change。
重命名时,需要指定旧的列名和新的列名以及新列名的类型。
举个栗子:
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_deptno | int(2) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
我们要将u_deptno修改为u_age,需要这样写:
alter table temp_user change u_deptno u_age int(3);
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(3) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
当然,如果你想修改列名(字段)的类型而不是名称,chage语法任然要求你写上旧的列名和新的列名,即使旧列名和新列名是一样的。
alter table temp_user change u_age u_age int(10);
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(10) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
-------------------------------------
但如果你使用modify来改变列的类型,就不需要重命名。
alter table temp_user modify u_age int(5);
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(5) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
当然了,具体怎么使用看你自己的习惯。
从个人来看,修改列名(字段名)使用change,仅仅是修改字段类型使用modify。
*************************************************************************************
修改字段的排列顺序
ALTER TABLE tablename ADD '新字段' '新字段类型' AFTER '目标字段';
ALTER TABLE tablename ADD '新字段' '新字段类型' FIRST;
上面的列名(字段)增加和修改语法(add/change/modify)中,都有两个可选参数 first / after,这两个参数可以用来修改字段在表中的位置。
默认add新增的新字段是加在表的最后位置,而change/modify默认都不回改变字段的位置。
举个栗子:
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(5) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table temp_user add u_number int(10);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(5) | YES | | NULL | |
| u_number | int(10) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
u_number字段放在了表的最后位置,对吧....
下面我新增一个字段,指定位置:
mysql> alter table temp_user add u_maxnumber int(20) after u_namess;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_maxnumber | int(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(5) | YES | | NULL | |
| u_number | int(10) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
6 rows in set (0.01 sec)
mysql> alter table temp_user add u_minnumber int(20) after u_namess;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_namess | varchar(20) | YES | | NULL | |
| u_minnumber | int(20) | YES | | NULL | |
| u_maxnumber | int(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(5) | YES | | NULL | |
| u_number | int(10) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> alter table temp_user add u_count int(20) first;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc temp_user;
+--------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| u_count | int(20) | YES | | NULL | |
| u_namess | varchar(20) | YES | | NULL | |
| u_minnumber | int(20) | YES | | NULL | |
| u_maxnumber | int(20) | YES | | NULL | |
| u_createdate | date | YES | | NULL | |
| u_sal | decimal(10,2) | YES | | NULL | |
| u_age | int(5) | YES | | NULL | |
| u_number | int(10) | YES | | NULL | |
+--------------+---------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
注意:这个特性属于mysql在标准sql上扩展的,其他数据库不一定适用。
*************************************************************************************
修改表名
ALTER TABLE old_table_name RENAME new_table_name;
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| temp_user |
+-----------------+
1 row in set (0.00 sec)
mysql> alter table temp_user rename temp_users;
Query OK, 0 rows affected (0.03 sec)
mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| temp_users |
+-----------------+
1 row in set (0.00 sec)
OK,DDL语句复习,就到这里....