过滤连表查询
select student.stuname,score.score,score.courseid from student, score
where student.stuname="jack" and student.stuid=score.stuid;
select student.stuname,score.score,score.courseid
from student join score
on student.stuid=score.stuid
where score.score >80;
一、数据的完整性
1.实体的完整性
实体:表中的一行或者一条记录代表一个实体
实体完整性的作用:标识每一行数据不重复
约束类型:
主键约束【primary key】
唯一约束【unique】
自动增长列【auto_increment】
1.1 主键约束【primary key】
特点:数据唯一,且不能为null
主键可以是表中的一个字段或者多个字段,它的值用来唯一标识表中的某一条记录
场景:在多个表的关联关系中
#方式一
mysql> create table stu1(
-> id int primary key,
-> name varchar(50)
-> );
#方式二
mysql> create table stu2(
-> id int,
-> name varchar(50),
-> primary key(id,name)
-> );
#方式三
mysql> create table stu3(
-> id int,
-> name varchar(50)
-> );
mysql> alter table stu3 add constraint stu3_id primary key(id);
1.2 唯一约束
作用:在非主键列中不能输入重复的值
mysql> create table stu4(
-> id int primary key,
-> name varchar(50) unique
-> );
#primary key和unique之间的区别
a.二者都强调的是唯一性
b.在同一个表中,一般只出现一个primary