MySQL DDL语句
来源:互联网 发布:邓小平网络纪念馆留言 编辑:程序博客网 时间:2024/05/18 01:35
DDL(date definitionlanguages)语句:数据定义了语言,定义了数据库、表、索引等对象的定义。常用的语句包括create、drop、alter等
1.创建数据库mysql> createdatabase test2 ;
Query OK, 1 row affected (0.00 sec)
row affected表示有一行记录受到影响
sec表示这个操作的执行时间
2.查看数据库
mysql> showdatabases;
+--------------------+
| 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> usesakila;
Database changed
4.查看表
mysql> showtables;
+----------------------------+
| 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 databasetest2 ;
Query OK, 0 rows affected (0.04 sec)
删除数据库后,数据库下面所有的表都会被删除。
6.创建表
语法:
create table tablename(
column_name 字段名称
column_type 字段类型
constraints 字段的约束
注意:表名额可以包括字母、数字,下划线和美元符号,但不能完全是数字。
取表名和数据库名的原则是看其名和其意
mysql> create tableemp(
Query OK, 0 rows affected (0.02 sec)
7.查看表结构
desc tablename
mysql> descemp ;
+---------+---------------+------+-----+---------+-------+
| 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 createtable emp \G
*************************** 1. row***************************
Create Table: CREATE TABLE `emp` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
除了看到表的定义外,可以看到表使用的存储引擎engine=innodb,使用的字符集charset=utf8。"\G"的意思是使得记录能够按照竖排显示,更易阅读
9.约束
[NOT NULL |NULL] 是否可以为空
[DEFAULEdefault_value] 是否有默认值
[AUTP_INCREMENT] 是否是自动增长
[UNIQUE [KEY] | [RPIMARY] KEY] 是否是主键
[CONMENT'string'] 注释
mysql> CREATE TABLEcity(
->city_id int NOT NULLAUTO_inCREMENt,
->city varchar(50) NOT NULL comment'city name',
->country_name varchar(20) NOT NULLdefault 'china',
->KEY(city_id)
->);
Query OK, 0 rows affected (0.01 sec)
10.插入数据
mysql> insert intocity(city) values('beijing');
Query OK, 1 row affected (0.01 sec)
11.创建相似表
create table table_name like table_name1;
mysql> create tablecity_bak like city;
Query OK, 0 rows affected (0.02 sec)
12.增加表字段
语法
alter table tablename ADDcol_name column_definition [FIRST | AFTER col_name]
mysql> alter tableemp add column age int(3);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> descemp;
+---------+---------------+------+-----+---------+-------+
| 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 tablenameMODIFY col_name column_definition [FIRST | AFTERcol_name]
mysql> alter tableemp modify name varchar(20);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> descemp;
+---------+---------------+------+-----+---------+-------+
| 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 tableemp modify hirdate date first;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> descemp;
+---------+---------------+------+-----+---------+-------+
| 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 DROPcol_name
mysql> alter tableemp drop age;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> descemp;
+---------+---------------+------+-----+---------+-------+
| 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 tablenameCHANGE old_col_name new_col_name column_definition [FIRST |AFTERcol_name]
mysql> alter tableemp change deption depton1 int(11);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> descemp;
+---------+---------------+------+-----+---------+-------+
| 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 tablenameRENAME [TO] new_tbl_name;
mysql> alter tableemp rename emp1;
Query OK, 0 rows affected (0.00 sec)
18.删除表
语法:
drop tabletablename;
mysql> drop tableemp;
Query OK, 0 rows affected (0.00 sec)
19.创建索引
语法:
CREATE[UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
index_col_name:
mysql> create indexind_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 ontable_name;
mysql> drop indexind_emp_dept on emp;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
21.创建视图
视图是一种虚拟存在的表,对于使用视图的用户来说是透明的。视图在数据库中并不占用存储空间。在使用过程中的数据是从实际存在的表动态生成的。
简单:使用视图的用户完全不用关心后面对应的表机构和筛选条件,对用户来说是已经过滤好的复合条件的结果集
安全:使用视图的用户只能访问他们被允许查询的结果集。
语法:
CREATE [ORREPLACE]
VIEW view_name[(column_list)]
ASselect_statement
[WITH [CASCADED | LOCAL]CHECK OPTION];
其中,[WITH [CASCADED | LOCAL] CHECKOPTION],决定了是否允许更新数据使用记录不再满足视图的条件。
*LOCAL是只要满足本视图的条件就可以更新
*CASCADED 是必须满足所有针对创建该视图的相关视图的条件才可以更新,这是默认选项
0 0
- mysql优化-----ddl语句
- mysql优化-----ddl语句
- MySQL DDL语句
- MySQL DDL操作语句
- MySQL笔记-DDL语句
- mysql---DDL语句
- MySql基础-DDL语句
- MySQL之DDL语句
- mysql 常用的 ddl 语句。
- Mysql之DDL语句_create_alter
- MYSQL数据库基本DDL语句
- mysql 常用 ddl 语句示例
- MySQL语句基础之DDL语句
- MySQL中的DDL语句,DML语句初步
- 【2】mysql 数据库的DDL语句
- MYSQL数据库——DDL语句
- MySql基础的增删改 ddl语句
- mysql语句及其分类(DDL,DML)
- MySQL存储引擎比较
- Windows Server 2003 R2 With Sp2 序列号
- MySQL 视图
- MySQL 常用函数
- MySQL DML语句
- MySQL DDL语句
- MySQL查询语句练习题,测试足够用…
- MySQL插入、更新与删除数据 …
- Centos下安装Django
- Centos下安装pycharm
- 响应时间/并发用户数/吞吐量…
- qtp监听请求响应时间,服务器ip,…
- 接口平台——自定义接口返回内容提高…
- 识别IE11