一、ALIAS (别名)
格式:表名 AS 表的别名,列名 AS列的别名;
select 表1别名.列1,表2别名.列2 from 表1 as 表1别名,表2 as 表2别名;eg:select stu.id,tea.name from student as stu,teacher as tea;
二、SQL关键字
1.BETWEEN 关键字
在某个范围内:select *from 表名 where 条件列 between 值1 and 值2;
eg:select *from student where id between '901' and '905';
不在某个范围内:select *from 表名 where 条件列 not between 值1 and 值2;
eg:select *from student where id not between '903' and '905';
tip:MYSQL在between...and中包含边界值,在not中不包含
2.[INNER] JOIN 关键字
select*from 表1 [inner]join 表2 on 表1.id=表2.id;
eg:select*from student inner join teacher on student.id=teacher.id;
tip:左右表都匹配则返回
3.LEFT JOIN
select*from 左表 left join 右表 on 左表.id=右表.id;
eg:select*from student left join teacher on student.id=teacher.id;
tips:a)左表所有数据都返回到结果中;
b)若右表无法匹配左表的数据,右表用null 进行配对.
4.RIGHT JOIN
select*from 左表 right join 右表 on 左表.id=右表.id;
eg:select*from student right join teacher on student.id=teacher.id;
tips:a)右表所有数据都返回到结果中;
b)若左表无法匹配右表的数据,左表用null 进行配对.
5.FULL JOIN
select*from 表1 full join 表2 on 表1.id=表2.id;
eg:select*from student full join teacher on student.id=teacher.id;
tip:只要其中一个表匹配,就返回,只有5.1以上才能使用这个全联接功能;
6.UNION 关键字
格式:
(select 列 from 表) UNION [ALL] (select 列 from 表);
eg:(select id from etudent) UNION [ALL] (select name from teacher);
tips:a)两次查询子句返回的结果集的列数必须一样,类型相似。
b)union会自动去掉重复行,不想去掉就在UNION后面加上ALL。
c)如果查询字句中没有order by,limit,可以不使用()。
d)在联合语句的查询子句中如果要使用ORDER BY进行排序,那么必须跟上LIMIT限定,否则ORDER BY。
e)返回结果集的字段名,是用第一个查询语句的字段名来命名。
f)用于将两个查询逻辑完全相反的结果集合并到一起。
三、SQL 约束
1.NOT NULL
create table 表名(字段名 字段类型 not null);
eg:create table student(id int(20) not null,name varchar(50));
2.UNIQUE
添加:create table 表名(字段名 字段类型,...,unique(id));
create table student(id int(20) not null,name varchar(50),unique(id));
追加:单列:alter table 表名 add unique(列);
eg:alter table student unique (id);
多列:alter table 表名 add constraint unique 约束名 unique(列名...);
eg:alter table student add constraint unique stu_tea unique(id,name);
tip:向表中添加唯一约束时,需注意这一列中是否已有重复值,若已有重复值,将无法添加唯一约束
删除:alter table 表名 drop index unique 约束名;
eg:alter table student drop index unique stu_tea;
tip:为多列设定unique约束,需多列全部重复,才会生效。
3.PRIMARY KEY(主键)
添加:create table 表名(字段名 字段类型,...,primary key(id));
create table student(id int(20) not null,name varchar(50),primary key(id));
追加:alter table 表名 add primary key(列);
eg:alter table student add primary key(id);
删除:alter table 表名 drop primary key;
eg:alter table student drop primary key;
tips:a)主键必须含唯一值(不能重复);
b)主键不能含有null值(第一次可不填,会自动补充默认值);
c)每个表只有一个主键
4.FOREIGN KEY(外键)
添加:create table 表名(字段1 字段类型1)foreign key(字段1)references 表2(表2字段名);
create table student(id int(20),foreign key(stu_id)references teacher(tea_id));
追加:alter table 表名 add foreign key(字段1)references 表2(表2字段名);
eg:alter table student add foreign key(stu_id)references teacher(tea_id);
删除:alter table 表名 drop foreign key 外键约束名;
5.CHECK 关键字
添加:create table 表名(字段名 字段类型,...,check(字段名>=值));
eg:create table student(id int(10),...,check(id<=50));
追加:单列:alter table 表名 add check(列>=值);
eg:alter table student check (id<=50);
多列:alter table 表名 add constraint 约束名 check(列>=值 and 列<=值);
eg:alter table student add constraint stu_tea_check check(id<=50and id>=0);
删除:alter table 表名 drop check 约束名;
6.DEFAULT 默认值
添加:create table 表名(字段名1 字段类型 default 默认值,字段名2 字段类型...);
create table student(id int(20) default null,name varchar(50));
追加:alter table 表名 alter 列名 set default 默认值;
eg:alter table student alter course set default ‘英语’;
删除:alter table 表名 alter 列名 drop default;
eg:alter table student alter course drop default;
tips:a)MySQL会给这些列自动加一个默认值为NULL的默认值;
b)如果追加默认值会覆盖掉前面设定的默认值;
c)若删除默认值,不会自动回到default NULL,而是没有了默认值,没有默认值那么我们插入数据的时候就一定需要插入这一列的数据,这个数据也可以是NULL。
四、VIEW视图:
约束用于限制介入表的数据的类型;
1.暴露部分数据给外部。
2.视图主要用于读取数据,因为插入数据要受到原来的表的限定。
3.视图实质上并不是一张表,尽管看起来一模一样,但是数据实质上市存在原来的表中的。
4.简化我们某些较为复杂的业务逻辑。
创建视图:create view 视图名 AS select statement(查询语句);
eg:create view v_stu_tea as select id,name from student where name='范冰冰';
修改视图:create view 视图名 AS select statement(查询语句);
eg:create view v_stu_tea select name from student where id='907';
删除视图:drop view 视图名;
eg:create view v_stu_tea;