MySQL语言分类——DDL
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的更多相关文章
- SQL语言分类DDL、DML、DQL、TCL、DCL
关系型数据库的SQL语句都可以分为4大类: 1. DDL(数据定义语言) DDL 主要是指如下的四种SQL 语句,以 CREATE.DROP.ALRET开头和 TRUNCATE TABLE 语 ...
- MySql 语言分类
(1)数据定义语言,即SQL DDL,用于定义SQL模式.基本表.视图.索引等结构.(2)数据操纵语言,即SQL DML.数据操纵分成数据查询和数据更新两类.(3)数据查询语言,即SQL DQL.(4 ...
- MySQL语言分类——DML
DML DML的全称是Database management Language,数据库管理语言.主要包括以下操作: insert.delete.update.optimize. 本篇对其逐一介绍 IN ...
- 07 MySQL_SQL语言分类
SQL语言分类 DDL Data Definition Language 数据定义语言 包括: create , alter ,drop , truncate; 不支持事务 DML Data Mani ...
- MySQL的sql语言分类DML、DQL、DDL、DCL、
MySQL的sql语言分类DML.DQL.DDL.DCL. SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语言DQL,数据控制语言DCL 1.数据定义语言DDL(Data ...
- mysql数据库语言分类
MySQL的sql语言分类DML.DQL.DDL.DCL. MySQL的sql语言分类DML.DQL.DDL.DCL. SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语 ...
- MySQL数据库之DDL(数据定义语言)
1.MySQL数据库之DDL创建.删除.切换 (1)查看所有数据库 show databases: (2)切换数据库 use 数据库名: (3)创建数据库 create database 数据库名: ...
- MySQL中的DDL,DML
MySQL中的DDL,DMLDDL:数据定义语言: CREATE,ALTER,DROP DB组件:数据库.表.索引.视图.用户.存储过程.存储函数.触发器.事件调度器等 CR ...
- MySQL操作之DDL
目录 SQL语句的分类 DDL语句 SQL语句的分类 DDL(Data Definition Languages)语句:数据定义语言.这些语句定义了不同的数据段. 数据库.表.列.索引等数据库对象的定 ...
随机推荐
- Springboot的entity,dao,controller,service层级理解
1.Dao层:持久层,主要与数据库交互 DAO层首先会创建Dao接口,接着就可以在配置文件中定义该接口的实现类:接着就可以在模块中调用Dao的接口进行数据业务的处理,而不用关注此接口的具体实现类是哪一 ...
- Unity检视面板的继承方法研究 (一)
对于检视面板 Inspector 的面板继承方式对项目来说是很有必要的, 比如一个基类, 写了一个很好看的检视面板[CustomEditor(typeof(XXX))], 可是所有子类的面板无法直接继 ...
- flask 基础2
一.装饰器的坑 在使用装饰器函数时候,当一个装饰器装饰多个函数的时候,会由于内存地址相同时发生报错,因为装饰的都是一个函数 所以就需要引入 import functools 重新定义每一个函数的名称 ...
- 使用spring jdbc遇到的一个性能问题
使用JdbcTemplate的queryForList方法,返回特别慢,40多万结果集耗时超过6分钟.双核CPU,占用率始终在50%,内存逐渐增长至2G左右. 进行debug跟进去看,看到jdbcTe ...
- 201871010110-李华《面向对象程序设计(java)》第十六周学习总结
博文正文开头格式:(2分) 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.co ...
- java实体 和 xml相互转换
参考: https://blog.csdn.net/LookForDream_/article/details/88884316 https://zhuchengzzcc.iteye.com/blog ...
- Spring Cloud微服务安全实战_4-1_微服务网关安全_概述&微服务安全面临的挑战
第四章 网关安全 这一章从简单的API的场景过渡到复杂的微服务的场景 4.1 概述 微服务安全面临的挑战:介绍中小企业的一个微服务架构,相比第三章的单体应用的简单的API所面临的哪些挑战 OAu ...
- Day01 确定选题
一起来选题 一.谁想个选题? 今天是第一节大软课,大家需要进行分组和确定选题.分组固然是快乐的,但是确定选题是让人费脑筋的.要新颖!要有需求!要我们能实现(笑)......大家面面相觑.面对这种情况, ...
- Linux学习笔记-第6天 - 问题的根本
这些知识其实看起来很简单,之前不管是在学习C语言还是bat批处理,类似结构早已熟知. 但其实运用起来并不算好,可能真正的原因还 是在于得多练习吧.希望明年的今天自己不要再纠结与这些基础性的知识.
- springboot模板(Freemarker与Thymeleaf)
Thymeleaf模板 Thymeleaf就是html页面 导入pom依赖 <dependency> <groupId>org.springframework.boot< ...