SQL语句之数据定义语言(DDL)详解
操作对象:数据库
1)创建数据库
MariaDB [(none)]> help create database
Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_specification] ...
create_specification:
[DEFAULT] CHARACTER SET [=] charset_name(指定字符集)
| [DEFAULT] COLLATE [=] collation_name(指定排序规则)
查看系统默认支持的字符集和排序规则:
MariaDB [(none)]> show character set;
MariaDB [(none)]> show collation;
我们可以在创建数据库的时候,指定字符集和排序规则,如果不指定,则默认使用mysql服务器级别指定的字符集和排序规则
案例:
创建discuz数据库
MariaDB [(none)]> create database discuz default character set utf8;
Query OK, 1 row affected (0.00 sec)
2)修改数据库
MariaDB [(none)]> help alter database
Name: 'ALTER DATABASE'
Description:
Syntax:
ALTER {DATABASE | SCHEMA} [db_name]
alter_specification ...(修改数据库的字符集和排序规则)
ALTER {DATABASE | SCHEMA} db_name
UPGRADE DATA DIRECTORY NAME
alter_specification:
[DEFAULT] CHARACTER SET [=] charset_name
| [DEFAULT] COLLATE [=] collation_name
(注意:一般工作中,很少用到)
3)删除数据库
MariaDB [(none)]> help drop database
Name: 'DROP DATABASE'
Description:
Syntax:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name
案例:
MariaDB [(none)]> drop database discuz;
Query OK, 0 rows affected (0.00 sec)
操作对象:表
1)创建表
三种方式创建表
MariaDB [(none)]> help create table
Name: 'CREATE TABLE'
Description:
Syntax:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
(create_definition,...)
[table_options]
[partition_options]
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
[table_options]
[partition_options]
select_statement
Or:
CREATE [TEMPORARY] TABLE [IF NOT EXISTS] tbl_name
{ LIKE old_tbl_name | (LIKE old_tbl_name) }
create_definition:
col_name column_definition(1)
| [CONSTRAINT [symbol]] PRIMARY KEY [index_type] (index_col_name,...)
[index_option] ...
| {INDEX|KEY} [index_name] [index_type] (index_col_name,...)
[index_option] ...
| [CONSTRAINT [symbol]] UNIQUE [INDEX|KEY]
[index_name] [index_type] (index_col_name,...)
[index_option] ...
| {FULLTEXT|SPATIAL} [INDEX|KEY] [index_name] (index_col_name,...)----------全文,稀疏索引 FUNLLTEXT INDEX (col1,col2)
[index_option] ...
| [CONSTRAINT [symbol]] FOREIGN KEY ------------外键
[index_name] (index_col_name,...) reference_definition
| CHECK (expr)
column_definition:(1)
data_type(2) [NOT NULL | NULL] [DEFAULT default_value] ----------------NOT NULL | NULL, DEFAULT 默认值,所有类型都可以用的
[AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY]-----------------AUTO_INCREMENT自动增长,UNIQUE KEY唯一键,PRIMARY KEY主键
[COMMENT 'string']
[COLUMN_FORMAT {FIXED|DYNAMIC|DEFAULT}]
[STORAGE {DISK|MEMORY|DEFAULT}]
[reference_definition](3)
data_type:(2)
数值类型
| TINYINT[(length)] [UNSIGNED] [ZEROFILL]-------------UNSIGNED无符号,取值:0-255
| SMALLINT[(length)] [UNSIGNED] [ZEROFILL]-----------UNSIGNED无符号,取值:0-65535
| INT[(length)] [UNSIGNED] [ZEROFILL]--------------UNSIGNED无符号,取值:0 ~ 4294967295
| BIGINT[(length)] [UNSIGNED] [ZEROFILL]
| REAL[(length,decimals)] [UNSIGNED] [ZEROFILL]
| DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL]------------双精度,length总长度,decimals小数点后占几位
| FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL]---------------单精度,length总长度,decimals小数点后占几位
| DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL]
| NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL]
日期类型
| DATE----------日期
| TIME----------时间
| TIMESTAMP---------------时间戳,从1970年1月1日0分0秒,到当前时间,所经过的秒数(相对时间)
| DATETIME---------日期时间型(绝对时间)
| YEAR------------记录年
字符串类型
| CHAR[(length)]--------------定长
[CHARACTER SET charset_name] [COLLATE collation_name]
| VARCHAR(length)----------变长
[CHARACTER SET charset_name] [COLLATE collation_name]
| BINARY[(length)]
| VARBINARY(length)
| TINYBLOB
| BLOB
| MEDIUMBLOB
| LONGBLOB
| TINYTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| TEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| MEDIUMTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| LONGTEXT [BINARY]
[CHARACTER SET charset_name] [COLLATE collation_name]
| ENUM(value1,value2,value3,...)----------------枚举类型
[CHARACTER SET charset_name] [COLLATE collation_name]
| SET(value1,value2,value3,...)
[CHARACTER SET charset_name] [COLLATE collation_name]
| spatial_type
reference_definition:(3)
REFERENCES tbl_name (index_col_name,...)
[MATCH FULL | MATCH PARTIAL | MATCH SIMPLE]
[ON DELETE reference_option]
[ON UPDATE reference_option(4)]
reference_option:(4)
RESTRICT | CASCADE | SET NULL | NO ACTION
table_option:
ENGINE [=] engine_name------------------------MariaDB [testdb]> show engines;
| AUTO_INCREMENT [=] value
| AVG_ROW_LENGTH [=] value
| [DEFAULT] CHARACTER SET [=] charset_name
| CHECKSUM [=] {0 | 1}
| [DEFAULT] COLLATE [=] collation_name
| COMMENT [=] 'string'
| CONNECTION [=] 'connect_string'
| DATA DIRECTORY [=] 'absolute path to directory'
| DELAY_KEY_WRITE [=] {0 | 1}
| INDEX DIRECTORY [=] 'absolute path to directory'
| INSERT_METHOD [=] { NO | FIRST | LAST }
| KEY_BLOCK_SIZE [=] value
| MAX_ROWS [=] value
| MIN_ROWS [=] value
| PACK_KEYS [=] {0 | 1 | DEFAULT}
| PASSWORD [=] 'string'
| ROW_FORMAT [=] {DEFAULT|DYNAMIC|FIXED|COMPRESSED|REDUNDANT|COMPACT}
| TABLESPACE tablespace_name [STORAGE {DISK|MEMORY|DEFAULT}]
| UNION [=] (tbl_name[,tbl_name]...)
总之:
create table 表名 (字段1 datatype 修饰符,字段2 datatype 修饰符......)table_option
例1
MariaDB [testdb]> create table tbl1 (id int not null,name varchar(100) not null,age tinyint);
Query OK, 0 rows affected (0.09 sec)
例2
MariaDB [testdb]> create table products (prod_id char(10) not null,vend_id char(10) not null,prod_name varchar(254) not null,prod_price decimal(8,2) not null,prod_desc varchar(1000));
Query OK, 0 rows affected (0.07 sec)
查看表结构
MariaDB [testdb]> desc products;
+------------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+---------------+------+-----+---------+-------+
| prod_id | char(10) | NO | | NULL | |
| vend_id | char(10) | NO | | NULL | |
| prod_name | varchar(254) | NO | | NULL | |
| prod_price | decimal(8,2) | NO | | NULL | |
| prod_desc | varchar(1000) | YES | | NULL | |
+------------+---------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
查看创建表时使用的命令:
MariaDB [testdb]> show create table tb2;
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb2 | CREATE TABLE `tb2` (
`id` smallint(5) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
`age` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`id`,`name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+-------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
MariaDB [testdb]> show create table students;
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| students | CREATE TABLE `students` (
`id` smallint(5) unsigned NOT NULL,
`name` varchar(20) NOT NULL,
`age` tinyint(3) unsigned DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
查看表状态的
MariaDB [testdb]> show table status like 'students'\G
*************************** 1. row ***************************
Name: students
Engine: InnoDB
Version: 10
Row_format: Compact
Rows: 0
Avg_row_length: 0
Data_length: 16384
Max_data_length: 0
Index_length: 0
Data_free: 0
Auto_increment: NULL
Create_time: 2017-11-16 15:41:18
Update_time: NULL
Check_time: NULL
Collation: latin1_swedish_ci
Checksum: NULL
Create_options:
Comment:
1 row in set (0.00 sec)
2)删除表
MariaDB [testdb]> help drop table
Name: 'DROP TABLE'
Description:
Syntax:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
MariaDB [testdb]> drop table tbl1;
Query OK, 0 rows affected, 1 warning (0.03 sec)
注意:数据库、表删除是不可逆的
3)修改表
MariaDB [testdb]> help alter table
Name: 'ALTER TABLE'
Description:
Syntax:
ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name
[alter_specification [, alter_specification] ...]
[partition_options]
alter_specification:
table_options
添加字段:add
添加一个字段:
| ADD [COLUMN] col_name column_definition-------------------添加一个字段
[FIRST | AFTER col_name ]
| ADD [COLUMN] (col_name column_definition,...)------------------添加多个字段
| ADD {INDEX|KEY} [index_name]----------------------添加索引
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]] PRIMARY KEY
[index_type] (index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
UNIQUE [INDEX|KEY] [index_name]
[index_type] (index_col_name,...) [index_option] ...
| ADD FULLTEXT [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD SPATIAL [INDEX|KEY] [index_name]
(index_col_name,...) [index_option] ...
| ADD [CONSTRAINT [symbol]]
FOREIGN KEY [index_name] (index_col_name,...)
reference_definition
修改字段:
案例:
MariaDB [testdb]> alter table students change id sid smallint unsigned not null;
Query OK, 0 rows affected, 1 warning (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 1
| ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT}----------修改表字段的default字符集
| CHANGE [COLUMN] old_col_name new_col_name column_definition------------------change把列名改名,并设置列的新选项
[FIRST|AFTER col_name]
| MODIFY [COLUMN] col_name column_definition---------------modify把一个字段的老定义改成新定义
[FIRST | AFTER col_name]
删除字段:drop
| DROP [COLUMN] col_name-------删除某个键
| DROP PRIMARY KEY--------删除主键
| DROP {INDEX|KEY} index_name----------删除唯一键名
| DROP FOREIGN KEY fk_symbol---------删除外键
| MAX_ROWS = rows
| DISABLE KEYS
| ENABLE KEYS
| RENAME [TO|AS] new_tbl_name
| ORDER BY col_name [, col_name] ...
| CONVERT TO CHARACTER SET charset_name [COLLATE collation_name]
| [DEFAULT] CHARACTER SET [=] charset_name [COLLATE [=] collation_name]
| DISCARD TABLESPACE
| IMPORT TABLESPACE
| FORCE
| ADD PARTITION (partition_definition)
| DROP PARTITION partition_names
| TRUNCATE PARTITION {partition_names | ALL}
| COALESCE PARTITION number
| REORGANIZE PARTITION [partition_names INTO (partition_definitions)]
| ANALYZE PARTITION {partition_names | ALL}
| CHECK PARTITION {partition_names | ALL}
| OPTIMIZE PARTITION {partition_names | ALL}
| REBUILD PARTITION {partition_names | ALL}
| REPAIR PARTITION {partition_names | ALL}
| PARTITION BY partitioning_expression
| REMOVE PARTITIONING
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
table_options:
table_option [[,] table_option] ... (see CREATE TABLE options)
partition_options:
(see CREATE TABLE options)
为表增加一个字段
MariaDB [testdb]> ALTER TABLE products ADD vend_phone CHAR(20);
删除一个表的某个字段
MariaDB [testdb]> alter table products drop vend_phone ;
Query OK, 0 rows affected, 1 warning (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 1
添加一个唯一键
MariaDB [testdb]> alter table students add unique key(name);
Query OK, 0 rows affected (0.07 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB [testdb]> desc students
-> ;
+--------+----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------------------+------+-----+---------+-------+
| sid | smallint(5) unsigned | NO | PRI | NULL | |
| name | varchar(20) | NO | UNI | NULL | |
查看表中的索引
MariaDB [testdb]> show index from students;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| students | 0 | PRIMARY | 1 | sid | A | 0 | NULL | NULL | | BTREE | | |
| students | 0 | name | 1 | name | A | 0 | NULL | NULL | | BTREE | | |
| students | 1 | age | 1 | age | A | 0 | NULL | NULL | YES | BTREE | | |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)
创建索引
MariaDB [testdb]> help create index
Name: 'CREATE INDEX'
Description:
Syntax:
CREATE [ONLINE|OFFLINE] [UNIQUE|FULLTEXT|SPATIAL] INDEX index_name
[index_type]
ON tbl_name (index_col_name,...)
[index_option] ...
index_col_name:
col_name [(length)] [ASC | DESC]
index_type:
USING {BTREE | HASH}
index_option:
KEY_BLOCK_SIZE [=] value
| index_type
| WITH PARSER parser_name
| COMMENT 'string'
SQL语句之数据定义语言(DDL)详解的更多相关文章
- MySQL之数据定义语言(DDL)
写在前面 本文中 [ 内容 ] 代表啊可选项,即可写可不写. SQL语言的基本功能介绍 SQL是一种结构化查询语言,主要有如下几个功能: 数据定义语言(DDL):全称Data Definition L ...
- ODPS SQL <for 数据定义语言 DDL>
数据定义语言:(DDL) 建表语句: CREATE TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment ...
- <MySQL>入门三 数据定义语言 DDL
-- DDL 数据定义语言 /* 库和表的管理 一.库的管理:创建.修改.删除 二.表的管理:创建.修改.删除 创建:create 修改:alter 删除:drop */ 1.库的管理 -- 库的管理 ...
- SQL语句整理(二) 数据定义语言DDL
前言: 这是我学数据库时整理的学习资料,基本上包括了所以的SQL语句的知识点. 我的教材是人大王珊老师的<数据库系统概论>. 因为是手打的,所以会用一些细节打错了,但都挺明显也不多(考完试 ...
- 30441数据定义语言DDL
数据定义:指对数据库对象的定义.删除和修改操作. 数据库对象主要包括数据表.视图.索引等. 数据定义功能通过CREATE.ALTER.DROP语句来完成. 按照操作对象分类来介绍数据定义的SQL语法. ...
- SQLite基础-4.数据定义语言(DDL)
目录 一.创建数据库 1. 创建方式 2. 数据库命名规范 二. 创建表 1. 基本用法 2. 数据表命名规范 3. 字段命名规范 三. 删除表 一.创建数据库 1. 创建方式 在第二章中我们讲了如何 ...
- mysql数据库-mysql数据定义语言DDL (Data Definition Language)归类(六)
0x01 创建数据库并指定字符集和排序规则 -- 三种实例写法 create database temptab2 character set utf8 collate utf8_general_ci; ...
- 操作 SQL语句之数据操作语言(DML)详解
一.插入数据(INSERT) MariaDB [(none)]> help insertName: 'INSERT'Description:Syntax:INSERT [LOW_PRIORITY ...
- 【MySQL笔记】数据定义语言DDL
1.创建基本表 create table <表名> (<列名><数据类型>[列级完整性约束条件] ...
随机推荐
- Linux基础知识之文件的权限(一)
Linux基础知识之文件权限(一) Linux优点之一就是它拥有多用户多任务的环境,在提供文件共享的同时也能保证用户文件的安全性.所以,设置文件的权限管理变得尤为重要. 权限讲解 [der@Der ~ ...
- python主要模块 和 常用方法简览
******************** PY核心模块方法 ******************** os模块: os.remove() 删除文件 os.unlink() 删除文件 os.rename ...
- 洛谷 P2756 飞行员配对方案问题 (二分图/网络流,最佳匹配方案)
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其 ...
- python 中的GIL (全局解释器锁)详解
1.GIL是什么? GIL全称Global Interpreter Lock,即全局解释器锁. 作用就是,限制多线程同时执行,保证同一时间内只有一个线程在执行. GIL并不是Python的特性,它是在 ...
- 如何用win10自带linux系统学习c语言---解决gdb使用问题
1.windos store---ubuntu18 2.改安装源 3.装gcc apt-install 即可 4.装gdb apt-instll 即可 5.写helloworld 记做 ...
- springmvc处理一个请求的全流程
首先,用户的浏览器发出了一个请求,这个请求经过互联网到达了我们的服务器. Servlet 容器首先接待了这个请求,并将该请求委托给 DispatcherServlet 进行处理. 接着 Dispatc ...
- <<代码大全>>阅读笔记之二 变量名的力量
1.变量命名的注意事项 1)可理解性 变量要望文知义,看到这个变量不用看其他的代码就知道这个变量表示什么意思 好的变量命:currentDate, heartRate 糟糕的变量名:newButton ...
- Serializable(转)
最近在阅读JDK源码中的集合,看到很多集合类实现了Serializable接口,Cloneable接口.在阅读了很多关于Serializable接口的博客后,浅谈下我对Serializable接口的理 ...
- 10 masterless、高可用、salt执行模块开发、sydic架构
1.salt无master 官方文档: http://docs.saltstack.cn/topics/tutorials/quickstart.html 1.使用场景 1.在项目中使用salt,写一 ...
- Noip2016 提高组 Day2 T1 组合数问题
题目描述 组合数表示的是从n个物品中选出m个物品的方案数.举个例子,从(1,2,3) 三个物品中选择两个物品可以有(1,2),(1,3),(2,3)这三种选择方法.根据组合数的定 义,我们可以给出计算 ...