structured query language(结构化查询语言)

root

 

测试mysql是否安装成功:

mysql -uroot -p

show databases;

 

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

mysql -uroot -p

create database mydb1;

show databases;

 

创建一个使用utf-8字符集的mydb2数据库。

create database mydb2 character set utf8;

 

创建一个使用utf-8字符集,并带校对规则的mydb3数据库。

create database mydb3 character set utf8 collate utf8_general_ci;

 

查看前面创建的mydb2数据库的定义信息

show create database mydb2;

 

删除前面创建的mydb1数据库

drop database mydb1;

 

查看服务器中的数据库,并把其中mydb2的字符集修改为gb2312;

alter database mydb2 character set gb2312;

show create database mydb2;

 

 

数据库的备份和恢复

 

1.准备数据

create database mydb4;

use mydb4;

create table test

(

id int

);

insert into test(id) values(1);

 

quit;

 

2、备份库

mysqldump -uroot -proot mydb4>d:\1.sql

 

3、删除库

mysql -uroot -proot

drop database mydb4;

4、恢复库

create database mydb4;

quit

mysql -uroot -proot mydb4<d:\1.sql

source d:\1.sql

 

创建带时间戳的表

create table test2

(

name varchar(20),

time TimeStamp

);

 

创建一个员工表

进入库:use mydb2

create table employee

(

id int,

name varchar(40),

gender varchar(10),

birthday date,

entry_date date,

job varchar(100),

salary double,

resume text

)character set utf8;

 

在上面员工表的基本上增加一个image列。

alter table employee add image blob;

 

修改job列,使其长度为60。

alter table employee modify job varchar(60);

 

删除sex列。

alter table employee drop gender;

 

看修改后的结果

show create table employee;(看表的创建语法)

desc employee (看表的结构)

 

表名改为user。

rename table employee to user;

 

修改表的字符集为utf-8

alter table user character set utf8;

 

列名name修改为username

alter table user change column name username varchar(40);

 

删除表

drop table user;

 

 

使用insert语句向employee表中插入一个员工的信息。

rename table user to employee;

insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-09-09','1989-09-09','aaa',98.98,'aaaaa');

 

 

使用insert语句插入中文数据

show variables like 'character%';   (显示mysql中所有涉及到字符编码的变量)

set character_set_client=gb2312;

 

insert into employee(id,username,birthday,entry_date,job,salary,resume) values(2,'李一','1980-09-09','1989-09-09','aaa',98.98,'aaaaa');

 

显示中文数据

set character_set_results=gb2312;

select * from employee;

 

 

将所有员工薪水修改为5000元。

update employee set salary=5000;

select * from employee;

 

将姓名为aaa’的员工薪水修改为3000元。

update employee set salary=3000 where username='aaa';

 

将姓名为’aaa’的员工薪水修改为4000元,job改为ccc。

update employee set salary=4000,job='ccc' where username='aaa';

 

将aaa的薪水在原有基础上增加1000元。

update employee set salary=salary+1000 where username='aaa';

 

删除表中名称为’zs’的记录。

delete from employee where username='aaa';

 

删除表中所有记录

delete from employee;

 

使用truncate删除表中记录

truncate table employee;

 

查询表中所有学生的信息。

select * from student

select id,name,chinese,english,math from student;

 

查询表中所有学生的姓名和对应的英语成绩。

select name,english from student;

 

过滤表中重复的英语成绩

select distinct name,english from student;

 

在所有学生每课成绩上加10分。

select name,(chinese+10),english+10,math+10 from student;

 

统计每个学生的总分。

select name,(chinese+english+math) from student;

 

使用别名表示学生分数。

select name as 姓名,(chinese+english+math) as 总分 from student;

 

查询姓名为wu的学生成绩

select * from student where name='wu';

 

查询英语成绩大于90分的同学

select * from student where english>90;

 

查询总分大于200分的所有同学

select * from student where (math+english+chinese)>200;

 

查询英语分数在 80-90之间的同学。

select * from student where english>=80 and english<=90;

select * from student where english between 80 and 90;

 

查询数学分数为89,90,91的同学。

select * from student where math in(89,90,91);

select * from student where math=89 or math=90 or math=91;

 

查询所有姓李的学生成绩。

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 from student order by(english+math+chinese) desc;

 

对姓李的学生成绩排序输出

select name from student where name like '李%' order by(english+math+chinese) desc;

 

统计一个班级共有多少学生?

select count(*) from student;

 

统计数学成绩大于90的学生有多少个?

select count(*) from student where math>90;

 

统计总分大于250的人数有多少?

select count(*) from student where (math+chinese+english)>250;

 

统计一个班级数学总成绩?

select sum(math) from student;

 

统计一个班级语文、英语、数学各科的总成绩

select sum(chinese),sum(english),sum(math) from student;

 

统计一个班级语文、英语、数学的成绩总和

select sum(chinese+english+math) from student;

 

统计一个班级语文成绩平均分

select sum(chinese)/count(chinese) from student;

 

求一个班级数学平均分?

select avg(math) from student;

 

求一个班级总分平均分

select avg(math+chinese+english) from student;

 

求班级最高分和最低分

select max(math+chinese+english),min(math+chinese+english) from student;

 

请统计表中共有几种商品 

select product from orders group by(product);

 

对商品归类后,统计每一类商品花了多少钱

select product,sum(price) from orders group by(product);

 

查询购买了几类商品,并且每类总价大于100的商品

select product from orders group by(product) where sum(price)>100;

select product,sum(price) from orders group by(product) having sum(price)>100;

 

 

定义主键约束

create table person

(

id int primary key,

name varchar(40)

);

 

定义主键自动增长

create table person2

(

id int primary key auto_increment,

name varchar(40)

);

 

定义唯一约束

create table person3

(

id int primary key auto_increment,

name varchar(40) unique

);

 

定义非空约束

create table person4

(

id int primary key auto_increment,

name varchar(40) unique not null

);

 

定义外键约束

create table husband

(

id int primary key,

name varchar(40)

);

 

create table wife

(

id int primary key,

name varchar(40),

husband_id int,

constraint husband_id_FK foreign key(husband_id) references husband(id)

);

 

 

部门关系管理

create table department

(

id int primary key,

name varchar(40) not null unique

);

 

create table employee

(

id int primary key,

name varchar(40) not null unique,

salary double not null,

department_id int,

constraint department_id_FK foreign key(department_id) references department(id)

);

 

 

老师和学生

create table teacher

(

id int primary key,

name varchar(40) not null unique,

salary double not null

);

 

create table student

(

id int primary key,

name varchar(40) not null unique

);

 

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,

name varchar(40)

);

 

create table idcard

(

id int primary key,

address varchar(100),

constraint id_FK foreign key(id) references person(id)

);

 

 

查询出1号老师所有的学生

select s.name from teacher_student ts,student s  where ts.teacher_id=1 and ts.student_id=s.id;

 

 

找出开发部所有的员工

 

insert into employee(id,name,salary,department_id) values(1,'aa',100,1);

insert into employee(id,name,salary,department_id) values(2,'bb',100,1);

insert into employee(id,name,salary,department_id) values(3,'cc',100,2);

 

select e.name from department d,employee e where d.name='kafa' and d.id=e.department_id;

你可能感兴趣的:(language)