#*********数据库的操作*****(掌握)
#创建数据库
create database mydb;
#查看创建数据库的语句
show create database mydb;
#改变当前的数据库
use mydb;
#删除数据库
drop database mydb;
#查看所有的数据库
show databases;
#修改数据库mydb1的字符集为utf8
alter database mydb1 character set utf8;
#***了解******
#创建数据库mydb1,字符集用GBK
create database mydb1 character set gbk;
#查看数据库中所有的校对规则
show collation;
#查看中文的校验规则
show collation like '%gb';
#创建数据库mydb2,字符集用gbk,校验规则用gbk_bin
create database mydb2 character set gbk collate gbk_bin;
#----
--针对表的操作(掌握)
#创建表t
create table t(
id int ,
name varchar(30)
);
#查看创建表的源码
show create table t;
#创建表t1,使用字符集gbk
create table t1(
id int ,
name varchar(30)
)character set gbk;
#创建表t3
create table t3
(
id int ,
name varchar(30),
optime timestamp
);
#插入数据
#设置客户端的字符集为GBK
set character_set_client=gbk;
#设置结果集的字符集为gbk
set character_set_results=gbk;
insert into t3(id,name) values(1,'张无忌');
insert t3(id,name) values(2,'乔峰');
#省略字段,意味着所有的字段都必须给值(自增例外)
insert t3 values(3,'杨过','2016-4-19');
#更新
#将表t3的第三条记录姓名改为杨康
update t3 set name = '杨康' where id = '3';
#将所有记录的名字都改为东方不败
update t3 set name = '东方不败';
#修改多个字段
update t3 set id = 6,name = '萧峰' where id = 2;
#删除
delete from t3 where id = 3;
#删除所有的记录
delete from t3;
#删除所有的记录
truncate table t3;
#给表t3增加一个字段address
alter table t3 add address varchar(100);
#删除字段address
alter table t3 drop column address;
#查看表的结构
desc t3;
#创建一个学生表
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,'乔峰','男',32,'雁门关');
insert into stu values(6,'黄药师','男','45','桃花岛');
insert into stu values(7,'张果老','男',54,'扬州');
insert into stu values(8,'张','男',22,'大理');
insert into stu(id,name,sex,age) values(9,'令狐冲','男',27);
#查看所有的数据
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;
select * from stu where age between 20 and 30;#包括20和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 '__张';
#查询年龄在20-30之间的人
select * from stu where age like '2[0-9]' or age = 30;
#过滤重复地数据用distinct
#查询表中有几种不同的性别
select distinct sex from stu;
#查询表中姓名和性别都不同的人
select distinct name,sex from stu;
#创建新表分数表
create table score
(
id int,
sid int,
china int,
english int,
history int
) ;
insert into score values(1,1,68,54,81);
insert into score values(2,3,89,98,90);
insert into score values(3,4,25,60,38);
insert into score values(4,6,70,75,59);
insert into score values(5,8,60,65,80);
#字段表达式
select id,china+10,english,history from score;
#给字段起别名
select id as 编号,china as 语文,english as 英语,history as 历史 from score;
select id 编号,china 语文,english 英语,history 历史 from score;
#查看所有人考试的总分是多少
select id, china + english + history 总分 from score;
#查看总分大于200的人
select * from score where china + english +history >200;
#查询家在桃花岛或者大理的人
select * from stu where address = '桃花岛' or address = '大理';
select * from stu where address in ('桃花岛','大理');
#查询没有参加考试的人
select id ,name from stu where id not in(select sid from score);
#查询没有地址的人
select * from stu where address = null;#错误
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 *, china + english + history 总分 from score order by china + english +history desc;
#创建数据库
create database mydb;
#查看创建数据库的语句
show create database mydb;
#改变当前的数据库
use mydb;
#删除数据库
drop database mydb;
#查看所有的数据库
show databases;
#修改数据库mydb1的字符集为utf8
alter database mydb1 character set utf8;
#***了解******
#创建数据库mydb1,字符集用GBK
create database mydb1 character set gbk;
#查看数据库中所有的校对规则
show collation;
#查看中文的校验规则
show collation like '%gb';
#创建数据库mydb2,字符集用gbk,校验规则用gbk_bin
create database mydb2 character set gbk collate gbk_bin;
#----
--针对表的操作(掌握)
#创建表t
create table t(
id int ,
name varchar(30)
);
#查看创建表的源码
show create table t;
#创建表t1,使用字符集gbk
create table t1(
id int ,
name varchar(30)
)character set gbk;
#创建表t3
create table t3
(
id int ,
name varchar(30),
optime timestamp
);
#插入数据
#设置客户端的字符集为GBK
set character_set_client=gbk;
#设置结果集的字符集为gbk
set character_set_results=gbk;
insert into t3(id,name) values(1,'张无忌');
insert t3(id,name) values(2,'乔峰');
#省略字段,意味着所有的字段都必须给值(自增例外)
insert t3 values(3,'杨过','2016-4-19');
#更新
#将表t3的第三条记录姓名改为杨康
update t3 set name = '杨康' where id = '3';
#将所有记录的名字都改为东方不败
update t3 set name = '东方不败';
#修改多个字段
update t3 set id = 6,name = '萧峰' where id = 2;
#删除
delete from t3 where id = 3;
#删除所有的记录
delete from t3;
#删除所有的记录
truncate table t3;
#给表t3增加一个字段address
alter table t3 add address varchar(100);
#删除字段address
alter table t3 drop column address;
#查看表的结构
desc t3;
#创建一个学生表
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,'乔峰','男',32,'雁门关');
insert into stu values(6,'黄药师','男','45','桃花岛');
insert into stu values(7,'张果老','男',54,'扬州');
insert into stu values(8,'张','男',22,'大理');
insert into stu(id,name,sex,age) values(9,'令狐冲','男',27);
#查看所有的数据
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;
select * from stu where age between 20 and 30;#包括20和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 '__张';
#查询年龄在20-30之间的人
select * from stu where age like '2[0-9]' or age = 30;
#过滤重复地数据用distinct
#查询表中有几种不同的性别
select distinct sex from stu;
#查询表中姓名和性别都不同的人
select distinct name,sex from stu;
#创建新表分数表
create table score
(
id int,
sid int,
china int,
english int,
history int
) ;
insert into score values(1,1,68,54,81);
insert into score values(2,3,89,98,90);
insert into score values(3,4,25,60,38);
insert into score values(4,6,70,75,59);
insert into score values(5,8,60,65,80);
#字段表达式
select id,china+10,english,history from score;
#给字段起别名
select id as 编号,china as 语文,english as 英语,history as 历史 from score;
select id 编号,china 语文,english 英语,history 历史 from score;
#查看所有人考试的总分是多少
select id, china + english + history 总分 from score;
#查看总分大于200的人
select * from score where china + english +history >200;
#查询家在桃花岛或者大理的人
select * from stu where address = '桃花岛' or address = '大理';
select * from stu where address in ('桃花岛','大理');
#查询没有参加考试的人
select id ,name from stu where id not in(select sid from score);
#查询没有地址的人
select * from stu where address = null;#错误
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 *, china + english + history 总分 from score order by china + english +history desc;