基本索引 index ...on
1、创建索引(index)create index 索引名称 on 表名(字段名);create index aa on student(name);2、查看student表中的索引show index from student;3、删除student 表中的索引alter table student drop index aa;
唯一索引 unique index on
对应的字段的值必须是唯一的,允许有空值create unique index aa on emp(sid); ===》为emp表中的sid创建索引aaalter table emp drop index aa; ===>删除索引aa
视图 view...as
create view new_view as (select sid,name,incoming from emp);
删除视图
drop view new_view;
show create view new_view; -- 查看视图
添加主键索引:只需要给字段添加主键就行
alter table emp drop primary key; -- 删除主键
alter table emp chang id id int(4) primary key; -- 添加主键
alter table emp add primary key(id); -- 添加主键
主键只有一个,但可以组合主键
alter table student add primary key(id,name);
修改视图
ALTER VIEW temp as (SELECT * from dept);
创建一个视图,就像是查询出来的内容一样
视图的特点:
1、视图就是一张需表
2、修改视图会影响基础表
3、修改基础表会影响视图
4、修改视图的语句和修改表的语句一样
外键的作用:保持数据的一致性、完整性
foreign key ==》使用外键来约束两个表之间的关系,防止出现主表中没有数据,子表又出现数据的情况
特点:
1要删除主表中的数据,需要先删除子表中相关联的所有数据
2.要往子表中插入数据,必须主表中有存在相关联的数据
创建外键的语句:
create table aa1(id int(4) primary key, name varchar(10)) engine =innodb; -创建子表
create table aa2 (sid int(4) primary key ,score int(4) ,constraint ss foreign key (sid) references aa1(id)) engine=innodb;--创建父表
创建外键语句结构:
(建表语句+ constraint外键名foreign key (子表字段) references 主表名(主表字段) ) engine=innodb;
engine=innodb ==》表示修改数据库引擎为innodb,因为innodb才支持外键
查看建表语句的过程:
show create table aa;
|
|