一些常用的mysql語句,增刪改查
阿新 • • 發佈:2019-01-08
DDL:資料庫定義語句
DML:增刪改查
DCL:資料庫控制語句
事物
資料庫的表設計:
一、庫操作
1.建立資料庫
create database mydb1;
show databases;
2.建立帶字符集的資料庫
create database mydb2 CHARACTER SET=utf8;
show create database mydb2;
3.建立帶校驗的資料庫
create database mydb3 CHARACTER SET=utf8 COLLATE utf8_general_ci;
4.顯示資料庫語句:SHOW DATABASES
5.顯示資料庫建立語句(詳情) SHOW C
REATE DATABASE mydb3
6.資料庫刪除語句:
DROP DATABASE mydb3;
7.修改資料庫的庫字元編碼
ALTER DATABASE mydb2 character set gb2312;
8.備份test庫中的資料,並恢復(備份有一種方式,恢復有兩種方式)
mysqldump -u root -p mydb2>c://test.sql 是一個win命令
第一種方式:
1.建立庫
create database mydb2;
use mydb2;
2.恢復資料
source c://test.sql;
第二種方式:win命令
1.建立庫
create database mydb2;
use mydb2;
exit;
2.恢復資料
mysql -u root -p mydb2<c://test.sql
9.建立表
1)create table test
(
name varchar(40)
);
2)增加資料
insert into test values("aaa");
二、表操作
1.建立資料庫表
create table employee
(
id int,
name varchar(40),
sex char(4),
birthday date,
Entry_date date,
job varchar(100),
salary Decimal(8,2),
resume Text
);
2.在上面員工表的基本上增加一個image列。
alter table employee add image blob;3.修改job列,使其長度為60。
alter table employee modify job varchar(60);
4.刪除sex列。
alter table employee drop image;
5.表名改為user。
rename table employee to user;
6.修改表的字符集為utf-8alter table user character set gbk;
alter table user character set utf8;
7.列名name修改為username
刪除表
alter table user change column name username varchar(100);
8.刪除表
drop table user;
三、增刪改查
準備表
create table employee
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
salary decimal(8,2),
resume text
);
1.插入資料
insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');
//可以省略表字段,但是必須插入全部欄位
insert into employee values(null,null,'male','1993-03-04','2016-11-10','1000','i am a developer');
//無法插入
insert into employee values('male','1993-03-04','2016-11-10','1000','i am a developer');
//可以包起來所有型別
insert into employee values('5',null,'male','1993-03-04','2016-11-10','1000','i am a developer');
//指定某些列插入資料
insert into employee(id) values(6);
//插入漢字
insert into employee(id,name) values(6,'張三');
要告訴mysql客戶採用gb2312編碼
show variables like 'chara%';
set character_set_client=gb2312;
insert into employee(id,username) values('3','張三');
要想檢視時不亂碼
show variables like 'chara%';
set character_set_results=gb2312;
select * from employee;
2.修改表資料
將所有員工薪水修改為5000元。
update employee set salary=5000;
將姓名為’zs’的員工薪水修改為3000元。
update employee set salary = 3000 where name='zhangsan';
將姓名為’aaa’的員工薪水修改為4000元,job改為ccc。
update employee set salary = 4000,job='ccc' where name='張三';
將wu的薪水在原有基礎上增加1000元。
update employee set salary = salary+1000 where name='張三';
3.刪除
刪除表中名稱為’zs’的記錄。
delete from employee where job='ccc';
刪除表中所有記錄。
delete from employee;
使用truncate刪除表中記錄
truncate table employee;
//細節
delete from employee where
4.查詢
查詢表中所有學生的資訊。
select id,name,chinese,english,math from student;
查詢表中所有學生的姓名和對應的英語成績。
select name,english from student;
過濾表中重複資料。select distinct english from student;
在所有學生分數上加10分特長分。
select name,(chinese+english+math)+10 from student;
統計每個學生的總分。
select name,(chinese+english+math) from student;
使用別名表示學生分數
select name,(chinese+english+math) as 總分 from student;
//可以不用as
select name,(chinese+english+math) 總分 from student;
查詢姓名為wu的學生成績
select * from student where name='張三';
查詢英語成績大於90分的同學
select * from student where english>'90';
查詢總分大於200分的所有同學
select name,(chinese+english+math) 總分 from student where chinese+english+math>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=89 or math=90 or math=91;
select * 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>80;
select * from student where chinese is null;
對數學成績排序後輸出。
select name,math from student order by math desc;對總分排序後輸出,然後再按從高到低的順序輸出
select name,math+english+chinese from student order by math+english+chinese desc;
對姓李的學生成績排序輸出
select name,math+english+chinese from student where name like '李%' order by math+english+chinese desc;
統計一個班級共有多少學生?
select count(*) from student;
select count(id) from student;
統計數學成績大於80的學生有多少個?
select count(*) from student where math>80;
統計總分大於250的人數有多少?
select count(*) from student where math+english+chinese>250;
//細節 null不能被count
select count(chinese) from student;
統計一個班級數學總成績?
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(*) from student;
求一個班級數學平均分?
select avg(chinese) from student;
求一個班級總分平均分?
select avg(math+english+chinese) from student;
求班級最高分和最低分(數值範圍在統計中特別有用)
select max(math+english+chinese) from student;
select min(math+english+chinese) from student;
對訂單表中商品歸類後,顯示每一類商品的總價
select product,sum(price) from orders group by product;
查詢購買了幾類商品,並且每類總價大於100的商品select product,sum(price) from orders group by product having sum(price)>100;
四、表約束
1.定義主鍵約束
create table demo1(
id int primary key,
name varchar(40)
);
//1.插入空id
insert into demo1(name) values('aaa');
//2.不允許重複
insert into demo1(id,name) values(1,'aaa');
insert into demo1(id,name) values(1,'aaa');
2.定義主鍵自動增長
create table demo2(
id int auto_increment primary key,
name varchar(40)
);
insert into demo2(name) values('aaa');
delete from demo2 where id = 3;
3.定義唯一約束
create table demo3(
id int auto_increment primary key,
name varchar(40) unique
);
insert into demo3(name) values('aaa');
4.定義非空
create table demo4(
id int auto_increment primary key,
name varchar(40) not null
);
insert into demo3(password) values('123');
5.定義外來鍵約束
create table department(
id int auto_increment primary key,
name varchar(30) not null
);
create table employee2(
id int auto_increment primary key,
name varchar(30) not null,
salary double,
department_id int,
constraint department_id_FK foreign key(department_id) references department(id)
);
DML:增刪改查
DCL:資料庫控制語句
事物
資料庫的表設計:
一、庫操作
1.建立資料庫
create database mydb1;
show databases;
2.建立帶字符集的資料庫
create database mydb2 CHARACTER SET=utf8;
show create database mydb2;
3.建立帶校驗的資料庫
create database mydb3 CHARACTER SET=utf8 COLLATE utf8_general_ci;
4.顯示資料庫語句:SHOW DATABASES
5.顯示資料庫建立語句(詳情) SHOW C
REATE DATABASE mydb3
6.資料庫刪除語句:
DROP DATABASE mydb3;
7.修改資料庫的庫字元編碼
ALTER DATABASE mydb2 character set gb2312;
8.備份test庫中的資料,並恢復(備份有一種方式,恢復有兩種方式)
mysqldump -u root -p mydb2>c://test.sql 是一個win命令
第一種方式:
1.建立庫
create database mydb2;
use mydb2;
2.恢復資料
source c://test.sql;
第二種方式:win命令
1.建立庫
create database mydb2;
use mydb2;
exit;
2.恢復資料
mysql -u root -p mydb2<c://test.sql
9.建立表
1)create table test
(
name varchar(40)
);
2)增加資料
insert into test values("aaa");
二、表操作
1.建立資料庫表
create table employee
(
id int,
name varchar(40),
sex char(4),
birthday date,
Entry_date date,
job varchar(100),
salary Decimal(8,2),
resume Text
);
2.在上面員工表的基本上增加一個image列。
alter table employee add image blob;3.修改job列,使其長度為60。
alter table employee modify job varchar(60);
4.刪除sex列。
alter table employee drop image;
5.表名改為user。
rename table employee to user;
6.修改表的字符集為utf-8alter table user character set gbk;
alter table user character set utf8;
7.列名name修改為username
刪除表
alter table user change column name username varchar(100);
8.刪除表
drop table user;
三、增刪改查
準備表
create table employee
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
salary decimal(8,2),
resume text
);
1.插入資料
insert into employee(id,name,sex,birthday,entry_date,salary,resume) values(1,'zhangsan','male','1993-03-04','2016-11-10','1000','i am a developer');
//可以省略表字段,但是必須插入全部欄位
insert into employee values(null,null,'male','1993-03-04','2016-11-10','1000','i am a developer');
//無法插入
insert into employee values('male','1993-03-04','2016-11-10','1000','i am a developer');
//可以包起來所有型別
insert into employee values('5',null,'male','1993-03-04','2016-11-10','1000','i am a developer');
//指定某些列插入資料
insert into employee(id) values(6);
//插入漢字
insert into employee(id,name) values(6,'張三');
要告訴mysql客戶採用gb2312編碼
show variables like 'chara%';
set character_set_client=gb2312;
insert into employee(id,username) values('3','張三');
要想檢視時不亂碼
show variables like 'chara%';
set character_set_results=gb2312;
select * from employee;
2.修改表資料
將所有員工薪水修改為5000元。
update employee set salary=5000;
將姓名為’zs’的員工薪水修改為3000元。
update employee set salary = 3000 where name='zhangsan';
將姓名為’aaa’的員工薪水修改為4000元,job改為ccc。
update employee set salary = 4000,job='ccc' where name='張三';
將wu的薪水在原有基礎上增加1000元。
update employee set salary = salary+1000 where name='張三';
3.刪除
刪除表中名稱為’zs’的記錄。
delete from employee where job='ccc';
刪除表中所有記錄。
delete from employee;
使用truncate刪除表中記錄
truncate table employee;
//細節
delete from employee where
4.查詢
查詢表中所有學生的資訊。
select id,name,chinese,english,math from student;
查詢表中所有學生的姓名和對應的英語成績。
select name,english from student;
過濾表中重複資料。select distinct english from student;
在所有學生分數上加10分特長分。
select name,(chinese+english+math)+10 from student;
統計每個學生的總分。
select name,(chinese+english+math) from student;
使用別名表示學生分數
select name,(chinese+english+math) as 總分 from student;
//可以不用as
select name,(chinese+english+math) 總分 from student;
查詢姓名為wu的學生成績
select * from student where name='張三';
查詢英語成績大於90分的同學
select * from student where english>'90';
查詢總分大於200分的所有同學
select name,(chinese+english+math) 總分 from student where chinese+english+math>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=89 or math=90 or math=91;
select * 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>80;
select * from student where chinese is null;
對數學成績排序後輸出。
select name,math from student order by math desc;對總分排序後輸出,然後再按從高到低的順序輸出
select name,math+english+chinese from student order by math+english+chinese desc;
對姓李的學生成績排序輸出
select name,math+english+chinese from student where name like '李%' order by math+english+chinese desc;
統計一個班級共有多少學生?
select count(*) from student;
select count(id) from student;
統計數學成績大於80的學生有多少個?
select count(*) from student where math>80;
統計總分大於250的人數有多少?
select count(*) from student where math+english+chinese>250;
//細節 null不能被count
select count(chinese) from student;
統計一個班級數學總成績?
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(*) from student;
求一個班級數學平均分?
select avg(chinese) from student;
求一個班級總分平均分?
select avg(math+english+chinese) from student;
求班級最高分和最低分(數值範圍在統計中特別有用)
select max(math+english+chinese) from student;
select min(math+english+chinese) from student;
對訂單表中商品歸類後,顯示每一類商品的總價
select product,sum(price) from orders group by product;
查詢購買了幾類商品,並且每類總價大於100的商品select product,sum(price) from orders group by product having sum(price)>100;
四、表約束
1.定義主鍵約束
create table demo1(
id int primary key,
name varchar(40)
);
//1.插入空id
insert into demo1(name) values('aaa');
//2.不允許重複
insert into demo1(id,name) values(1,'aaa');
insert into demo1(id,name) values(1,'aaa');
2.定義主鍵自動增長
create table demo2(
id int auto_increment primary key,
name varchar(40)
);
insert into demo2(name) values('aaa');
delete from demo2 where id = 3;
3.定義唯一約束
create table demo3(
id int auto_increment primary key,
name varchar(40) unique
);
insert into demo3(name) values('aaa');
4.定義非空
create table demo4(
id int auto_increment primary key,
name varchar(40) not null
);
insert into demo3(password) values('123');
5.定義外來鍵約束
create table department(
id int auto_increment primary key,
name varchar(30) not null
);
create table employee2(
id int auto_increment primary key,
name varchar(30) not null,
salary double,
department_id int,
constraint department_id_FK foreign key(department_id) references department(id)
);