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、把從表的主鍵列當作外鍵列,以說明從的一方屬於哪個主