mysql---DDL语句
2016-11-15 14:29
162 查看
数据库连接以后,一般通过
连接到数据库,mysql代表客户端命令,-u后面跟连接的数据库用户,-p后面表示需要输入密码,命令符的结束以;或者\g结束
创建user数据库
query ok代表执行成功,1 row affected,受影响行数,0.01 sec代表操作时间
如果重复创建数据库会出现下列提示
查看当前系统中所有数据库
information_schema 存储系统中的一些数据库对象信息,比如用户信息,列信息,权限信息,
mysql 存储用户权限信息
选择数据库
显示当前数据库中的数据表
删除数据库
创建表emp
查看emp表
更详细的信息
可以看到engine和charset \G让记录可以按照字段竖着排列
删除表
修改表
修改表增加一个column
修改表,删除一个column
修改表的column字段名称和类型
change 和 modify都是修改表 change可以修改表字段 而modify不可以
after添加一个字段在某个字段之后
修改一个字段的位置让他处于表的最前方
表改名
mysql -uroot -ppassword;
连接到数据库,mysql代表客户端命令,-u后面跟连接的数据库用户,-p后面表示需要输入密码,命令符的结束以;或者\g结束
创建user数据库
create database user; Query OK, 1 row affected (0.01 sec)
query ok代表执行成功,1 row affected,受影响行数,0.01 sec代表操作时间
如果重复创建数据库会出现下列提示
create database user; ERROR 1007 (HY000): Can't create database 'user'; database exists" data-snippet-id="ext.f3f65b2cbd03a54d16ae052d1bb87283" data-snippet-saved="false" data-codota-status="done">[code]mysql> create database user; ERROR 1007 (HY000): Can't create database 'user'; database exists
查看当前系统中所有数据库
show databases; +--------------------+ | Database | +--------------------+ | information_schema | | localhost | | mysql | | performance_schema | | sys | | test1 | | user | +--------------------+ 7 rows in set (0.01 sec)" data-snippet-id="ext.bb4a52d24eaddec1cfed0133ce11a40b" data-snippet-saved="false" data-codota-status="done">[code]mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | localhost | | mysql | | performance_schema | | sys | | test1 | | user | +--------------------+ 7 rows in set (0.01 sec)
information_schema 存储系统中的一些数据库对象信息,比如用户信息,列信息,权限信息,
mysql 存储用户权限信息
选择数据库
mysql> use user; Database changed
显示当前数据库中的数据表
mysql> show tables; Empty set (0.00 sec)
删除数据库
drop database test1; Query OK, 0 rows affected (0.05 sec) " data-snippet-id="ext.86830be5c7c21e31ff648fde7158ecd3" data-snippet-saved="false" data-codota-status="done">[code]mysql> drop database test1; Query OK, 0 rows affected (0.05 sec)
创建表emp
create table emp (ename varchar(10), hiredate date, sal decimal(10, 2), deptno int(2)); Query OK, 0 rows affected (0.04 sec)" data-snippet-id="ext.a487a8e457d1adb4aac4e93e85f21496" data-snippet-saved="false" data-codota-status="done">[code]mysql> create table emp (ename varchar(10), hiredate date, sal decimal(10, 2), deptno int(2)); Query OK, 0 rows affected (0.04 sec)
查看emp表
desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(10) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | | deptno | int(2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) " data-snippet-id="ext.710b34ca35d7041fc1d63310a7c499d0" data-snippet-saved="false" data-codota-status="done">[code]mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(10) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | | deptno | int(2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec)
更详细的信息
show create table emp \G; *************************** 1. row *************************** Table: emp Create Table: CREATE TABLE `emp` ( `ename` varchar(10) DEFAULT NULL, `hiredate` date DEFAULT NULL, `sal` decimal(10,2) DEFAULT NULL, `deptno` int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified " data-snippet-id="ext.124ab1ded9f8d969aed0de84bed204d4" data-snippet-saved="false" data-codota-status="done">[code]mysql> show create table emp \G; *************************** 1. row *************************** Table: emp Create Table: CREATE TABLE `emp` ( `ename` varchar(10) DEFAULT NULL, `hiredate` date DEFAULT NULL, `sal` decimal(10,2) DEFAULT NULL, `deptno` int(2) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1 1 row in set (0.00 sec) ERROR: No query specified
可以看到engine和charset \G让记录可以按照字段竖着排列
删除表
drop table emp; Query OK, 0 rows affected (0.03 sec) " data-snippet-id="ext.6ec904a52059864019c96f76ead8f4dc" data-snippet-saved="false" data-codota-status="done">[code]mysql> drop table emp; Query OK, 0 rows affected (0.03 sec)
修改表
desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(20) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> alter table emp modify ename varchar(30); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) " data-snippet-id="ext.4c7ce25e55c493b6d887c0b6e1e98c16" data-snippet-saved="false" data-codota-status="done">[code]mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(20) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.01 sec) mysql> alter table emp modify ename varchar(30); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
修改表增加一个column
alter table emp add column sex varchar(6); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | | sex | varchar(6) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec) " data-snippet-id="ext.53a44d05ddb4eed4fd131f1198d6a0b9" data-snippet-saved="false" data-codota-status="done">[code]mysql> alter table emp add column sex varchar(6); Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | | sex | varchar(6) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
修改表,删除一个column
alter table emp drop sex; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) " data-snippet-id="ext.dc9856c371b90a48c69a644cb0884f0d" data-snippet-saved="false" data-codota-status="done">[code]mysql> alter table emp drop sex; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | ename | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
修改表的column字段名称和类型
alter table emp change ename name varchar(40); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | name | varchar(40) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) " data-snippet-id="ext.13e340073cf99955732726f4fe0b5ae7" data-snippet-saved="false" data-codota-status="done">[code]mysql> alter table emp change ename name varchar(40); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | name | varchar(40) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 4 rows in set (0.00 sec)
change 和 modify都是修改表 change可以修改表字段 而modify不可以
after添加一个字段在某个字段之后
mysql> alter table emp add address after name; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'after name' at line 1 mysql> alter table emp add address varchar(30) after name; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | name | varchar(40) | YES | | NULL | | | address | varchar(30) | YES | | NULL | | | age | int(3) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
修改一个字段的位置让他处于表的最前方
mysql> alter table emp modify age int(5) first; Query OK, 0 rows affected (0.03 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc emp; +----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+---------------+------+-----+---------+-------+ | age | int(5) | YES | | NULL | | | name | varchar(40) | YES | | NULL | | | address | varchar(30) | YES | | NULL | | | hiredate | date | YES | | NULL | | | sal | decimal(10,2) | YES | | NULL | | +----------+---------------+------+-----+---------+-------+ 5 rows in set (0.00 sec)
表改名
mysql> alter table emp rename userinfo; Query OK, 0 rows affected (0.00 sec) mysql> show tables; +----------------+ | Tables_in_user | +----------------+ | userinfo | +----------------+ 1 row in set (0.00 sec)
相关文章推荐
- MYSQL常用DDL,DML语句及外键创建
- 在 mysql 中对特定的库禁用 DDL 语句
- MySQL--- DDL、DML、DCL都包括那些语句?
- 第三十天 MySQL语句DDL、DDL语句索引的策略、Select查询语句、子查询视图
- mysql一个事务中有DDL语句的binlog情况
- MySQL临时表增删的DDL语句是否会提交事务的测试
- MySQL 数据库SQL语句---DDL语句
- MySQL DDL操作语句
- MySQL基础教程14 —— SQL语法之数据定义语句DDL
- MySQL笔记-DDL语句
- mysql优化-----ddl语句
- mysql DDL语句
- mysql系统表加trigger和对特定的库禁用 DDL 语句
- Mysql之DDL语句_create_alter
- MySQL DDL语句
- mysql 5.1中DDL语句对Transaction的影响
- mysql里得到数据库或表的DDL创建语句
- mysql 常用的 ddl 语句。
- 详解MySQL---DDL语句、DML语句与DCL语句
- Mysql基础入门-SQL_DDL语句