DDL


DDL语句:定义语言的缩写,也就是数据库内部的对象进行创建、删除、修改等操作的语言。和DML语句的最大区别是DML只是对表内部数据操作,而不涉及表的定义,结构的修改,更不会涉及到其他对象。

数据库:

create database bookstore;//创建数据库
drop database bookstore;//删除数据库

在命令行创建数据库指定编码:

CREATE DATABASE db_gz DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

表:

语法:
CREATE
TABLE table_name( column_name1 datatype1 [constraint_condition1], column_name2 datatype2 [constraint_condition2], ..... )
create table books (/*建表*/ book_id int, title varchar(50), author varchar(50) );
drop table tablename;//删除表

修改表:

//向表中添加一项
ALTER TABLE table_name ADD(column_name datatype [constraint_condition])
//修改表的某一项:
ALTER TABLE table_name MODIFY column_name datatype...
ALTER TABLE table_name CHANGE column_name column_name datatype...
//删除表中某一项:
ALTER TABLE table_name DROP column_name
//改表名
ALTER TABLE table_name rename newtable_name


alter table emp add column age int(3);/*增加age字段*/
alter table emp drop column age;/*删除age字段*/
alter table emp change age age1 int(4);/*将age改名为age1,且数据类型更改为int(4)*/
ALTER TABLE T_student MODIFY sex CHAR(2)/*修改数据类型例子*/
alter table emp rename emp1;/*更改表名为emp1*/

change和modify的区别:
/*change 和modify都可以修改表的定义,不同的是change后面需要写两次列名,不方便。但是change的有点是可以修改列名称,modify则不能*/

after和first的使用:
  直接使用add column默认增加到最后一列
  使用first关键字可以让指定列在最前面
  使用after关键字可以让指定列在某列后面
alter table emp modify age int(3) first;/*修改age列放到最前面*/
alter table emp add birth date after ename;/*增加birth列在ename后面*/

约束:

[constraint_condition1]: 指定列完整性约束条件  唯一、主、外、检查、非空
完整性约束条件可以定义在列级上,也可以定义在表级上。列级完整性约束指的是在定义列和指定列中数据类型之后就定义完整性约束条件;表级的完整性约束指的是在定义了所有咧之后在定义完整性的约束条件。

/*非空约束
  not null 不允许插入空值,只能用来约束列
      在建表的时候创建:
               create table tab(id int not null,name varchar(20) not null);
*/
create table tabA
(
 id int not null
)
insert into tabA values(null);-- 报错不能插入空值
drop table tabA;
/*唯一约束
unique  保证某列的值唯一,允许为空,这里注意:是允许多个空值。就是空值的不唯一
      在建表的时候创建:
               create table tab(name varchar(50) unique);     
*/
create table tabA
(
 name varchar(50) unique       
);
insert into tabA values('sag'); -- 不能重复插入了
/*
主键约束
primary key 保证使用主键约束的某一列或者一组列中的值唯一,不能为空,不能包含空,在表中只能有一个主键约束
        在建表的时候创建
                create table tab(id int primary key);
                create table tab(id int,name varchar(20), primary key(id,name));
*/
create table tabA
(
 id int primary key
)
create table tabB
(
 id int,
 name varchar(20),
 primary key(id,name)
)
insert into tabb (id,name) values (1,'sa');
insert into tabb values(null,'sb'); -- 可以看到不能插入null值
insert into tabb values(1,'sa'); --也不能插入相同的值
drop table tabb;

/*检查约束  注意:mysql检查约束无效
check 用来限制列的取值范围或者取值条件
      在建表的时候创建
      create table tabA(id int,name varchar(20),sex varchar(2),check(sex in ('男','女'));
      create table tabA(id int,name varchar(20),sex varchar(2) check(sex in ('男','女'));
*/
create table tabA
(
 id int check(id>3 and id<5)
);
insert into tabA values(1);
create table tabB
(
 sex varchar(2),
 check(sex in ('',''))
);

/*外键约束 
foreign key 建立主从表关联
        语法格式:
        foreign key[表名](列名1) references 表名2(列名2)
        [on update [cascade]|[ser null]|[restrict]]
        [on delete [cascade]|[set null]|[restrict]]
        on update和ondelete分别制定了在对表中的数据做修改和删除时,主从表要采取的操作方式,可选操作
           以删除为例子,三种操作方式讲解下:
           cascade:级联删除。如果主表中一条数据被删除,那么从表中与之对应的数据也将被删除
           set null:置空删除。如果主表中的一条数据被删除,那么从表中与之对应的数据将被设置为空值
           restrict:受限删除。如果主表中的一条数据被删除,则在执行delete命令时数据库管理会报错,通知用户与主表对应的从表中的相对应的数据仍然存在,报错,删除失败。这是默认的方式
           update的时候也是这样子的。
       在建表的时候创建
       create table tabA(stuid int,name varchar(20),foreign key(stuid) references student(stuid) on delete casade) ;
*/
create table student
(
 id int primary key,
 name varchar(50),
 tea_id int,
 foreign key(tea_id) references teacher(id)
)
create table teacher
(
 id int primary key,
 name varchar(20)
)
insert into student values(1,'guozhen',1); -- 直接插入会受到外键约束限制而报错
insert into teacher values(1,'xionggong');

drop table teacher; -- 先删从表再删主表,否则可能报错
drop table student;

--- 另一种方式添加约束,约束效果就不演示了
create table tabA
(
 id int,
 name varchar(20)
)
create table tabB
(
 id int,
 name varchar(20),
 a_id int
)

select * from taba;
select * from tabb;

alter table tabA add constraint primary key(id);

--非空约束好像这样玩会出问题,只能在建表的时候添加或者修改表项添加
alter table tabA add constraint is not null(name);
alter table taba modify name varchar(20) not null; -- 这样可以

--外键约束这样
alter table tabb add constraint fk_s foreign key (a_id) references taba(id);
--注意:有些时候我们误以为只有主键才可以作为其他表的外键,这个想法是错误的,其实,是只有该键有索引才可以作为其他表的外键
alter table taba add constraint fk_a foreign key (name) references tabb(name);--这个不会成功

alter table tabb add index tabb_index_name(name);--增加索引,增加成功索引之后就可以用来做外键了

--基本上所有约束都会自动生成索引,默认的索引名是你的约束名


删除一个约束条件:
ALTER TABLE table_name DROP constraint_type
ALTER TABLE T_dept DROP PRIMARY KEY//例子
alter table taba drop foreign key fk_a;//例子

索引:

索引
唯一标识,类似于数组中的下标,方便查询数据。

索引的分类:
唯一索引、主索引、复合索引、聚簇索引等等。

unique关键字创建唯一索引。

primary key关键字创建主索引

单列索引:
    定义在一个数据列上的索引就是单列索引。
复合索引:
    创建在多个列上的索引叫做复合索引
聚簇索引:
    为了提高SQL语言对数据表的查询效率,可以为数据表创建一个聚簇索引。聚簇索引中索引项的顺序与数据表中数据集的物理顺序保持一致。一个聚簇索引在一个数据表中只能创建一个。

一般数据库中对数据表中的索引数量给予限制。

创建与删除索引:
创建索引:
CREATE [UNIQUE]|[CLUSTRE] INDEX索引名
ON 表名(列名[排序方式]...)
其中UNIQUE表示创建的索引是唯一索引;关键字CLUSTER表示创建的索引是聚簇索引,这两个索引都是可选的 ASC表示升序DESC表示降序

CREATE INDEX i_profession ON T_teacher(profession)//单列索引
CREATE INDEX i_dept_profession ON T_teacher(dept,profession)//复合索引
CREATE INDEX i_salary ONT_teacher(salary ASC)//使用排序

删除索引:
DROP INDEX 索引名
DROP INDEX i_profession

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值