文章目录
create view view_emp_10 as(select * from emp where deptno=10);
创建一个视图view_emp_10 (简单视图)with check option
即可create view v_emp_30 as(select * from EMP where deptno=30) with check option;
with check option
,那么我们就会插入成功,虽然不会在视图中显示,但是插入到原表中了,造成了视图数据污染create view view_emp_10 as(select * from emp where deptno=10);
create or replace view view_emp_10 as(select * from EMP where deptno=10 and sal>3000);
直接在create后面加上or replace
即可,有就替换if exists
如果存在就删除,不存在也不报错
drop view if exists view_emp_10;
create view view_1 as(select ename name from emp where deptno=10);
这里面的子查询将字段ename起了别名,那么我们在以后操作的时候只能使用别名对这个字段操作create or replace view view_emp_dept as(select d.dname,e.ename from EMP e join Dept d on d.deptno=e.deptno where e.sal<3000);
drop view if exists v_emp_dept;
create index 索引名 on 表名(字段名([长度]));
show index from 表名
drop index 索引名 on 表名
select * from item2 where title='100' and price <100000;
可以看出查询效率很高create table t_index(id int,age int ,index index index_age(age));
index 名字(字段)
where
,order by
,distinct
后面的字段创建索引 ,效果更好crate table t(id int ,age int unique);
create table t(id int primary key auto_increment,age int);
alter table t add primary key(id);
alter table t drop primary key;
create table t(id int primary key auto_increment,age int);
delete from t
) ,那么自增的值不会从头开始
truncate table t
的方式清空表,那么自增的值会从头开始,则从1开始
create table t(id int primary key auto_increment,deptid int,constraint 约束名 foregin key(deptid) references 关联的表名(关联表的字段名))
create table t_dept(id int primary key auto_increment,name varchar(10));
create table t_emp(id int primary key auto_increment,name varchar(10),deptid int,constraint fk_dept foreign key(deptid) references t_dept(id));
create table t(id int primary key auto_increment,age int not null default 0);
设置字段age设置默认值为0 ,如果插入数据的时候没有插入age的值,那么默认赋值为0create table t_check(id int,age int,check(age>10));