創建1個名稱為mydb1的
數據庫,如果有mydb1
數據庫則直接使用,如果無則創建mydb1
數據庫
create database if not exists mydb1;
create database if not exists mydb1;
創建1個使用UTF8字符集的mydb2
數據庫,注意這里不是UTF⑻
create database if not exists mydb2 character set UTF8;
create database if not exists mydb2 character set UTF8;
創建1個使用UTF8字符集,并帶校訂規則的mydb3
數據庫
create database if not exists mydb3 character set UTF8 collate utf8_general_ci;
校訂規則:是
數據庫表中的所有記錄按甚么方式存儲數據的前后順序,例如:a在前,z在后
字符集與校訂規則是逐一對應,不能亂改
如果不寫校訂規則的話,默許是[對應字符集]默許的校訂規則。參考<<MySQL5手冊-⑴0.10.10>>
查看當前
數據庫服務器中的所有
數據庫
show databases;
查看前面創建的mydb1
數據庫的定義信息
show create database mydb1;
show create database mydb1;
刪除前面創建的mydb1
數據庫,如果有mydb1則刪除
drop database if exists mydb1;
drop database if exists mydb1;
使用mydb2
數據庫
use mydb2;
use mydb2;
查看
數據庫服務器中的
數據庫,并把其中mydb3庫的字符集修改成GBK
alter database mydb3 character set GBK;
alter database mydb3 character set GBK;
以下代碼是在mydb2
數據庫中創建users表,并插入幾條記錄,學員們暫時不用理睬
create table if not exists users(
name varchar(10)
);
insert into users values('XX');
insert into users values('YY');
insert into users values('ZZ');
備份mydb1庫中的數據到e:/xx.sql的文件中,以便將來恢復之用
window7(先exit到windows環境)備份:mysqldump -uroot -p mydb1 > e:\mydb1.sql回車
mysqldump -uroot -p mydb1 > d:/myydb1.sql
mysqldump是mysql提供的用于備份
數據庫的命令
mysqldump命令必須window環境運行
source命令中mysql環境運行
mysql恢復:source e:\mydb1.sql回車
注意:恢復時,[先創建
數據庫]并使用該
數據庫,[再]通過source命令,由于sql文件中[只有]表信息,[無]
數據庫信息
創建1個users表,包括id/username/password/birthday/salary
create table if not exists users(
id int(4),
username varchar(10),
password varchar(6),
birthday datetime,
salary float(6,2)
);
float(6,2)表示:2表示顯示時小數點后最多幾位,超過2位,4舍5入;
6表示整數和小數最多幾位,整數部分最多(6⑵)位
以上表中的所有字段都允許為NULL,且默許NULL
如果數據顯示出來是亂碼,按以下步驟:
向users表中插入1條記錄,先英后中(沒法插入中文)
insert into users values(1,'jack','123456','2015⑻⑻ 8:8:8',9999.99);
insert into users values(2,'哈哈','123456','2015⑼⑼ 9:9:9',9999.99);
insert into users values(3,'呵呵','123456','2015⑺⑺ 7:7:7',7777.77);
查詢users表的結構
desc users;
創建employees表------------------------------用employees.sql表
在上面員工表的基本上增加1個image列
alter table employees
add image blob;
alter table employees add image blob;
企業中,不是真真正正存照片本身,而是存照片路徑,即E:/zgl.jpg
修改name列,使其長度為60
alter table employees
modify name varchar(60);
alter table employees
modify name varchar(60);
刪除image列
alter table employees
drop column image;
alter table employees
drop column image;
如果表中有內容的話,刪除或添加某列,不影響其它列的信息
表名employees改成staff
rename table employees to staff;
rename table employees to staff;
修改表的字符集為GBK
alter table staff
character set UTF8/GBK/GB2312;
列名name修改成username
alter table staff
change column name username varchar(60);
alter table staff
change column name username varchar(60);
向staff表中插入數據
insert into staff values(3,'趙君','男','2005⑴⑴',3333.33,'2005⑺⑴','這是備注信息');
顯示插入NULL
insert into employees values(1,'哈哈','男','2015⑴⑴',1111.11,'2015⑸⑸','這是備注信息',NULL);
隱式插入NULL
insert into employees(id,name,sex,birthday,salary,resume)
values(2,'呵呵','男','2015⑴⑴',2222.22,'這是備注信息');
如果存在表就刪除表
drop table if exists staff;//表不在了
drop table if exists staff;
truncate table users;//表還在,只不過沒有內容了
再重新創建表
create table staff(
id int(5),
name varchar(6),
sal int(5)
);
insert into staff values(1,'哈哈',7000);
insert into staff values(2,'呵呵',8000);
insert into staff values(3,'嘻嘻',9000);
insert into staff values(4,'明明',10000);
insert into staff(id,name,sal) values(4,'星星',6000);
insert into staff(name,id,sal) values('月月',5,6000);
----------------------------------------------------------------------------------------------------------------
將所有員工薪水修改成10000元
update staff set sal=10000;
update staff set sal=10000;
以上就是SQL語句的威力,如果發送命令,MySQL
數據庫服務器自行會解析其命令,并做相對應的進程處理,這個
進程處理對程序員來說,是封閉的,看不見。
SQL的全稱【結構化查詢語句】,第4代計算機語言
第1代:機器語言,即01010010100100101
第2代:匯編語言,即用1個代號去表示10101010這些數字
第3代:高級語言,即c/c++/vb/java/c#/...
第4代:命令語言,即SQL
第5代:智能語言,。。。
將姓名為'哈哈'的員工薪水修改成11000元
update staff set sal=11000 where name = '哈哈';
update staff set sal=11000 where name = '哈哈';
將月月的薪水在原有基礎上增加1000元
update staff set sal = sal + 1000 where name = '月月';
update staff set sal = sal+1000 where name = '月月';
刪除表中3號的記錄
delete from staff where id = 3;
delete from staff where id = 3;
刪除表中所有記錄
delete from staff;
delete它是1行1行刪除,速慢較【慢 】 drop table staff;全部表都被刪除
使用truncate刪除表中記錄
truncate它是整表刪除后再重構,速慢較【快】。但是它的表結構還在,知識表內容沒了
---------------------------------------------------------------------------------------------------------------
students表
use mydb1;
drop table if exists students;
create table if not exists students(
id int(5),
name varchar(20),
chinese int(5),
english int(5),
math int(5)
);
insert into students(id,name,chinese,english,math) values(1,'張小明',89,78,90);
insert into students(id,name,chinese,english,math) values(2,'李進',67,98,56);
insert into students(id,name,chinese,english,math) values(3,'王5',87,78,77);
insert into students(id,name,chinese,english,math) values(4,'李1',88,98,90);
insert into students(id,name,chinese,english,math) values(5,'李來財',82,84,67);
insert into students(id,name,chinese,english,math) values(6,'張進寶',55,85,45);
insert into students(id,name,chinese,english,math) values(7,'黃蓉',85,75,80);
insert into students(id,name,chinese,english,math) values(8,'張1李',75,65,30);
insert into students(id,name,chinese,english,math) values(9,'何李',75,65,30);
insert into students(id,name,chinese,english,math) values(10,'單',75,65,30);
insert into students(id,name,chinese,english,math) values(11,'李',75,65,NULL);
insert into students(id,name,chinese,english,math) values(12,'jack',75,65,40);
insert into students(id,name,chinese,english,math) values(13,'marry',75,65,60);
查詢表中所有學生的信息,*表示所有字段,順序與表結構相同------------------------------用students.sql表
select id,name,chinese,math,english from students;
select name,id,chinese,math,english from students;
select name,chinese,math,english,id from students;
select * from students;
*號雖然寫好起來方便,但充滿不肯定因素,要慎用
查詢表中所有學生的姓名和對應的英語成績
select name,english from students;
select name,english from students;
過濾表中重復語文成績distinct(區分的)放在單列名前面,可以對該列名重復你元素進行挑選,僅僅保存1個
select distinct chinese from students;
select distinct chinese from students;
在所有學生分數上加10分特長分
select name,chinese+10,math+10,english+10 from students;
select name,chinese+10,math+10,english+10 from students;
NULL與任何數值進行運算,結果為NULL
統計每一個學生的總分
select name,chinese+math+english from students;
select name,chinese+math+english from students;
**使用(別名)表示學生分數,注意使用別名加上雙引號
select name "姓名",chinese+math+english "總分" from students;
select name "姓名",chinese+math+english "總分" from students;
查詢姓名為'張小明'的學生成績 注意字符串用單引號'' 條件用where
select id,name,chinese,math,english from students where name = '張小明';
select * from students where name = '張小明';
查詢英語成績大于90分的同學
select id,name,chinese,math,english
from students
where english > 90;
select * from students where english>90;
查詢總分大于200分的所有同學
select id,name,chinese,math,english
from students
where chinese+math+english > 200;
查詢數學分數為89或90或91的同學
方式1:
select name,math
from students
where (math=89) or (math=91) or (math=90);
方式2:(推薦)
select name,math
from students
where math in (91,89,90,100,10000);//即便加上1些不存在的值也沒問題
查詢英語分數在 80-90之間的同學,包括80和90
方式1:
select name,english
from students
where (english>=80) and (english<=90);
方式2:
select name,english
from students
where english between 80 and 90;
select name,english from students where english between 80 and 90;
查詢所有姓'李'的學生成績,%表示0或多個字符(模糊查詢)
select name,english,math,english
from students
where name like '李%';
select * from students where name like '李%';
=表示精確比較
模糊比較,like關鍵字
查詢所著名'李'的學生成績
select name,english,math,english
from students
where name like '%李';
查詢所有姓名中包括’李’的學生成績
select name,english,math,english
from students
where name like '%李%';
以下3條SQL都表示查詢表中的[所有]記錄
select name,english,math,english
from students
where name like '%';
select name,english,math,english
from students
where name like '%%';
select name,english,math,english
from students
where name like '%%%';
查詢所有姓'李'的學生成績,但姓名必須是3個字符,_表示1個字符
select *
from students
where name like '李__';
select * from students where name like '李__';
查詢數學分>80且語文分>80的同學
select *
from students
where 1=1 and
(math>80) and
(chinese>80);
select * from students where 1=1 and (math>80) and (chinese>80);
---------------------------------------------
對數學成績排序(降序)后輸出
select id,name,math
from students
order by math desc;
select id,name,math from students order by math desc;
desc表示降序,排序字段用數值型
select id,name,math
from students
order by math asc;
asc表示升序,不寫asc和desc默許是升序
**對總分排序(降序)后輸出
SELECT name "姓名",math+chinese+english "總分"
From students
order by (math+chinese+english) desc;
select name "姓名",math+chinese+english "總分" from students order by (math+chinese+english) desc;
擴大:通常<order by>后面可以跟以下內容:
1)字段
order by math desc
2)表達式
order by (math+chinese+english) desc
3)別名(這個別名可以不加引號)
select name "姓名",math+chinese+english "總分"
from students
order by 總分 desc;
4)列號,即列在select中出現的位置,從1開始排序
select name "姓名",math+chinese+english "總分"
from students
order by 2 desc;
對姓'李'的學生總分排序(降序)輸出
select name "姓名",(chinese+math+english) "總分"
from students
where name like '李%'
order by 2 desc;
--
select name "姓名",(chinese+math+english) "總分" from students where name like "李%" order by 2 desc;
查詢數學分為NULL的學生
select name,math
from students
where math is null;
select name,math from students where math is null;
select name,math
from students
where math is not null;
還有not in,not between and,is not
-------------------------------------------------------------------------------------------------------
統計函數:統計函數會把null值得排除掉
統計1個班級共有多少學生
select count(*) "總人數"
from students;
select count(*) "總人數" from students;
不提倡用*號,而用非NULL唯1列,即id(主健)
select count(id)
from students;
select count(id) from students;
select count(math)
from students;
建議不要統計含有NULL的列值
統計數學成績大于80的學生有多少個
select count(id)
from students
where math>80;
select count(id) from students where math>80;
統計總分大于250的人數有多少
select count(id)
from students
where (math+chinese+english)>250;
-----
統計1個班級數學總成績
select sum(math) "數學總成績"
from students;
select sum(math) "數學總成績" from students;
統計1個班級語文、英語、數學各科的總成績
select sum(math) "數學總成績",sum(english) "英語總成績",sum(chinese) "語文總成績"
from students;
統計1個班級語文、英語、數學的成績總和
select sum(chinese+math+english) "總成績"
from students;
------
統計1個班級語文成績平均分
select avg(chinese) "語文平均分"
from students;
select avg(chinese) "語文平均分"
from students;
求1個班級數學平均分
select avg(math) "數學平均分"
from students;
求1個班級總分平均分
select avg(math+chinese+english) "班級總分平均分"
from students;
求班級最高分和最低數學分數
select max(math) "數學最高分",min(math) "數學最低分"
from students;
回顧:
count() 統計總數
sum() 求和
avg() 平均
max() 最值
min()
-------------------------------------------------------------------------------------------------------
orders表:
drop table if exists orders;
create table if not exists orders(
o_id int,
o_product varchar(20),
o_price int
);
insert into orders values(1,'電視',900);
insert into orders values(2,'洗衣機',100);
insert into orders values(3,'洗衣粉',90);
insert into orders values(4,'桔子',10);
insert into orders values(5,'洗衣粉',80);
對定單表中商品歸類后,顯示每類商品的總價------------------------------用orders.sql表
select o_product "商品",sum(o_price) "總價"
from orders
group by o_product;
查詢購買了幾類商品,并且每類總價大于100的商品,即分類后,還要過濾
select o_product "商品",sum(o_price) "總價"
from orders
group by o_product
having sum(o_price) > 100;
小結:
where:
行過濾器
針對原始記錄,即沒有分組的記錄
可以不出現
通常出現在where后面
先履行where
having:
組過濾器
針對分組后的記錄
可以不出現
通過出現在group by 后面
后履行having
查詢購買了幾類商品,并且每類總價大于100的商品,同時依照總價的降序排列
select o_product "商品",sum(o_price) "總價"
from orders
group by o_product
having sum(o_price) > 100
order by 2 desc;
小結:
select子句
from 子句
where子句
group by子句
having子句
order by子句
在MySQL
數據庫服務器中,上面的所有子句,哪一個是必須寫的呢?
答:select