操作对象:数据库

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)详解的更多相关文章

  1. MySQL之数据定义语言(DDL)

    写在前面 本文中 [ 内容 ] 代表啊可选项,即可写可不写. SQL语言的基本功能介绍 SQL是一种结构化查询语言,主要有如下几个功能: 数据定义语言(DDL):全称Data Definition L ...

  2. ODPS SQL <for 数据定义语言 DDL>

    数据定义语言:(DDL) 建表语句: CREATE TABLE [IF NOT EXISTS] table_name [(col_name data_type [COMMENT col_comment ...

  3. <MySQL>入门三 数据定义语言 DDL

    -- DDL 数据定义语言 /* 库和表的管理 一.库的管理:创建.修改.删除 二.表的管理:创建.修改.删除 创建:create 修改:alter 删除:drop */ 1.库的管理 -- 库的管理 ...

  4. SQL语句整理(二) 数据定义语言DDL

    前言: 这是我学数据库时整理的学习资料,基本上包括了所以的SQL语句的知识点. 我的教材是人大王珊老师的<数据库系统概论>. 因为是手打的,所以会用一些细节打错了,但都挺明显也不多(考完试 ...

  5. 30441数据定义语言DDL

    数据定义:指对数据库对象的定义.删除和修改操作. 数据库对象主要包括数据表.视图.索引等. 数据定义功能通过CREATE.ALTER.DROP语句来完成. 按照操作对象分类来介绍数据定义的SQL语法. ...

  6. SQLite基础-4.数据定义语言(DDL)

    目录 一.创建数据库 1. 创建方式 2. 数据库命名规范 二. 创建表 1. 基本用法 2. 数据表命名规范 3. 字段命名规范 三. 删除表 一.创建数据库 1. 创建方式 在第二章中我们讲了如何 ...

  7. mysql数据库-mysql数据定义语言DDL (Data Definition Language)归类(六)

    0x01 创建数据库并指定字符集和排序规则 -- 三种实例写法 create database temptab2 character set utf8 collate utf8_general_ci; ...

  8. 操作 SQL语句之数据操作语言(DML)详解

    一.插入数据(INSERT) MariaDB [(none)]> help insertName: 'INSERT'Description:Syntax:INSERT [LOW_PRIORITY ...

  9. 【MySQL笔记】数据定义语言DDL

    1.创建基本表   create table <表名> (<列名><数据类型>[列级完整性约束条件]                                 ...

随机推荐

  1. 前端基础(九):SweetAlert(弹出框)

    简介 SweetAlert是一款很好用的弹出框框架 下载 点我下载 导入 博主用的是bootstrap-sweetalert,所以要依赖bootstrap,导入前先导入原生jQuery以及bootst ...

  2. Linux学习笔记(四)Linux常用命令:帮助命令

    一.帮助命令man [man] [命令] 例如: man ls man的级别 man -f [命令]  相当于  whereis [命令] 可查看该命令有几个等级,进而可以通过 man [等级数] [ ...

  3. javascript只弹出一次框 再次刷新不弹出

    .打开页面自动弹出  当关闭弹框的时候  设置cookie生存时间  再次刷新页面判断cookie是否失效 <html> <head> <meta charset=&qu ...

  4. ThreadPoolExecutor源码分析二

      接上文,这里继续分析源码 private static final int COUNT_BITS = Integer.SIZE - 3; private static final int CAPA ...

  5. VMware ESXi 和 VMware Server 有区别

    VMware ESXi 和 VMware Server 有区别: VMware ESXi 是一个企业级虚拟机管理程序,提供接近本机性能的祼机体系结构.各种旨在提高整合率的功能(例如取消内存复制),以及 ...

  6. MyBatis日期用法技巧

    当你想在实体类中使用Java.util.Date类型,而且还想在数据库中保存时分秒时,你可以在xml中修改为: #{xxdate,jdbcType=TIMESTAMP} 就是将#{}中的jdbcTyp ...

  7. Java8-Stream-No.06

    import java.io.IOException; import java.math.BigDecimal; import java.util.Arrays; import java.util.s ...

  8. QT 设置应用程序图标和可执行程序图标

    1, 首先准备个ICO图标.例如:myappico.ico 在工程目录下新建images文件夹并你的图标myappico.ico(只能用ico格式的图片)放到工程目录下的images文件夹下 2, 用 ...

  9. React组件式编程Demo-用户的增删改查

    1.项目结构 项目是基于webpack4, 参考 创建基本的webpack4.x项目 2.页面效果 初始化效果

  10. js上传文件夹

    用过浏览器的开发人员都对大文件上传与下载比较困扰,之前遇到了一个php文件夹上传下载的问题,无奈之下自己开发了一套文件上传控件,在这里分享一下.希望能对你有所帮助.此控件PC全平台支持包括mac,li ...