11-sql语句
數據庫: 保存在硬盤上的文件
數據庫產品: 采用一種優良的數據結構來保存數據,方便我們對數據進行查詢和修改
sql語句: 用于操作數據庫的語句
數據庫database 表table
創建create
查看show
修改alter
刪除drop
所有對數據庫和表單的操作 就是上面六個單詞的組合
// 創建一個數據庫
create database mydb;
// 刪除一張表
drop table mytable;
一、 數據庫的操作
創建一個名稱為mydb1的數據庫。
create database mydb1;
創建一個使用utf-8字符集的mydb2數據庫。
create database mydb2 character set utf8;
創建一個使用utf-8字符集,并帶校對規則的mydb3數據庫。
create database mydb3 character set utf8 collate utf8_general_ci;
查看所有的數據庫
show databases;
查看數據庫的創建語句
show create database mydb2;
刪除前面創建的mydb3數據庫
drop database mydb3;
drop database if exists mydb3;? 如果存在才會刪除
數據庫的修改
數據庫一旦創建名稱無法修改? , 字符集和校對規則可以改
把mydb2的字符集修改為gb2312
alter database mydb2 character set gb2312;
備份數據庫
// 使用數據庫
use mydb2;
// 創建表
create table a
(
?name varchar(20)
);
// 插入數據
insert into a(name) values('aaa');
insert into a(name) values('bbb');
備份數據庫
mysqldump -uroot -proot mydb2 > d:\a.sql
刪除 mydb2
drop database mydb2;
恢復數據庫? 實際上是恢復數據庫中的數據
創建數據庫 使用數據庫
create database mydb3;
use mydb3;
source d:\a.sql
實際上source命令用于執行一段sql腳本
二、 表的操作
創建表
id?整形
name?字符型
gender?字符型或bit型
brithday?日期型
entry_date?日期型
job?字符型
Salary?小數型
resume?大文本型
create table employee
(
?id int,
?name varchar(20),
?gender varchar(6),
?birthday date,
?entry_date date,
?job varchar(20),
?salary float,
?resume text
);
修改表
在上面員工表的基礎上增加一個image列
alter table employee add image blob;
修改job列,使其長度為60
alter table employee modify job varchar(60);
刪除gender列。
alter table employee drop gender;
表名改為users。
rename table employee to users;
修改表的字符集為utf-8
alter table users character set utf8;
列名name修改為username
alter table users change column name username varchar(40);
查看所有的表
show tables;
查看表的創建語句
show create table users;
查看表的結構
desc users;
// 刪除表
drop table employee;
三、 表中數據的操作 (重點)
crud: create read update delete
sql語句
1. insert語句? 增加數據
employee.sql
create table employee
(
?id int,
?name varchar(20),
?gender varchar(10),
?birthday date,
?salary float,
?entry_date date,
?resume text
);
插入三條記錄
insert into employee(id,name,gender,birthday,salary,entry_date,resume) values(1,'zhangsan','male','1980-1-1',1000,'2000-3-16','good boy');
insert into employee(id,name,gender,birthday,salary,entry_date,resume) values(2,'lisi','male','1983-1-1',1000,'2010-3-16','good boy');
insert into employee(id,name,gender,birthday,salary,entry_date,resume) values(3,'xiaohong','female','1984-1-1',1000,'2008-3-16','good girl');
insert into employee(id,name,gender,birthday,salary,entry_date,resume) values(4,'王五','男','1983-1-1',1000,'2010-3-16','一個好男孩');
// 查看數據庫的所有編碼
show variables like 'character%';
character_set_client?使用的客戶端編碼
character_set_results?結果集的編碼
設置的方式
set character_set_client=gbk
set character_set_results=gbk
// 創建一張a表
create table a
(
?id int,
?name varchar(20)
);
insert into a values(1,'aaa');
insert into a(name) values('bbbb');
insert a(id,name) values(2,'cccc'),(3,'dddd');
規范的方式書寫
insert into a(id,name) values(6,'eeeee');
查看表中的數據
select * from employee;
2. update語句 更新數據
將所有員工薪水修改為5000元。
update employee set salary=5000;
將姓名為’zhangsan’的員工薪水修改為3000元。
update employee set salary=3000 where name='zhangsan';
將姓名為’lisi’的員工薪水修改為4000元,gender改為female。
update employee set salary=4000,gender='female' where name='lisi';
將xiaohong的薪水在原有基礎上增加1000元。
update employee set salary=salary+1000 where name='xiaohong';
?
3. delete語句 刪除數據
刪除表中name為’zhangsan’的記錄。
delete from employee where name='zhangsan';
刪除表中所有記錄。
delete from employee;
使用truncate刪除表中記錄。 刪除表再創建表
truncate employee;
4. select語句 查詢數據
student.sql
create table student(
?id int,
?name varchar(20),
?chinese float,
?english float,
?math float
);
insert into student(id,name,chinese,english,math) values(1,'張小明',89,78,90);
insert into student(id,name,chinese,english,math) values(2,'李進',67,53,95);
insert into student(id,name,chinese,english,math) values(3,'王五',87,78,77);
insert into student(id,name,chinese,english,math) values(4,'李一',88,98,92);
insert into student(id,name,chinese,english,math) values(5,'李來財',82,84,67);
insert into student(id,name,chinese,english,math) values(6,'張進寶',55,85,45);
insert into student(id,name,chinese,english,math) values(7,'黃蓉',75,65,30);
insert into student(id,name,chinese,english,math) values(8,null,75,65,30);
查詢表中所有學生的信息。
select * from student;
查詢表中所有學生的姓名和對應的英語成績。
select name,english from student;
過濾表中重復數據。
select distinct english from student;
在所有學生分數上加10分特長分。
select name,chinese+10 as chinese,math+10,english+10 from student;
統計每個學生的總分。
select name,chinese+math+english from student;
使用別名表示學生分數。
select name,chinese c,english e from student;
查詢姓名為李一的學生成績
select * from student where name='李一';
查詢英語成績大于90分的同學
select * from student where english>90;
查詢總分大于200分的所有同學
select * from student where english+chinese+math>200;
查詢英語分數在 80-90之間的同學。 兩頭的值都包含
select * from student where english between 80 and 90;
查詢數學分數為89,90,91的同學。
select * from student where math in(89,90,30);
查詢所有姓李的學生成績。
select * from student where name like '李%';
查詢所有姓李的,名字是兩個字的學生成績。
select * from student where name like '李_';
查詢數學分>80,語文分>80的同學。
select * from student where math>80 and chinese>80;
查詢英語>80或者總分>200的同學
select *,chinese+english+math as sss from student where english>80 or chinese+english+math>200;
對數學成績排序后輸出。
select * from student order by math;
對總分排序后輸出,然后再按從高到低的順序輸出
select *,chinese+math+english from student order by chinese+math+english desc;
對姓李的學生成績排序輸出
select * from student where name like '李%' order by chinese;
合計函數 --? count
統計一個班級共有多少學生?
select count(*) from student;
統計數學成績大于90的學生有多少個?
select count(*) from student where math>90;
統計總分大于230的人數有多少?
select count(*) from student where math+chinese+english>230;
sum 只對數值類型起作用
統計一個班級數學總成績?
select sum(math) from student;
統計一個班級語文、英語、數學各科的總成績
select sum(math),sum(chinese),sum(english) from student;
統計一個班級語文、英語、數學的成績總和
select sum(math+chinese+english) from student;
統計一個班級語文成績平均分
select sum(chinese)/count(*) from student;
select sum(chinese)/count(chinese) from student;
求一個班級數學平均分?
select avg(chinese) from student;
求一個班級總分平均分
select avg(chinese+math+english) from student;
求班級語文最高分和最低分
select max(chinese) from student;
group by 子句 用于分類查詢
orders.sql
create table orders(
?id int,
?product varchar(20),
?price float
);
insert into orders(id,product,price) values(1,'電視',900);
insert into orders(id,product,price) values(2,'洗衣機',100);
insert into orders(id,product,price) values(3,'洗衣粉',90);
insert into orders(id,product,price) values(4,'桔子',9);
insert into orders(id,product,price) values(5,'洗衣粉',90);
對訂單表中商品歸類后,顯示每一類商品的總價
// 歸類
select * from orders group by product;
select product,sum(price) from orders group by product;
查詢購買了幾類商品,并且每類總價大于100的商品
select product,sum(price) from orders group by product having sum(price)>100;
四、表的約束
有的時候針對某個列,我們不希望別人插入任意的數據,這樣會導致錯誤
例如插入null值,取數據的時候就會出錯
例如有的列我們希望數據是有唯一性的(不允許重復)
not null 非空約束?? 這一列不允許插入null值
create table a
(
?name varchar(20) not null
);
insert into a(name) values('aaa');
insert into a(name) values(null);
// id需要唯一? unique 唯一約束?? 限定了列的值不能重復
create table b
(
?id int not null unique,
?name varchar(20)
);
insert into b(id,name) values(1,null);
insert into b(id,name) values(2,'zhangsan');
insert into b(id,name) values(2,'lisi');
// 只用唯一約束
create table c
(
?id int unique,
?name varchar(20)
);
insert into c(name) values('aaa');
insert into c(name) values('bbb');
insert into c(id,name) values(1,'ccc');
// 通常來講我們會根據id來查詢某條記錄?? id一般會加非空約束、唯一約束
// 主鍵約束?? =? 非空約束 + 唯一約束
create table d
(
?id int primary key,
?name varchar(20)
);
insert into d(name) values('aaa');?不行 ,因為非空
insert into d(id,name) values(1,'aaa');
insert into d(id,name) values(1,'bbb');?不行,唯一要唯一
通常來講 如果主鍵定義為int 我們為了方便? 會定義主鍵自定增長
create table e
(
?id int primary key auto_increment,
?name varchar(20)
);
insert into e(name) values('aaa');
insert into e(id,name) values(4,'bbb');
// 聲明聯合主鍵
create table f
(
?firstname varchar(20),
?lastname varchar(20),
?primary key(firstname,lastname)
);
insert into f(firstname,lastname) values('aaa','bbb');
insert into f(firstname,lastname) values('aaa','ccc');
insert into f(firstname) values('eee');
// 增加主鍵約束
create table g
(
?id int,
?name varchar(20)
);
// 增加兩條記錄
insert into g values(1,'aaa');
// 增加主鍵約束?? 但是如果主鍵列有重復或者為空的情況,主鍵約束加不上
alter table g add primary key(id);
// 刪除主鍵約束
create table h
(
?id int primary key,
?name varchar(20)
);
alter table h drop primary key;
夫妻關系系統
創建老公表
create table husband
(
?id int primary key auto_increment,
?name varchar(20)
);
insert into husband(name) values('張三');
insert into husband(name) values('李四');
insert into husband(id,name) values(3,'王五');
創建老婆表
create table wife
(
?id int primary key auto_increment,
?name varchar(20),
?husbandid int
);
insert into wife(name,husbandid) values('小紅',2);
insert into wife(name,husbandid) values('小蘭',3);
insert into wife(name,husbandid) values('小黑',1);
insert into wife(name,husbandid) values('小黃',8);
?
// 小蘭來找老公
select * from husband where id=3;
// 刪除王五
delete from husband where id=3;
// 外鍵約束
某一列數據是參照另一張表的某一個列? 這是就需要加外鍵約束
好處: 外鍵列只能插入參照列存在的值, 參照列被參照的值是不能刪除的
create table wife
(
?id int primary key auto_increment,
?name varchar(20),
?husbandid int,
?constraint husbandid_FK foreign key(husbandid) references husband(id)
);
表的關系 有三種情況
多對一
在多的一方建立外鍵
多對多
需要創建中間表描述關系
中間表有兩個字段都是外鍵參照兩個表的主鍵列,同時這兩列又是聯合主鍵
一對一
分清主從關系
在從的一方建立外鍵? 此時應將主鍵直接作為外鍵
多對一
創建部門表
create table department
(
?id int primary key auto_increment,
?name varchar(20)
);
創建員工表
drop table if exists employee;
create table employee
(
?id int primary key auto_increment,
?name varchar(20),
?departmentid int,
?constraint departmentid_FK foreign key(departmentid) references department(id)
);
insert into department(name) values('開發部');
insert into department(name) values('銷售部');
insert into department(name) values('人事部');
insert into employee(name,departmentid) values('張三',1);
insert into employee(name,departmentid) values('李四',1);
insert into employee(name,departmentid) values('王五',2);
insert into employee(name,departmentid) values('趙六',2);
insert into employee(name,departmentid) values('田七',2);
insert into employee(name,departmentid) values('周八',3);
多表查詢
查出1號部門所有的員工
select * from employee where departmentid=1;
查出銷售部所有的員工
select * from department where name='銷售部';
select * from employee where departmentid=2;
多表查詢
select * from employee,department;
笛卡爾集? :? 兩張表所有記錄的組合
+----+------+--------------+----+--------+
| id | name | departmentid | id | name?? |
+----+------+--------------+----+--------+
|? 1 | 張三???? |??????????? 1 |? 1 | 開發部?????? |
|? 1 | 張三???? |??????????? 1 |? 2 | 銷售部????? |
|? 1 | 張三???? |??????????? 1 |? 3 | 人事部????? |
|? 2 | 李四???? |??????????? 1 |? 1 | 開發部?????? |
|? 2 | 李四???? |??????????? 1 |? 2 | 銷售部????? |
|? 2 | 李四???? |??????????? 1 |? 3 | 人事部????? |
|? 3 | 王五???? |??????????? 2 |? 1 | 開發部?????? |
|? 3 | 王五???? |??????????? 2 |? 2 | 銷售部????? |
|? 3 | 王五???? |??????????? 2 |? 3 | 人事部????? |
|? 4 | 趙六???? |??????????? 2 |? 1 | 開發部?????? |
|? 4 | 趙六???? |??????????? 2 |? 2 | 銷售部????? |
|? 4 | 趙六???? |??????????? 2 |? 3 | 人事部????? |
|? 5 | 田七???? |??????????? 2 |? 1 | 開發部?????? |
|? 5 | 田七???? |??????????? 2 |? 2 | 銷售部????? |
|? 5 | 田七???? |??????????? 2 |? 3 | 人事部????? |
|? 6 | 周八??? |??????????? 3 |? 1 | 開發部?????? |
|? 6 | 周八??? |??????????? 3 |? 2 | 銷售部????? |
|? 6 | 周八??? |??????????? 3 |? 3 | 人事部????? |
+----+------+--------------+----+--------+
去掉廢數據? (不匹配的數據 錯誤的數據)
參照表的外鍵列等于被參數表的主鍵列(被參照的列)
select * from employee,department where employee.departmentid=department.id;
+----+------+--------------+----+--------+
| id | name | departmentid | id | name?? |
+----+------+--------------+----+--------+
|? 1 | 張三???? |??????????? 1 |? 1 | 開發部?????? |
|? 2 | 李四???? |??????????? 1 |? 1 | 開發部?????? |
|? 3 | 王五???? |??????????? 2 |? 2 | 銷售部????? |
|? 4 | 趙六???? |??????????? 2 |? 2 | 銷售部????? |
|? 5 | 田七???? |??????????? 2 |? 2 | 銷售部????? |
|? 6 | 周八??? |??????????? 3 |? 3 | 人事部????? |
+----+------+--------------+----+--------+
加查詢條件獲得結果
select * from employee,department where employee.departmentid=department.id and department.name='銷售部';
題目: 查出銷售部所有的員工
最終結果
select e.* from employee e,department d where e.departmentid=d.id and d.name='銷售部';
多對多
// 創建老師表
create table teacher
(
?id int primary key auto_increment,
?name varchar(20)
);
// 創建學生表
create table student
(
?id int primary key auto_increment,
?name varchar(20)
);
// 創建中間表
create table tea_stu
(
?teaid int,
?stuid int,
?primary key(teaid,stuid),
?constraint teaid_FK foreign key(teaid) references teacher(id),
?constraint stuid_FK foreign key(stuid) references student(id)
);
// 插入三個老師
insert into teacher(name) values('老張');
insert into teacher(name) values('老黎');
insert into teacher(name) values('老方');
// 插入7個學生
insert into student(name) values('大毛');
insert into student(name) values('二毛');
insert into student(name) values('三毛');
insert into student(name) values('四毛');
insert into student(name) values('五毛');
insert into student(name) values('六毛');
insert into student(name) values('小毛');
// 插入中間表
insert into tea_stu(teaid,stuid) values(1,1);
insert into tea_stu(teaid,stuid) values(1,2);
insert into tea_stu(teaid,stuid) values(1,3);
insert into tea_stu(teaid,stuid) values(1,4);
insert into tea_stu(teaid,stuid) values(1,5);
insert into tea_stu(teaid,stuid) values(2,7);
insert into tea_stu(teaid,stuid) values(2,6);
insert into tea_stu(teaid,stuid) values(2,4);
insert into tea_stu(teaid,stuid) values(2,2);
insert into tea_stu(teaid,stuid) values(3,1);
insert into tea_stu(teaid,stuid) values(3,3);
insert into tea_stu(teaid,stuid) values(3,4);
insert into tea_stu(teaid,stuid) values(3,5);
insert into tea_stu(teaid,stuid) values(3,7);
// 查詢
2號老師的所有學生的id
select stuid from tea_stu where teaid=2;
2號老師的所有的學生信息
select s.* from student s,tea_stu ts where ts.stuid=s.id and ts.teaid=2;
查看老黎的所有學生
select s.* from student s,teacher t,tea_stu ts where ts.teaid=t.id and ts.stuid=s.id and t.name='老黎';
n張表聯合查詢,需要寫n-1個條件來去掉廢數據, 然后加上真正的篩選條件來查
參照表的外鍵列=被參照表的被參照列(主鍵)
總結
- 上一篇: 波兰PT-91主战坦克“坚韧”在何处?
- 下一篇: 新中国第一艘核潜艇