DDL的全称Data Definition Language,即数据定义语言

  DDL的语法有:create、alter、drop、rename、truncate。对此做一个详细的解释:

create (创建)

  create 可以创建数据库

# 创建数据库
create database database_name; # 然后进入该数据库
use database_name;

  create 可以创建表格

    创建表格的语法:方括号的表示可以省略

      create [temporary] table [if not exits] table_name(

        column_name data_type [not null | null] [default default_value] [auto_increment] [comment ""] [Constraints约束条件],

      );

    [if not exits] 如果新建表不存在,则会创建新表;如果存在,不会报错,数据也不会被覆盖

create table if not exits table_name(
id int,
name varchar(20)
);

    [temporary] 创建临时表 用于存储临时计算的数据,生命周期只限于本次连接

create temporary table table_name(
id int key,
name varchar(20)
);

    [not null | null] 字段值是否可以为空; 如果这个字段值设置不为空,后面还有默认值,那么在插入时当前字段不插入数据,会按照后面的默认值填入

create table null_test(
account varchar(20) not null,
password varchar(32) not null,
gender enum("0", "1") null
); insert into null_test(account) values("zhangsan"); # error 因为password不能为空
insert into null_test(account, password) values("zhangsan", "abcdef");

  [default default_value] 字段值的默认值是什么,建议项目中表格的每一列都给默认值(以防用户不填数据)

create table default_test(
id int not null default 0,
address varchar(50) default "",
gender enum("0", "1") null default "0"
); # 首先id不为空,插入时也没有写,在插入的过程中,会将默认值插进去
insert into default_test(address, gender) values ("kunshan", "1");

    [auto_increment] 字段值自动增加,一般(不是必须)适用于主键而且是int类型,默认从1开始

# auto_increment的三种表现形式
create table auto_test(
id int primary key auto_increment,
name varchar(20) not null
); create table auto_test(
id int primary key auto_increment,
name varchar(20) not null
)auto_increment=1; create table auto_test(
id int primary key auto_increment,
name varchar(20) not null
)auto_increment=2016211001000;

    [comment ""] 给表格字段添加备注

create table comment_test(
id int comment "标号"
);

  Constraints约束:

    约束是用来限制和保护表的数据符合我们定义的条件

约束的表现形式:

    列级约束

      写在列的后面,针对某个列的约束

        • Create table student(id number primary key,name varchar(10));

    表级约束

      写在建表语句的后边,针对某个列的约束

        • Create table student(id number , name varchar(10),primary key(id));

    列级约束和表级约束作用相同,但是表级约束功能更强一些,具体见后边

    如果是表级约束,若涉及联合唯一或者是联合主键,只有联合的所有的字段值一样的才会报错(局部可以重复,全局不可以重复)

    not null 上面已有介绍

    unique   字段值唯一

create table unique_test(
id int,
stuno int not null unique # 列级约束
); create table unique_test(
id int,
stuno int not null unique default 0 # 只能默认插入一次
); create table unique_test(
id int,
stuno int not null,
unique(stuno) # 表级约束
); create table unique_test(
id int,
stuno in not null,
unique(id, stuno) # 表级约束---联合约束唯一
); # 联合唯一:单个列可以重复,整体不能重复; 条件必须时表级约束,同时重复才会报错

    primary key(表格的主键)    非空 唯一 索引

create table primary_test(
id int primary key, # 列级约束
name char(20)
); create table primary_test(
id int,
name char(20),
primary key(id) # 表级约束
); create table primary_test(
id int,
name char(20),
primary key(id, name) # 表级约束 -----联合主键
);
# 联合唯一:单个列可以重复,整体不能重复; 条件必须时表级约束,同时重复才会报错

    foreign key(表格的外键)

      外键只能是表级约束,先有主表(与之关联的外键表),才有外表(当前表是外表)

      外表的引擎必须是InnoDB,在分号之前建议写上

      两者关联涉及的字段值类型必须相同

      删除数据(表),先删除子(外)数据(表),在删除父(主)数据(表)

      填写foreign key(字段名) references 主表名(主键)

# 主表(父表)
create table parent_table(
id int primary key, # 列级约束
name varchar(20) not null default "",
); # 外表(子表)
create table child_table(
id int,
parent_id int,
name varchar(20) not null default "",
primary key(id), # 表级约束
foreign key (parent_id) references parent_table(id) # 表级约束
); #插入数据: 先插入父表的数据,在插入子表的数据
insert into parent_table values(1001);
insert into child_table values(1, 1001);

    index/key 索引 建立索引提高查询速率

      key:如果表格没有主键,又是列级约束,会自动设置成主键;又是表级约束,就是普通的索引key了

      在已有的表格上创建/删除 索引:create/drop index index_name on table_name

# key 索引测试
create table key_test(
id int key, # 列级约束转换成主键了
name varchar(20)
); create table key_test(
id int,
name varchar(20),
key(id) # 表级约束 ---只是一个关键字索引
); create table key_test(
id int,
name varchar(20),
key(id, name) # 表级约束---联合约束
); #index 索引测试 create table index_test(
id int index, # index 没有列级约束 # error
name varchar(20)
); create table index_test(
id int,
name varchar(20),
index(id) # 表级约束 ---只是一个关键字索引
); create table index_test(
id int,
name varchar(20),
indexid, name) # 表级约束---联合约束
); # 在已有的表格创建索引
create index index_name on table_name(column_item1[, column_item2]);
# 在已有的存在索引字段的表格删除索引
drop index index_name on table_name(column_item1[, column_item2]);

    check(没有什么具体作用)

创建视图view(视图和相关联的表格是一致的):

create view view_name as 数据(某个结果数据集);

增加、删除、修改、查询(前提是操作成功)等功能和表格的一样

视图的作用:

  重用sql语句,即多次使用相同语句

  数据的安全性:限定特定字段数据

  对数据的重构并且不影响其他程序的运行,让数据更加清晰

# 创建一个视图(可以将视图比作望远镜)
# 即实物与望远镜看到的事物是一致的
create view view_name as select * from table_name; # 下面两个结果是一样的
select * from view_name;
select * from table_name;

DROP

  drop可以删除数据库、数据表、view视图、index索引

drop database database_name;
drop table table_name;
drop view view_name;
drop index index_name on table_name(column_item1[, column_item2]);

RENAME(重命名)

  rename只限于表格操作

# 修改表格名称
rename table old_name to new_name

TRUNCATE

  清空表格的数据,释放磁盘空间(自定义的设置会回复原来的设置)

  

create table truncate_test(
id int primary key auto_increment,
name varchar(20)
)auto_increment=10000; insert into truncate_test(name) values ("aaa"); truncate truncate_test; # 原来设置的auto_increment=10000,会改为auto_increment=1

ALTER

alter 完整语法的用法:

# 创建一个测试表格
create table test(id int);

  1、向表格增加一列:

# 语法:
#alter table table_name add column_name column_type column_constraints alter table test add name varchar(20) not null;
 2、向表格的一个/多个字段增加索引(多个字段即联合索引)
# 语法
# alter table table_name add index [unique_name](column_name1[,column_name2])
alter table test add index (id);

  3、向表格的一个/多个字段增加主键(多个字段即联合主键)

# 语法:
# alter table table_name add primary key (column_name1[,colum_name2]) alter table test add primary key (id);

  4、向表格的一个/多个字段增加唯一(多个字段即联合唯一)

# 语法:
# alter table table_name add unique [unique_name] (column_name1[, clumn_name2]) alter table test add unique (id);

  5、修改/删除表格某个字段的默认值

# 语法:
# alter table table_name alter column_name {set defaulle column_value |drop default} # 色湖之默认值
alter table test alter name set default "root"; # 删除默认值
alter table test alter name drop default;

  6、修改表格字段

# 语法:
# 修改字段过程中若连同字段名也修改采用change
# alter table table_name change column_old_name column_new_name [constraints] alter table test change id test_id int primary key; # 不修改字段名可采用modify
# alter table table_name modify column_name [constraints] alter table test modify test_id int(10) unsigned primary key;

  7、删除字段、主键、索引

# 语法:
# 删除某个字段
# alter table table_name drop column_name alter table test drop name; # 删除索引
# alter table table_name drop index index_name 默认是字段名,也可以查看表结构 alter table test drop index id; # 删除主键
# alter table table_name drop primary key alter table test drop primary key;

  8、修改表格的名称

# 修改表格的两种方式:
# 1、采用rename方式
# rename table table_old_name to table_new_name; rename table test to test1; # 2、采用alter方式
# alter table table_name rename [as] table_new_name alter table test1 rename test;

  9、修改表格的配置

# 语法
# 修改的地方是在 ) 和 ; 之间的配置 # 修改引擎
# alter table table_name engine=InnoDB alter table test engine=InnoDB; # 修改字符编码
# alter table table_name charset=gbk2313 alter table test charset=gbk2312;

  

MySQL语言分类——DDL的更多相关文章

  1. SQL语言分类DDL、DML、DQL、TCL、DCL

    关系型数据库的SQL语句都可以分为4大类: 1. DDL(数据定义语言)     DDL 主要是指如下的四种SQL 语句,以 CREATE.DROP.ALRET开头和 TRUNCATE TABLE 语 ...

  2. MySql 语言分类

    (1)数据定义语言,即SQL DDL,用于定义SQL模式.基本表.视图.索引等结构.(2)数据操纵语言,即SQL DML.数据操纵分成数据查询和数据更新两类.(3)数据查询语言,即SQL DQL.(4 ...

  3. MySQL语言分类——DML

    DML DML的全称是Database management Language,数据库管理语言.主要包括以下操作: insert.delete.update.optimize. 本篇对其逐一介绍 IN ...

  4. 07 MySQL_SQL语言分类

    SQL语言分类 DDL Data Definition Language 数据定义语言 包括: create , alter ,drop , truncate; 不支持事务 DML Data Mani ...

  5. MySQL的sql语言分类DML、DQL、DDL、DCL、

    MySQL的sql语言分类DML.DQL.DDL.DCL. SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语言DQL,数据控制语言DCL 1.数据定义语言DDL(Data ...

  6. mysql数据库语言分类

    MySQL的sql语言分类DML.DQL.DDL.DCL.   MySQL的sql语言分类DML.DQL.DDL.DCL. SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语 ...

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

    1.MySQL数据库之DDL创建.删除.切换 (1)查看所有数据库 show databases: (2)切换数据库 use 数据库名: (3)创建数据库 create database 数据库名: ...

  8. MySQL中的DDL,DML

    MySQL中的DDL,DMLDDL:数据定义语言:    CREATE,ALTER,DROP        DB组件:数据库.表.索引.视图.用户.存储过程.存储函数.触发器.事件调度器等    CR ...

  9. MySQL操作之DDL

    目录 SQL语句的分类 DDL语句 SQL语句的分类 DDL(Data Definition Languages)语句:数据定义语言.这些语句定义了不同的数据段. 数据库.表.列.索引等数据库对象的定 ...

随机推荐

  1. 用户交互Scanner的用法

    Java用户交互的目的是实现程序与人的交互:一般通过Scanner来获取用户的输入:java.util.Scanner 是Java5的新特征. 基本语法: Scanner s=new Scanner( ...

  2. Rust中的测试用例的写法

    有点类似 #[derive(Debug)] pub struct Rectangle { length: u32, width: u32, } impl Rectangle { pub fn can_ ...

  3. 201871010102-常龙龙《面向对象程序设计(java)》第十周学习总结

    项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...

  4. hadoop 源码编译

    hadoop 源码编译 1.准备jar 1) hadoop-2.7.2-src.tar.gz 2) jdk-8u144-linux-x64.tar.gz 3) apach-ant-1.9.9-bin. ...

  5. 查看ubuntu已安装软件

    查看安装的所有软件 dpkg -l “dpkg ”是“Debian Packager ”的简写.为 “Debian” 专门开发的套件管理系统,方便软件的安装.更新及移除.所有源自“Debian”的“L ...

  6. luoguP3172 [CQOI2015]选数

    题意 所求即为: \(\sum\limits_{i_1=L}^{R}\sum\limits_{i_2=L}^{R}...\sum\limits_{i_k=L}^{R}[\gcd(i_1,i_2,... ...

  7. mongo db 去除 _class 字段

    import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.NoSuc ...

  8. [LeetCode] 718. Maximum Length of Repeated Subarray 最长的重复子数组

    Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...

  9. 屏蔽flash地区识别

    host文件添加以下0.0.0.0 geo2.adobe.com

  10. LeetCode 220. Contains Duplicate III (分桶法)

    Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...