MySQL基本语法
mysql數(shù)據(jù)庫的操作
數(shù)據(jù)庫操作
1.創(chuàng)建數(shù)據(jù)庫
(1).character set:指定數(shù)據(jù)庫采用的字符集,如果不指定字符集,默認(rèn)時候是utf8
(2).collate:指定數(shù)據(jù)庫字符集的校對規(guī)則 (常用utf8_bin、utf8_general_ci注意默認(rèn)是utf8_general_ci)
create database mysql01;//默認(rèn)創(chuàng)建數(shù)據(jù)庫表mysql01create database mysql01 default character set utf8;//創(chuàng)建指定默認(rèn)字符集為交的數(shù)據(jù)庫show character set;//查看可用字符集名稱show collation;//查看可用的排序規(guī)則名稱create database student character set utf8 default collate utf8_bin; //創(chuàng)建指定的默認(rèn)字符集utf8,排序規(guī)則為utf8_bin的數(shù)據(jù)庫(student)2.查看、刪除和修改數(shù)據(jù)庫
(1).使用數(shù)據(jù)庫:use 數(shù)據(jù)庫名稱;
(2).修改數(shù)據(jù)庫:使用alter database語句可以修改一個已有的數(shù)據(jù)庫的默認(rèn)字符集和排序規(guī)則。
(3)刪除數(shù)據(jù)庫:drop database mysql01;
? 每次只能刪除一個數(shù)據(jù)庫。
show databases;//查看所有數(shù)據(jù)庫show create databases;//查看數(shù)據(jù)庫屬性use mysql01;//使用數(shù)據(jù)庫alter database mysql01 character set gbk; //將指定的utf8的數(shù)據(jù)庫修改為gbk的數(shù)據(jù)庫alter database student character set gb2312 default collate gb2312_chinese_ci; //將student數(shù)據(jù)庫的默認(rèn)字符集修改為gb2312,排序規(guī)則修改為gb2312_chinese_cidrop database mysql01;//刪除數(shù)據(jù)庫Mysql常用數(shù)據(jù)類型
1.數(shù)值類型
int(4個字節(jié)) float(單精度 4個字節(jié)) double(雙精度 8個字節(jié)) decimal(M,D)(大小不確定)
decimal(M,D)精度高 可以用于財務(wù)和貨幣 內(nèi)存和分配不一樣
int not null default 0不允許就空默認(rèn)為0
create table student(id int(5),name varchar(10),age int not null default 20); //創(chuàng)建一個學(xué)生表編號為整型5個字節(jié),名字為 變長字符串類型10個字節(jié),年齡不允許為空默認(rèn)為202.字符串類型
char(定長字符串 0-255) Varchar( 變長字符串0-65535) text(0-2^16-1)
char和varchar的區(qū)別:
char:定長字符串在分配數(shù)據(jù)的存儲空間是固定的
varchar:它會根據(jù)數(shù)據(jù)的實際長度動態(tài)的改變存儲值的長度,所以不能確定字段需要多少字符
就可以使用varchar大大節(jié)省了磁盤空間。
3.日期和時間類型
date[日期 年月日] time[時間 時分秒] datetime[年月日 時分秒 YYYY-MM-DD HH:mm:ss]
timestamp[時間戳] year[年]
注意:沒有在time類型加冒號,理解為持續(xù)的的時間,而不是一個時間段
系統(tǒng)在年份中,將00-69范圍內(nèi)的年份轉(zhuǎn)化為2000-2069
把70-99范圍內(nèi)的年份值轉(zhuǎn)化為1970-1999
4.復(fù)合類型
enum 枚舉類型
表示:從一個集合選取一個值,猶似一個單選題
比如性別enum(“男”,“女”)
集合中每一個值都是字符串類型的,他們都是都好隔開
set可以預(yù)定義的集合中取得任意數(shù)量的值
set(“值1”,“值2”…)多項選擇器 可以選擇多個或者一個 ,set不可以包含兩個相同的元素
表操作
1.創(chuàng)建表
create table shop(id int(10),goods_name varchar(10),price double(10));創(chuàng)建表2.查看表
show tables;3.查看表結(jié)構(gòu)
desc 表名;4.查看建表語句
show create table;5.快速創(chuàng)建一個表結(jié)構(gòu)相同的表
create table 新表名 like 舊表名;6.刪除表
drop table 表名;7.刪除表,如果表結(jié)構(gòu)存在的話
drop table if exits 表名;修改表結(jié)構(gòu)
8.添加字段
alter table 表名 add [column] 字段名 類型;9.刪除字段
alter table 表名 drop 字段名;10.修改字段名
alter table 表名 change 舊字段名 新字段名 類型;11、修改字段類型
alter table 表名 modify 字段 新字段類型;12.修改表名
rename table 表名 to 新表名;13.修改表的字符集
alter table 表名 character set 字符集;增刪改查
insert(添加數(shù)據(jù))
插入全部數(shù)據(jù)
insert into 表名 values (值1,值2);值的順序一定要與字段對應(yīng)
insert into user values('張三','30','133324','男');插入部分?jǐn)?shù)據(jù)
insert into 表名 (字段名1,字段名2,字段名3,...)values(值1,值2,值3,...); insert into user(name,age,modile) values('小艾',20,'243252'); insert into 表名(字段名1,字段名2,字段名3,...)values(值1,值2,值3,...),(值1,值2,值3,...)...;復(fù)制表
目標(biāo)表必須存在,并且表結(jié)構(gòu)要與源表一致。
insert into 目標(biāo)表名 select*from 源表名;update(修改數(shù)據(jù))
更新表記錄
update 表名 set 字段名1 = 值1, 字段名2 = 值2,......更新全部記錄
update user1 set gender = '男';更新時加入運算
update user1 set age = age+1;部分更新:
update 表名 set 字段名1 = 值1,字段名2 = 值2,...where 字段條件 update user1 set sex='女' where name = '張三';delete(刪除數(shù)據(jù))
刪除記錄
delete from 表名; 刪除全部記錄 delete from 表名 where 字段條件; 按條件刪除 delete from user1 where name = '張三'; delete from user1 where age<20; delte from user1 where age<23 and name = '小天'; delete from user1; 刪除表內(nèi)全部數(shù)據(jù)刪除全部記錄
truncate 表名; truncate user1;delete與truncate的區(qū)別
delete是將表中的數(shù)據(jù)逐行清除。如果表里的數(shù)據(jù)過多速度會很慢,但是它支持按條件刪除
truncate是直接將表中的數(shù)據(jù)物理刪除,刪除速度快和表里有多少數(shù)據(jù)無關(guān),它不支持條件刪除
查看mysql的字符集設(shè)置
show varables like '%character%';設(shè)置字符集編碼集編碼:
set character_set_client = 字符集名稱 set character_set_connection = 字符集名稱 set character_set_ results = 字符集名稱set character_set_client = gbk; set character_set_connection = gbk; set character_set_results = gbk;select(查找數(shù)據(jù))
查詢語句
select 關(guān)鍵字用來查詢?nèi)〕霰碇械臄?shù)據(jù)簡單查詢:
select *from 表名;取出表中的所有數(shù)據(jù)查詢指定列:
select 字段名1,字段名2,.... from 表名查詢指定列帶別名:
select 字段名1 as 別名 ,字段名2 as 別名,.....消除重復(fù)值:
distinct關(guān)鍵字用來去除重復(fù)值 只能寫道select后面,一個select只能有一個distinct
distinct 會根據(jù)查詢字段的數(shù)據(jù)做組合操作,如果合并后的數(shù)據(jù)和前面的數(shù)據(jù)沒有沖突,那就顯示。
如果有沖突。那就不顯示。
distinct并沒有去掉表中的重復(fù)數(shù)據(jù)。只是去掉了結(jié)果集中的重復(fù)數(shù)據(jù)。
如果去除表中的重復(fù)數(shù)據(jù),步驟如下、
? 1.先創(chuàng)建一個和原表結(jié)構(gòu)相同的表
? 2.用insert into 目標(biāo)表 select distinct from 原表
? 3.刪除原表或為安全起見將原表改名
? 4.將目標(biāo)表的名字改為源表名
? 這樣就完成了一個對表的去重
查詢結(jié)果參與運算:
CREATE TABLE student (id int,name varchar(20),age int,sex varchar(5),address varchar(100),math int,english int );INSERT INTO student (id, NAME, age, sex, address , math, english) VALUES (1, '馬云', 55, '男', '杭州' , 66, 78),(2, '馬化騰', 45, '女', '深圳' , 98, 87),(3, '馬景濤', 55, '男', '香港' , 56, 77),(4, '柳巖', 20, '女', '湖南' , 76, 65),(5, '柳青', 20, '男', '湖南' , 86, NULL),(6, '劉德華', 57, '男', '香港' , 99, 99),(7, '馬德', 22, '女', '香港' , 99, 99),(8, '德瑪西亞', 18, '男', '南京' , 56, 65);用到的表 select name,english+10 from student; 注意,這里面的null值是不參與 運算的,但也不會報錯,會被忽略 select name,ifnull(esnglish ,0) as 英語 from student;where 關(guān)鍵字 按條件查詢
相關(guān)運算符: < 小于 = 等于 >大于 >=大于等于 <=小于等于 != 或 <>不等于
英語不等于77 select * from student where english !=77; math和英語都大于60的 select *from student where math>60 and ehglish>60; 數(shù)學(xué)大于60 或者英語大于 80 select * from student where math>60 or english>80;邏輯運算符: and (&&)與 并且 or(||)或 not(!)非
select * from student where math not in(66,76);范圍關(guān)鍵字 between and
between 值1 and 值2 取出來的結(jié)果集范圍包含 值1 和 值2
select * from student where math between 56 and 66;等同于
select *from student where math >= 56 and math <=66;模糊查詢關(guān)鍵字:
%通配符 代表任意長度的任意字符
like like ‘馬%’
select * from student where name like '馬%'; select * from student where name like '%德'; select * from student where name like '%德%';_通配符 代表一個字符長度的任意字符 用法與%一樣,只不過只代表一個字符
select * from student where name like ‘馬_’; select * from student where name like ‘馬__’; select * from student where name like ‘_巖’;排序關(guān)鍵字
order by [asc] [desc] asc 按升排序 desc 降序排序
單列排序
select * from student order by math; asc默認(rèn)可以不寫 select * from student order by math desc; desc 必須寫上組合排序
order by 字段1 [asc|desc], 字段2 [asc|desc]
select * from student order by math desc, english desc;聚合函數(shù):
? count 統(tǒng)計數(shù)量
? count(*) 統(tǒng)計共有多少行
? count(字段名)
select count(*) as 行數(shù) from student;min(字段名) 函數(shù):
? 求字段的最小值。
select min(math) from student;max(字段名) 函數(shù)
? 求字段的最大值。
select max(math) from student;?
avg 求某列的平均值
select avg(ifnull(english,0)) as 平均分 from student; --因為NULL不計入計算,所必須給null一個0值select avg(math) from student; --沒有空的列可以直接進行計算。?
sum(字段名) 求合
select sum(english) from student; select sum(ifnull(english,0)) from student;分組關(guān)鍵字
group by 將數(shù)據(jù)進行 分組
select sum(sex) from student group by sex; 按性別進行分組計算。執(zhí)行順序是先進行分組,然后再按組對字段進行計算
按條件進行篩選
having關(guān)鍵字
select math,count(*) from student group by math having count(*)>1;執(zhí)行順序:
where 在group by 之前執(zhí)行
having 在group by 之后執(zhí)行
先分組,在count()統(tǒng)計,再按條件顯示
分頁
分頁關(guān)鍵字limit
limit 一個數(shù)字 ,限制條數(shù)
limit 數(shù)字1,數(shù)字2, 數(shù)字1加上1是開始的行位置,數(shù)字2要取的記錄條數(shù)
注意查詢時候列的起始值為0,不是為1
select *from student limit 0,2;主鍵
表中值不重復(fù)的字段,可以作為記錄的唯一標(biāo)識
create table user2(id int primary key,name varchar(10));自增 auto_increment
create table user3(id int primary key auto_increment,name varchar(10));給沒有主鍵的表添加主鍵
alter table user add id int primary key auto_increment;約束
? 唯一
? 關(guān)鍵字 unique
create table user5 (id int primary key auto_increment,name varchar(10),mobile varchar(11) unique --此字段值唯一 )? 非空
? 關(guān)鍵字 not null
create table user5 (id int primary key auto_increment,name varchar(10),mobile varchar(11) unique, --此字段值唯一sex varchar(2) not null )? 默認(rèn)值
? 關(guān)鍵字 default
create table user5 (id int primary key auto_increment,name varchar(10),mobile varchar(11) unique, --此字段值唯一sex varchar(2) not null,age int default 18 )嵌套查詢
select 語句可以把語句查詢出來的結(jié)果集當(dāng)成表來再次查詢
select name from (select * from user_db where sex = ‘女’)as nigu;
union 關(guān)鍵字
將兩個結(jié)果集上下拼接起來。要求兩個結(jié)果集的表結(jié)構(gòu)一致
select *from student where sex ='女' union select * from student where name = '馬化騰';表之間的關(guān)系
一對多:
? 用戶表: 購物記錄表:
? id name id userid spname
? 100 張三 1 100 小米手機
? 101 李四 2 100 蘋果手機
? 3 101 巧克力
? 4 100 華為手機
多對多:
雙向一對多,就是多對多。? 用戶表: 中間表 購物記錄表:
? id name id userid gwid id userid spname
? 100 張三 1 100 1 1 100 小米手機
? 101 李四 2 101 3 2 100 蘋果手機
? 3 100 4 3 101 巧克力
? 4 100 2 4 100 華為手機
一對一:
? 一張表里的一條記錄,對應(yīng)另一張表里的一條記錄
? 用戶表: 用戶頭像表:
? id name id userid Picture
? 100 張三 1 100 1.png
? 200 李四 2 200 2.png
外鍵
一個表的某個字段,是另一張表的主鍵
外鍵約束
外鍵表在入更新數(shù)據(jù)時,會從主檢查表是否存在,如果不存在,則插入更新失敗。
如何創(chuàng)建外鍵:
constraint: 約束
? froeign : 外鍵
? references: 引用
? constraint: 外鍵名稱 foreign key 當(dāng)前表的外鍵字段 references 主鍵表主鍵字段
? 創(chuàng)建主表: 數(shù)據(jù)復(fù)制自 testdb.student表
create table user6 (id int primary key auto_increment,---主鍵name varchar(10) );? 從表:
create table sp (id int primary key auto_increment,---主鍵spname varchar(30),userid int,constraint spuserid foreign key (userid) references user6(id) );將某鍵改為主鍵:
alter table user add primary key(id)將某鍵改為主鍵并自增:
alter table user change id id int not null auto_increment;外鍵約束:
從表里如果插入的數(shù)據(jù)不包含在主表的主鍵里,那就插入失敗
如果從表數(shù)據(jù)已經(jīng)包含了主表的信息,那主表刪除對應(yīng)主鍵的記錄時會失敗
如果要刪除主表的數(shù)據(jù),必須先將從表里的關(guān)聯(lián)的數(shù)據(jù)全部刪除之后才能刪除
刪除外鍵方法
alter to table 表名 drop forrign key 外鍵名alter to table sp drop foreign key spuserid刪除外鍵就是刪除兩個之間的關(guān)系
多表查詢
笛卡爾積:
進行多表查詢時,數(shù)據(jù)庫會將兩張表的數(shù)據(jù)進行組合。笛卡爾積會生成無意義的數(shù)據(jù)。
如果要必免生成笛卡爾積,那就要在確認(rèn)關(guān)聯(lián)字段,使用條件過濾。
內(nèi)連接:
內(nèi)連接,查詢兩張表的交集數(shù)據(jù)
? 1.隱式內(nèi)連接
select a.name,b.spname from user6 as a,sp as b where a.id=b.userid;? 2.顯示內(nèi)連接
select * from sp as a inner join user6 as b on a.userid=b.id;外連接
? 1.左外連接
select 字段名1,字段名2,... from 表1 left [outer] join 表2 on 條件? 2.右外連接
select 字段名1,字段名2,... from 表1 right [outer] join 表2 on 條件grant 賦權(quán)語句
revoke 收回用戶權(quán)限語句
備份還原
mysqldump 備份數(shù)據(jù)庫
登錄之前在命令提示抽口執(zhí)行
mysqldump -u root -p 數(shù)據(jù)庫名 > 備份出的文件要保存的位置
source 還原是數(shù)據(jù)庫
登錄數(shù)據(jù)庫后執(zhí)行
source D:/testdb.sql
范式
1.什么是范式
? 范式就是數(shù)據(jù)庫建表的規(guī)范
? 常用的范式有一范式,二范式,三范式
一范式
? 每個字段儲存的數(shù)據(jù)都是不可再分的原子數(shù)據(jù)
二范式
一張表只能描述一件事,如果不滿足就需要進行拆分
所有的字段都必須依賴于主鍵,也就是說,每行都有一個唯一標(biāo)識
三范式
從表的外鍵必須使用主表的主鍵
視圖
1.什么是視圖
? 視圖是一句select查詢語句。是一張?zhí)摂M表
2.如何創(chuàng)建視圖
? 語法:
create [ or replace][algorithm] = {undefinde| merge | temptable} view 視圖名稱 [列名1,列名2,列名3......] as select 語句[with check option]整個表作為視圖
部分表作為視圖
指定視圖的列名
3.對視圖進行操作時的注意事項
視圖可以當(dāng)作表來用,支持用戶對視圖中的數(shù)據(jù)進行查詢操作,DML操作
不是所有的視圖都可以進行dml操作
例如創(chuàng)建視圖的語句包含有下列關(guān)鍵字,那就無法將其當(dāng)作表來進行更新操作
4.刪除視圖
drop view 視圖名稱;表與視圖的區(qū)別
? 視圖已編譯好的sql語句,而表不是
2.視圖沒有實際的物理記錄,而表有
3.表是內(nèi)容,視圖是窗口
4.表占用物理空間,而視圖不占用,它只是邏輯存在
5.表可以及時修改,而視圖只能由創(chuàng)建語句來進行修改
6.視圖是查看 數(shù)據(jù)表的方法 從安全角度來看,視圖可以只給用戶看到表的部分字段,
從而不知道表的結(jié)構(gòu)
7.表是全局中的表,是實表,視圖局部模式,是虛表
8.視圖的創(chuàng)建和刪除只影響視圖本身,而不會影響到基本表
視圖的優(yōu)缺點
? 優(yōu)點:
? 1.使用視圖可以定制用戶數(shù)據(jù),聚焦特定數(shù)據(jù)
? 2.使用視圖可以簡化數(shù)據(jù)庫的操作
? 3.使用視圖對基表有一定的安全性
? 4.使用視圖可以合并分離數(shù)據(jù),也可以創(chuàng)建分區(qū)視圖
? 缺點:
? 1.視圖有修改限制
? 2.視圖的性能差
索引
1.什么是索引及索引的作用
? 索引相當(dāng)于字典中的目錄,使用索引可以提高數(shù)據(jù)查詢的速度
? 索引本質(zhì)是一張地址表,它里面報訊了主鍵與索引字段的地址
? 經(jīng)常變動的列不適合建立索引
? 數(shù)據(jù)分布均勻的列不適合建立索引
? 記錄太少的表不適合建立索引
2.創(chuàng)建普通索引
create index 索引名字 on 表名(列名)3.創(chuàng)建唯一索引
create unique index 索引名字 on 表名(列名);4.創(chuàng)建復(fù)合索引
index 索引名字 on 表名 (列名,列名2);5.使用alter語句也可以給表添加或刪除索引
6.刪除索引
drop index [索引名] on 表名7.顯示表中有那些索引
show index from 表名\G索引基礎(chǔ)使用
普通索引 查詢條件里加上建立索引的列
select *from student where name = '馬云'復(fù)合索引 查詢條件里的字段順序要和創(chuàng)建索引時的順序一致
select * from student where name =’馬云‘ and math = 80;如果選擇要顯示的字段,那順序也要一致
select name , math from student where name = '馬云' and math = 80;復(fù)合索引創(chuàng)建時排一位的字段叫先導(dǎo)列,先導(dǎo)列的順序要排前面
查詢時如果條件上面有建立索引,那就會自動走索引
系統(tǒng)函數(shù)
字符串函數(shù)
字符串函數(shù)
合并字符串函數(shù) concat(s1,s2,…)
返回字符串長度 char_length(str);
返回字節(jié)長度 length(str);
將小寫字母轉(zhuǎn)為大寫字母 ucase(str) 或 upper(str)
將大寫字母轉(zhuǎn)為小寫 lcase(str) 或 lower(str)
去空格 trim(str) ltrim() rtrim()
字符替換 replace(s,s1,s2); s要操作的字符串,s1要替換的字符串,s2要替換成的字符串
字符串截取 substr(s,strat,length); s要操作的字符串,start開始位置,length長度
字符串比較 strcmp(str1,str2); str1大返回1 str1小返回-1
日期時間函數(shù)
now() 或curdate() curtime()
now()返回日期和時間
curdate()返回年月日
curtime()返回時分秒
year(now()或字段名) month(now()或字段名) day(now()或字段名)
last_day(now()或字段名) 返回一個月中的最后一天
adddate(日期,天數(shù)) 返回當(dāng)前日期+天數(shù)后的日期
subdate(日期,天數(shù)) 返回當(dāng)前日期-天數(shù)后的日期
quarter(日期) 返回日期為一年中的第幾個季度
datediff(日期1,日期2) 返回兩個日期之間差的天數(shù) (注意日期一定要用引號引起來)
date_format(日期,’%Y-%m-%d’) 按指定格式顯示日期。
數(shù)字相關(guān)的函數(shù)
ABS(X) 求絕對值
ceil(x) 向上取整
floor(x)向下取整
mod(x,y) 取余數(shù)
RAND() 返回0到1之間的隨機數(shù)。
Round(任意小數(shù)) 四舍五入
truncate(x,y) 返回數(shù)值x 保留到小數(shù)點后y位的值。
高級函數(shù)
selectcase 字段when 判斷條件1then 希望得到值1when 判斷條件then 希望得到的值2........else前面的條件都不滿足時的值endfrom 表 select case name when '馬云' then '阿里巴巴' when '馬化騰' then '騰訊'else ‘其它’ end,math,english from student;trim(str) ltrim() rtrim()
字符替換 replace(s,s1,s2); s要操作的字符串,s1要替換的字符串,s2要替換成的字符串字符串截取 substr(s,strat,length); s要操作的字符串,start開始位置,length長度字符串比較 strcmp(str1,str2); str1大返回1 str1小返回-1日期時間函數(shù)
now() 或curdate() curtime()
now()返回日期和時間
curdate()返回年月日
curtime()返回時分秒
year(now()或字段名) month(now()或字段名) day(now()或字段名)
last_day(now()或字段名) 返回一個月中的最后一天
adddate(日期,天數(shù)) 返回當(dāng)前日期+天數(shù)后的日期
subdate(日期,天數(shù)) 返回當(dāng)前日期-天數(shù)后的日期
quarter(日期) 返回日期為一年中的第幾個季度
datediff(日期1,日期2) 返回兩個日期之間差的天數(shù) (注意日期一定要用引號引起來)
date_format(日期,’%Y-%m-%d’) 按指定格式顯示日期。
數(shù)字相關(guān)的函數(shù)
ABS(X) 求絕對值
ceil(x) 向上取整
floor(x)向下取整
mod(x,y) 取余數(shù)
RAND() 返回0到1之間的隨機數(shù)。
Round(任意小數(shù)) 四舍五入
truncate(x,y) 返回數(shù)值x 保留到小數(shù)點后y位的值。
高級函數(shù)
selectcase 字段when 判斷條件1then 希望得到值1when 判斷條件then 希望得到的值2........else前面的條件都不滿足時的值endfrom 表 select case name when '馬云' then '阿里巴巴' when '馬化騰' then '騰訊'else ‘其它’ end,math,english from student;總結(jié)
- 上一篇: 2021 年 Linux 界的 12 件
- 下一篇: db2与mysql语法区别_db2和my