您的位置:首页 > 数据库 > MySQL

MySQL DDL语句

2014-08-21 13:58 148 查看
DDL(date definition
languages)语句:
数据定义了语言,定义了数据库、表、索引等对象的定义。常用的语句包括create、drop、alter等
1.创建数据库[/b]

mysql> create
database test2 ;
Query OK, 1 row affected (0.00 sec)

row affected表示有一行记录受到影响

sec表示这个操作的执行时间

2.查看数据库

mysql> show
databases;
+--------------------+
| Database
|
+--------------------+
| information_schema |
| mysql

|
| performance_schema |
| sakila

|
| test

|
| test1

|
| test2

|
+--------------------+
7 rows in set (0.01 sec)

information_schema:存储了系统中一些对象信息,如:用户表信息,试图信息,各种数据库变量状态、字符集信息等

mysql:主要是用户的相关权限信息

performance_schema:记录系统性能的数据库。通过performance_schema变更决定是否开启,默认不是开启的

test:测试数据库

注意:数据库和表名是否区别大小写由一个参数决定:

lower_case_table_mames

当值为1时,表名和库名对大小写不敏感,用小写保存在硬盘上;设置为0时,按指定的名字保存在硬盘上,大小写敏感

3.选择数据库

mysql> use
sakila;
Database changed

4.查看表

mysql> show
tables;
+----------------------------+
| Tables_in_sakila
|
+----------------------------+
| actor

|
| actor_info

|
| address

|
| category

|
| city

|
| country

|
| customer

|
| customer_list

|
| film

|
| film_actor

|
| film_category

|
| film_list

|
| film_text

|
| inventory

|
| language

|
| nicer_but_slower_film_list |
| payment

|
| rental

|
| sales_by_film_category
|
| sales_by_store

|
| staff

|
| staff_list

|
| store

|
+----------------------------+
23 rows in set (0.00 sec)

5.删除数据库

mysql> drop database
test2 ;
Query OK, 0 rows affected (0.04 sec)

删除数据库后,数据库下面所有的表都会被删除。

6.创建表

语法:

create table tablename(

column_name1 column_type1
constrains,

column_name2 column_ytpe2
constraints,

......

column_nameN colume_tupeN
constraints);

column_name
字段名称

column_type
字段类型

constraints

字段的约束

注意:表名额可以包括字母、数字,下划线和美元符号,但不能完全是数字。

取表名和数据库名的原则是看其名和其意

mysql> create table
emp(

-> name varchar(10) ,

-> hirdate date,

-> sal decimal(10,2),

-> depton int);
Query OK, 0 rows affected (0.02 sec)

7.查看表结构

desc tablename

mysql> desc
emp ;

+---------+---------------+------+-----+---------+-------+
| Field | Type

| Null | Key | Default | Extra |

+---------+---------------+------+-----+---------+-------+
| name | varchar(10)
| YES |
| NULL |
|
| hirdate | date
| YES
| | NULL
|
|
| sal | decimal(10,2) |
YES | |
NULL |
|
| depton | int(11)
| YES |
| NULL
|
|

+---------+---------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

Filed:字段名称

Type:字段类型

Null:是否可以为空

Key:是否有索引

Default:是否有默认值

8.查看具体表

show create table tablename

mysql> show create
table emp \G
*************************** 1. row
***************************

Table: emp
Create Table: CREATE TABLE `emp` (
`name` varchar(10) DEFAULT NULL,
`hirdate` date DEFAULT NULL,
`sal` decimal(10,2) DEFAULT NULL,
`depton` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

除了看到表的定义外,可以看到表使用的存储引擎engine=innodb,使用的字符集charset=utf8。"\G"的意思是使得记录能够按照竖排显示,更易阅读

9.约束

[NOT NULL |
NULL]
是否可以为空

[DEFAULE
default_value]
是否有默认值

[AUTP_INCREMENT]

是否是自动增长

[UNIQUE [KEY] | [RPIMARY] KEY]
是否是主键

[CONMENT
'string']
注释

mysql> CREATE TABLE
city(
->city_id int NOT NULL
AUTO_inCREMENt,
->city varchar(50) NOT NULL comment
'city name',
->country_name varchar(20) NOT NULL
default 'china',
->KEY
(city_id)
->);
Query OK, 0 rows affected (0.01 sec)

10.插入数据

mysql> insert into
city(city) values('beijing');
Query OK, 1 row affected (0.01 sec)

11.创建相似表

create table table_name like table_name1;

mysql> create table
city_bak like city;
Query OK, 0 rows affected (0.02 sec)

12.增加表字段

语法

alter table tablename ADD
col_name column_definition [FIRST | AFTER col_name]

mysql> alter table
emp add column age int(3);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0
Warnings: 0

mysql> desc
emp;

+---------+---------------+------+-----+---------+-------+
| Field | Type

| Null | Key | Default | Extra |

+---------+---------------+------+-----+---------+-------+
| name | varchar(10)
| YES |
| NULL |
|
| hirdate | date
| YES
| | NULL
|
|
| sal | decimal(10,2) |
YES | |
NULL |
|
| depton | int(11)
| YES |
| NULL
|
|
| age | int(3)

| YES |
| NULL |
|

+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

13.修改字段类型

语法:

alter table tablename
MODIFY col_name column_definition [FIRST | AFTER
col_name]

mysql> alter table
emp modify name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0
Warnings: 0

mysql> desc
emp;

+---------+---------------+------+-----+---------+-------+
| Field | Type

| Null | Key | Default | Extra |

+---------+---------------+------+-----+---------+-------+
| name
| varchar(20)
| YES | |
NULL |
|
| hirdate | date
| YES
| | NULL
|
|
| sal | decimal(10,2) |
YES | |
NULL |
|
| depton | int(11)
| YES |
| NULL
|
|
| age | int(3)

| YES |
| NULL |
|

+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

14.修改字段排列顺序

mysql> alter table
emp modify hirdate date first;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0
Warnings: 0

mysql> desc
emp;

+---------+---------------+------+-----+---------+-------+
| Field | Type

| Null | Key | Default | Extra |

+---------+---------------+------+-----+---------+-------+
| hirdate | date

| YES |
| NULL
|
|
| name | varchar(20)
| YES |
| NULL |
|
| sal | decimal(10,2) |
YES | |
NULL |
|
| depton | int(11)
| YES |
| NULL
|
|
| age | int(3)

| YES |
| NULL |
|

+---------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

15.删除表字段

语法:

alter table tablename DROP
col_name

mysql> alter table
emp drop age;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0
Warnings: 0

mysql> desc
emp;

+---------+---------------+------+-----+---------+-------+
| Field | Type

| Null | Key | Default | Extra |

+---------+---------------+------+-----+---------+-------+
| hirdate | date
| YES
| | NULL
|
|
| name | varchar(20)
| YES |
| NULL |
|
| sal | decimal(10,2) |
YES | |
NULL |
|
| depton | int(11)
| YES |
| NULL
|
|

+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

16.字段改名

alter table tablename
CHANGE old_col_name new_col_name column_definition [FIRST |AFTER
col_name]

mysql> alter table
emp change deption depton1 int(11);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0
Warnings: 0

mysql> desc
emp;

+---------+---------------+------+-----+---------+-------+
| Field | Type

| Null | Key | Default | Extra |

+---------+---------------+------+-----+---------+-------+
| hirdate | date
| YES
| | NULL
|
|
| name | varchar(20)
| YES |
| NULL |
|
| sal | decimal(10,2) |
YES | |
NULL |
|
| depton1 |
int(11) |
YES | |
NULL |
|

+---------+---------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

注意:change和modify都可以修改表的定义,不同的是change后面需要写两次列名。但是change的优点是可以修改列名称,modify则不能。

change/first/after这些关键字是MySQL在标准SQL上的扩展,在其他数据库上不适用。

17.更改表名

语法:

alter table tablename
RENAME [TO] new_tbl_name;

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

18.删除表

语法:

drop table
tablename;

mysql> drop table
emp;
Query OK, 0 rows affected (0.00 sec)

19.创建索引

语法:

CREATE
[UNIQUE|FULLTEXT|SPATIAL] INDEX index_name

[USING index_type]

ON tbl_name (index_col_name,...)

index_col_name:

col_name [(length)] [ASC | DESC]

mysql> create index
ind_emp_dept using btree on emp(depton);
Query OK, 0 rows affected (0.10 sec)
Records: 0 Duplicates: 0
Warnings: 0

注意:索引起名最好以ind开头或者idx等类似index开头,做到看其名字知其意,创建索引默认是BTREE索引

使用alter table也可以创建和删除索引

20.删除索引

语法:

drop index index_name on
table_name;

mysql> drop index
ind_emp_dept on emp;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0
Warnings: 0

21.创建视图

视图是一种虚拟存在的表,对于使用视图的用户来说是透明的。视图在数据库中并不占用存储空间。在使用过程中的数据是从实际存在的表动态生成的。

简单:使用视图的用户完全不用关心后面对应的表机构和筛选条件,对用户来说是已经过滤好的复合条件的结果集

安全:使用视图的用户只能访问他们被允许查询的结果集。

语法:

CREATE [OR
REPLACE]

VIEW view_name
[(column_list)]

AS
select_statement

[WITH [CASCADED | LOCAL]
CHECK OPTION];

其中,[WITH [CASCADED | LOCAL] CHECK
OPTION],决定了是否允许更新数据使用记录不再满足视图的条件。

*LOCAL是只要满足本视图的条件就可以更新

*CASCADED 是必须满足所有针对创建该视图的相关视图的条件才可以更新,这是默认选项
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: