DQL(数据查询语言)(简单的)
DQL:Data Query Language
作用:查询数据
常用关键字:SELECT
创建一个学生表
create table stu
(
id int,
name varchar(30),
sex char(2),
age int,
address varchar(50)
);
insert into stu values(1,'张无忌','男',20,'北京');
insert into stu values(2,'小龙女','女',18,'古墓');
insert into stu values(3,'黄蓉','女',15,'桃花岛');
insert into stu values(4,'韦小宝','男',24,'扬州');
insert into stu values(5,'乔峰','男',34,'雁门关');
insert into stu values(6,'张果老','男',34,'雁门关');
insert into stu values(7,'老张','男',38,'黑木崖');
insert into stu values(8,'黄药师','男',34,'桃花岛');
insert into stu values(9,'张','女',34,'桃花岛');
insert into stu values(10,'张','男',34,'桃花岛');
查看所有的数据
select * from stu;
查看小龙女的信息
select * from stu where id = 2;
select * from stu where name='小龙女';
查看年龄在20~30之间的人
select * from stu where age >= 20 and age <= 30;
//包括20和30
//select * from stu where age between 20 and 30;
查看所有人的姓名
select name from stu;
查看所有人的姓名,年龄,性别
select name,age,sex from stu;
模糊查询
select * from 表名 where 字段名 like 字段表达式
%表示任意字符数
_表示任意的一个字符
查询所有以“张”开头的人
select * from stu where name like '张%';
查询姓名中含有“张”这个字的人
select * from stu where name like '%张%';
查询姓名中含有“张”这个字并且姓名的长度是3个字的人
select * from stu where name like '张__' or name like '_张_' or name like '__张';
过滤重复的数据
查询表中总共有几种性别
select distinct sex from stu;
查询姓名和年龄整体都不同的记录
select distinct name,set from stu;
字段可以有表达式
创建新表分数表
create table score
(
id int,
sid int,
china int,
english int,
history int
);
insert into score values(1,1,68,55,81);
insert into score values(2,3,86,23,34);
insert into score values(3,4,34,87,76);
insert into score values(4,6,98,99,93);
insert into score values(5,7,24,39,66);
把每人的china加上10分
select id,china+10,english,history from score;
给字段起别名
as 可以省略
select id as 编号,china as 语文,english as 英语,history 历史 from score;
查看所有人考试的总分
select id,china + english + history 总分 from score;
查看所有总分大于200的人
select * from score where china+english+history > 200;
查询家在桃花岛或者黑木崖的人
select * from stu where address in('桃花岛','黑木崖');
查询没有参加考试的人
select id,name from stu where id not in(select sid from score);
查询没有地址的人
select * from stu where address is null;
查询地址不是空的人
select * from stu where address is not null;
排序(order by)
对考试的人的语文升序排序
select * from score order by china asc;
对考试的人的历史降序排序
select * from score order by history desc;
根据多个字段进行排序(语文升序,对语文成绩一样的人再进行历史降序)
select * from score order by china asc,history desc;
根据考试总分降序排序
select * from score order by china + english + history desc;
数据完整性
为了保证插入到数据中的数据是正确的,防止用户可能存在的输入错误
- 实体完整性
- 主键:唯一的去区分每一条记录的一列或者多列的值。
- 特点:唯一,非空。
域完整性
指数据库表的列(即字段)必须符合某种特定的数据类型或约束。
参照完整性
保证一个表的外键和另一个表的主键对应
创建一个表
create table stu
(
id int primary key, #主键约束(唯一,非空)
name varchar(30) unique, #唯一约束(可以为空,但是只能有一个)
sex char(2) not null, #非空约束
age int check(age > 0 and age < 100), #检查约束(MySQL不支持)
address varchar(50) default '北京' #默认约束
)
再创建一个表和上表建立联系
create table score
(
id int primary key,
sid int,
china int,
english int,
history int,
constraint sid_FK foreign key(sid) references stu(id)
)
在表外创建引用约束
alter table score add constraint stu_score_FK foreign key (sid) references stu(id);
删alter table score drop foreign key stu_score_FK;d自动增长(系统帮你创建)
create table t
(
id int primary key auto_increment,
name varchar(20)
);
insert into t(name) values('张三');
注意:
- 添加记录时必须先添加主表中的记录,再添加子表中的数据
- 不能更改主表中具有外键约束的记录的主键
- 删除记录时不允许删除具有外键关系的主表中的记录(删除的顺序应当是先删除子表中的记录,然后删除主表中的记录)