数据库sql语句规则

sql练习:

创建一个名称为mydb1的数据库。
create database mydb1;

查看库
show databases;

创建一个使用utf-8字符集的mydb2数据库。
create database mydb2 character set utf8;

查看上面创建的库的细节(是不是使用utf8)
show create database mydb2;

创建一个使用utf-8字符集,并带校对规则的mydb3数据库。
create database mydb3 character set utf8 collate utf8_general_ci

删除前面创建的mydb1数据库
drop database mydb1;

查看服务器中的数据库,并把其中某一个库的字符集修改为gb2312;
alter database mydb2 character set gb2312;

备份mydb2库中的数据,并恢复
 //为了演示效果,首先向库中保存一些数据
 use mydb2;
 create table user
 (
  name varchar(20)
 );
 insert into user(name) values('aaa');
 select * from user;
 
 //开始备份数据库
 quit(退出mysql客户端)
 mysqldump -u root -p mydb2 > c:\test.sql   (备份这条命令是在windows命令行执行)

 //为了演示恢复,首先删除库
 drop database mydb2;
 source恢复库,只能恢复库中的数据,不能恢复数据库,所以为了恢复,首先要建库
 create database mydb2;
 use mydb2;  (进入库再恢复)
 source c:\test.sql   (恢复这条命令是在mysql客户端执行,千万不要加;号)

查看所有的表
show tables;

创建一个员工表
  首先要进入库 use mydb2
 (注意:表声明语句的最后一行不能为,)

create table employee
(
 id int,
 name varchar(20),
 sex varchar(4),
 birthday date,
 entry_date date,
 job varchar(100),
 salary double,
 resume text
)character set utf8 collate utf8_general_ci;

在上面员工表的基本上增加一个image列
alter table employee add image blob;

查看表的创建语句
show create table employee;

查看表的结构(重要)
desc employee;

修改job列,使其长度为60。
alter table employee modify job varchar(60);

删除sex列。
alter table employee drop sex;

表名改为user。
rename table employee to user;

修改表的字符集为gb2312
alter table user character set gb2312;

查看改后的结果
show create table user;

列名name修改为username
alter table user change column name username varchar(30);

使用insert语句向表中插入三个员工的信息。
insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(1,'aaa','girl','1980-09-09',30,'1990-0-09','aaaaaaaaaaaaaa');

查看插入的数据
select * from employee;

向数据库插入中文数据
insert into employee(id,name,sex,birthday,salary,entry_date,resume) values(2,'李一','girl','1980-09-09',30,'1990-0-09','aaaaaaaaaaaaaa');

插入中文数据的解决办法
 1、首先查询出mysql涉及到字符编码的所有变量
  show variables like 'character%';
 2. 改变和客户端相关的变量的值
  set character_set_client=gb2312;
 3.成功插入中文
 
 4.取数据的乱码的解决
  set character_set_results=gb2312
  select * from employee;

将所有员工薪水修改为5000元。
update employee set salary=5000;

将姓名为’aaa’的员工薪水修改为3000元
update employee set salary=3000 where name='aaa';

将aaa的薪水在原有基础上增加1000元。 
update employee set salary=salary+1000 where name='aaa';

将id为1的所有都改一下
update employee set name='bbb', sex='男',salary=10000,birthday='1940-09-09' where id=1;

删除表中id为1的记录。
delete from employee where id=1;

删除表中所有记录。
delete from employee;

使用truncate删除表中记录。
truncate table employee;  (摧毁表,重建表结构)


查询练习:
导入脚本:source c:\Student.sql

查询表中所有员工的信息。
select * from student;
select id,name,chinese,english,math from student;

查询表中所有学生的姓名和对应的英语成绩。
select name,english from student;

过滤表中重复数据。
select distinct english from student;

在所有学生分数上加10分特长分显示。
select name,chinese+10,english+10,math+10 from student;


统计每个学生的总分。
select name,chinese+math+english from student;

使用别名表示学生分数。
select name as 姓名,chinese+math+english as 总分 from student;
select name 姓名,chinese+math+english 总分 from student;
select s.name,s.english from student s;   (为表取别名,并根据别名取列的数据(s.name))


查询姓名为wu的学生成绩
select * from student where name='黄蓉';

查询英语成绩大于90分的同学
select * from student where english>90;

查询总分大于200分的所有同学
select name from student where (english+chinese+math)>200;

查询英语分数在 80-90之间的同学。
select name from student where english>=80 and english<=90;
select name from student where english between 80 and 90;

查询数学分数为89,90,91的同学
select name from student where math=89 or math=90 or math=91;
select name from student where math in(89,90,91);

查询所有姓李的学生成绩。
select * from student where name like '李%';
select * from student where name like '李_';

查询数学分>80,语文分>80的同学。
select * from student where math>80 and chinese>90;

对数学成绩排序后输出。
select name,math from student order by math;
select name,math from student order by math desc;

对总分排序后输出,然后再按从高到低的顺序输出
select name from student order by (math+english+chinese) desc;

对姓李的学生成绩排序输出
select * from student where name like '李%' order by (math+english+chinese) desc;

统计一个班级共有多少学生?
select count(*) from student;

统计数学成绩大于90的学生有多少个?
select count(*) from student where math>90;

统计总分大于250的人数有多少?
select count(*) from student where (math+english+chinese)>250;

统计一个班级数学总成绩?
select sum(math) from student;

统计一个班级语文、英语、数学各科的总成绩
select sum(math),sum(english),sum(chinese) from student;

统计一个班级语文、英语、数学的成绩总和
select sum(math+english+chinese) from student;

统计一个班级语文成绩平均分
select sum(chinese)/count(chinese) from student;

求一个班级数学平均分?
select avg(math) from student;

求一个班级总分平均分
select avg(math+english+chinese) from student;

求班级最高分和最低分
select max(math+english+chinese), min(math+english+chinese) from student;

对订单表中商品归类后,显示每一类商品的总价
select product,sum(price) from orders group by product;

查询购买了几类商品,并且每类总价大于100的商品
select product from orders group by(product) where sum(price)>100; (×)
select product from orders group by(product) having sum(price)>100;


主键约束
create table test1
(
 id int primary key,
 name varchar(40)
);

create table test2
(
 id int primary key auto_increment,  UUID
 name varchar(40)
);

create table test3
(
 id varchar(40) primary key,
 name varchar(40)
);

create table test4
(
 id int primary key auto_increment,
 name varchar(40) unique
);


create table employee
(
 id int primary key auto_increment,
 name varchar(40) not null,
 sex varchar(4) not null,
 birthday date not null,
 entry_date date not null,
 job varchar(100),
 salary double not null,
 resume text
);


create table husband
(
 id int primary key,
 name varchar(40) not null
);

create table wife
(
 id int primary key,
 name varchar(40) not null,
 husband_id int,
 constraint husband_id_FK foreign key(husband_id) references husband(id)
);

 

创建用于保存部门员工数据的表(一对多,多对一)
create table department
(
 id int primary key auto_increment,
 name varchar(40) not null unique 
);
create table employee
(
 id int primary key auto_increment,
 name varchar(40) not null,
 department_id int,
 constraint department_id_FK foreign key(department_id) references department(id)
);

创建保存老师学生数据的表(多对多)
create table teacher
(
 id int primary key auto_increment,
 name varchar(40)

);

create table student
(
 id int primary key auto_increment,
 name varchar(40)
);

//联合主键
create table teacher_student
(
 teacher_id int,
 student_id int,
 primary key(teacher_id,student_id),
 constraint teacher_id_FK foreign key(teacher_id) references teacher(id),
 constraint student_id_FK foreign key(student_id) references student(id)
);

创建用于保存人和身份证数据表(一对一)
create table person
(
 id int primary key auto_increment,
 name varchar(40)
);

create table idcard
(
 id int primary key,
 location varchar(100),
 constraint idcard_id_FK foreign key(id) references person(id)
);


//多表查询
insert into department(name) values("开发部");
insert into employee(name,department_id) values("lisi",1);
insert into employee(name,department_id) values("wangwu",1);

//查询1号部门所有的员工
select * from employee where department_id=1


insert into student(name) values('aaa');
insert into student(name) values('bbb');

insert into teacher(name) values("zxx");
insert into teacher(name) values("lihuoming");

insert into teacher_student(teacher_id,student_id) values(1,1);
insert into teacher_student(teacher_id,student_id) values(1,2);
insert into teacher_student(teacher_id,student_id) values(2,1);
insert into teacher_student(teacher_id,student_id) values(2,2);


//查询出1号老师所有的学生
select s.id,s.name from teacher_student ts,student s where ts.teacher_id=1 and ts.student_id=s.id;

 

一对多的对象怎么保存
1、不管对象什么关系,保存的对象涉及到了几个实体,数据库中就设计几张表
2、表有什么列?先不要管对象之间的关系,只看对象有什么基本属性,表就设计什么列
3、最后为描述对象之间的关系,就在多的一方加外键

多对多对象的保存
1、不管对象什么关系,保存的对象涉及到了几个实体,数据库中就设计几张表
2、表有什么列?先不要管对象之间的关系,只看对象有什么基本属性,表就设计什么列
3、多对多对象的数据在中间表中描述

一对一对象的保存
1、不管对象什么关系,保存的对象涉及到了几个实体,数据库中就设计几张表
2、表有什么列?先不要管对象之间的关系,只看对象有什么基本属性,表就设计什么列
3、把从表的主键列当作外键列,以说明从的一方属于哪个主

 

posted @ 2016-11-03 14:01  小胖猪  阅读(4221)  评论(0编辑  收藏  举报