DDL(Data definition language)
数据定义语言,用于描述数据库中要存储的现实世界实体的语言。
操作
数据库
建立,修改,删除:create/alter/dorp database
表
建立,修改,删除:create/alter/drop table
清空表:truncate table
重命名表:rename table
视图
建立,修改,删除:create/alter/drop view
索引
建立,删除:create/drop index
注意:当我们删除了这里的索引,不会对数据造成影响,但是,会对sql执行计划造成影响
关于视图
1, 什么是视图-view 视图是一个自定义的查询结果表文件记录 用视图简化大表查询难度 2, 创建视图 create view 视图名称 as 查询语句
视图的数据是脱离原表的 视图的数据也可以被增删改查,但是改了之后不影响原表数据 只能将查询结果作为视图创建出来 3,删除视图 drop view 视图名称 as 查询语句 4, 修改视图 alter view 视图名称 as 查询语句
5, 视图的作用 (1),面向视图查询,可以提高查询效率
(2),隐藏细节 面向视图查询,不知道该视图背后的查询的真实表是哪些。 注意:视图里面的select语句不能有子查询 |
关于表
清空表:tuncate table 表名 truncate table course;
![]() 重命名表:rename table 表名 to 新表名 rename table course to coursesee; |
创建表的三种模式
1,正常创建
2,在查询出的新表的基础上创建表 create table user3 as select * from s;
![]() 3,create table user4 like user3; |
修改表
1,删除表的多列 ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d; 2,修改表的引擎 ALTER TABLE t1 ENGINE = InnoDB; 1,更改字符集 ALTER TABLE t1 CHARACTER SET = utf8; 更多操作详见官网api: |
删除表
DROP TABLE IF EXISTS `t_customer`; 删除一个或多个表,你必须具有drop每个表的特权。 |
ALTER更改操作
//添加主键 alter table c add ind int(2) unsigned not null auto_increment,add primary key(ind);
![]() //添加一个新列 alter table tablename(表名) add fieIdname(列名) fieldtyle(列的类型) alter table c add cno int; //删除列 alter table table_name(表名) drop field_name(列名) 如:alter table c drop column cno; //修改原字段名称及类型 ALTER TABLE tablename CHANGE old_field_name new_field_name field_type; alter table s change sname studentname char(10);
![]() alter table s change studentname studentname varchar(100) not null default 'null';
![]() //重命名表 alter table c rename course;
![]() //添加索引 mysql>ALTER TABLE tablename ADD INDEX 索引名 (字段名1[,字段名2……]); alter table c add index ind_cteacher(cteacher);
![]() //添加关键字搜索 alter table s add primary key(sno);
![]() //加唯一限制条件的索引 alter table c add unique uni_ind(ind);
![]() //删除某个索引 alter table c drop index uni_cname;
![]() |