MySQL 學習筆記 一
轉載原文:https://www.cnblogs.com/hoojo/archive/2011/06/20/2085390.html
MySQL 學習筆記 一
一、資料庫簡單介紹
1、 按照資料庫的發展時間順序,主要出現了以下型別資料庫系統:
Ø 網狀型資料庫
Ø 層次型資料庫
Ø 關係型資料庫
Ø 面向物件資料庫
上面4中資料庫系統中,關係型資料庫使用最為廣泛。面向物件資料庫則是由面嚮物件語言催生的新型資料庫,目前的一些資料庫系統,如:SQL Server 2005、Oracle10g等都開始增加面向物件的特性。
二、常用基本SQL語句/語法
ØSQL語句基礎理論
SQL是操作和檢索關係型資料庫的標準語言,標準SQL語句可用於操作然後關係型資料庫。
標準的SQL語句通常劃分為以下型別:
查詢語句:主要由於select關鍵字完成,查詢語句是SQL語句中最複雜,功能最豐富的語句。
DML(Data Munipulation Language,資料操作語言)語句,這組DML語句修改後資料將保持較好的一致性;操作表的語句,如插入、修改、刪除等;
DDL(Data Definition Language,資料定義語言)語句,操作資料物件的語言,有create、alter、drop。
DCL(Data Control Language,資料控制語言)語句,主要有grant、revoke語句。
事務控制語句:主要有commit、rollback和savepoint三個關鍵字完成
DDL語句
DDL語句是操作資料庫物件的語句,包括建立create、刪除drop、修改alter資料庫物件。
常見資料庫物件
物件名稱 |
對應關鍵字 |
描述 |
表 |
table |
表是資料庫儲存的邏輯單元,以行和列的形式存在;列是欄位,行就是一條資料記錄 |
資料字典 |
就是系統表,儲存資料庫相關資訊的表,系統表裡的資料通常有資料庫系統維護。系統表結構和資料,開發人員不應該手動修改,只能查詢其中的資料 |
|
檢視 |
view |
一個或多個數據表裡的資料的邏輯顯示,檢視就是一張虛擬的表,並不真正儲存資料 |
約束 |
constraint |
執行資料檢驗規則,用於保證資料完整性的規則 |
索引 |
index |
用於提高查詢效能,相當於書的目錄 |
函式 |
function |
用於完成一個特定的計算,具有返回值和引數 |
儲存過程 |
procedure |
完成某項完整的業務處理,沒有返回值,但可通過傳出引數將多個值傳個呼叫環境 |
觸發器 |
trigger |
相當於一個事件的監聽器,當資料庫發生特定的事件後,觸發器被觸發,完成響應處理 |
上面的物件都可以通過用create、alter、drop完成相關的建立、修改、刪除操作。
常用資料型別
列型別 |
說明 |
tinyint/smallint/mediumint int(integer)/bigint |
1位元組、2位元組、3位元組、4位元組、8位元組整數,又可分有符號和無符號兩種。這些整數型別的區別僅僅表現範圍不同 |
float/double |
單精度、雙精度浮點型別 |
decimal(dec) |
精確小數型別,相當於float和double不會產生精度丟失問題 |
date |
日期型別,不能儲存時間。當Java裡的Date物件儲存到該型別中,時間部分丟失 |
time |
時間型別,不能儲存日期。當Java的Date物件的儲存在該型別中,日期部分丟失 |
datetime |
日期、時間型別 |
timestamp |
時間戳型別 |
year |
年型別,僅儲存年份 |
char |
定長字串型別 |
varchar |
可變長度字串型別 |
binary |
定長二進位制字串型別,它以二進位制形式儲存字串 |
varbinary |
可變長度的二進位制字串型別,二進位制形式儲存字串 |
tingblob/blob mediumblob/longblob |
1位元組、2位元組、3位元組、4位元組的二進位制大物件,可存儲存超圖片、音樂等二進位制資料,分別可儲存:255/64K/16M/4G的大小 |
tingtext/text mediumtext/longtext |
1位元組、2位元組、3位元組、4位元組的文字物件,可儲存超長長度的字串,分別可儲存:255/64K/16M/4G的大小的文字 |
enum(‘val1’, ‘val2’, …) |
列舉型別,該列的值只能是enum括號中出現的值的之一 |
set(‘value1’, ‘value2’, …) |
集合型別,該列的值可以是set中的一個或多個值 |
Ø 常用查詢
MySQL結束符是“;”結束。
1、 顯示所有資料庫
show databases;
2、 刪除資料庫
drop database dbName;
3、 建立資料庫
create database [if not exists] dbName;
中括號部分可選的,判斷該資料不存在就建立
4、 切換、使用指定資料庫
use dbName;
5、 顯示當前使用資料庫所有的表物件
show tables;
6、 顯示錶結構describe(desc)
desc tableName;
7、 建立一張表
create table user (
--int 整型
uId int,
--小數
uPrice decimal,
--普通長度文字,default設定預設值
uName varchar(255) default ‘zhangsan’,
--超長文字
uRemark text,
--圖片
uPhoto blob,
--日期
uBirthday datetime
);
8、 子查詢建表方法
部分列名匹配模式:
create table userInfo (
name varchar(20),
sex char
)
as
select name, sex from user;
上面的列名和子查詢的列名以及型別要對應
全部列名模式:
create table userInfo
as
select * from user;
直接將整個表的型別和資料備份到新表userInfo中
9、 新增表字段
新增單列
alter table user add tel varchar(11) default ‘02012345678’;
新增多列
alter table user
add (
photo blob,
birthday date
);
上面就同時增加了多列欄位
10、 修改表字段
修改tel列
alter table user modify tel varchar(15) default ‘02087654321’;
修改tel列的位置,在第一列顯示
alter table user modify tel varchar(15) default '02087654321' first;
修改tel列的位置,在指定列之後顯示
alter table user modify tel varchar(15) default '02087654321' after age;
注意:alter modify不支援一次修改多個列,但是Oracle支援多列修改
但是MySQL可以通過多個modify的方式完成:
alter table user
modify tel varchar(15) default '02087654321' first,
modify name varchar(20) after tel;
11、 刪除指定欄位
alter table user drop photo;
12、 重命名錶資料
表重新命名
alter table user rename to users;
欄位重新命名
alter table users change name u_name varchar(10);
alter table users change sex u_sex varchar(10) after u_name;
如果需要改變列名建議使用change,如果需要改變資料型別和顯示位置可以使用modify
13、 刪除表
drop table users;
drop刪除表會刪除表結構,表物件將不存在資料中;資料也不會存在;表內的物件也不存在,如:索引、檢視、約束;
truncate刪除表
truncate都被當成DDL出來,truncate的作用就是刪除該表裡的全部資料,保留表結構。相當於DDL中的delete語句,
但是truncate比delete語句的速度要快得多。但是truncate不能帶條件刪除指定資料,只會刪除所有的資料。如果刪除的表有外來鍵,
刪除的速度類似於delete。但新版本的MySQL中truncate的速度比delete速度快。
Ø 約束
MySQL中約束儲存在information_schema資料庫的table_constraints中,可以通過該表查詢約束資訊;
約束主要完成對資料的檢驗,保證資料庫資料的完整性;如果有相互依賴資料,保證該資料不被刪除。
常用五類約束:
not null:非空約束,指定某列不為空
unique: 唯一約束,指定某列和幾列組合的資料不能重複
primary key:主鍵約束,指定某列的資料不能重複、唯一
foreign key:外來鍵,指定該列記錄屬於主表中的一條記錄,參照另一條資料
check:檢查,指定一個表示式,用於檢驗指定資料
MySQL不支援check約束,但可以使用check約束,而沒有任何效果;
根據約束資料列限制,約束可分為:
單列約束:每個約束只約束一列
多列約束:每個約束約束多列資料
MySQL中約束儲存在information_schema資料庫的table_constraints中,可以通過該表查詢約束資訊;
1、 not null約束
非空約束用於確保當前列的值不為空值,非空約束只能出現在表物件的列上。
Null型別特徵:
所有的型別的值都可以是null,包括int、float等資料型別
空字串“”是不等於null,0也不等於null
create table temp(
id int not null,
name varchar(255) not null default ‘abc’,
sex char null
)
上面的table加上了非空約束,也可以用alter來修改或增加非空約束
增加非空約束
alter table temp
modify sex varchar(2) not null;
取消非空約束
alter table temp modify sex varchar(2) null;
取消非空約束,增加預設值
alter table temp modify sex varchar(2) default ‘abc’ null;
2、 unique
唯一約束是指定table的列或列組合不能重複,保證資料的唯一性。雖然唯一約束不允許出現重複的值,但是可以為多個null
同一個表可以有多個唯一約束,多個列組合的約束。在建立唯一約束的時候,如果不給唯一約束名稱,就預設和列名相同。
唯一約束不僅可以在一個表內建立,而且可以同時多表建立組合唯一約束。
MySQL會給唯一約束的列上預設建立一個唯一索引;
create table temp (
id int not null,
name varchar(25),
password varchar(16),
--使用表級約束語法,
constraint uk_name_pwd unique(name, password)
);
表示使用者名稱和密碼組合不能重複
新增唯一約束
alter table temp add unique(name, password);
alter table temp modify name varchar(25) unique;
刪除約束
alter table temp drop index name;
3、 primary key
主鍵約束相當於唯一約束+非空約束的組合,主鍵約束列不允許重複,也不允許出現空值;如果的多列組合的主鍵約束,
那麼這些列都不允許為空值,並且組合的值不允許重複。
每個表最多隻允許一個主鍵,建立主鍵約束可以在列級別建立,也可以在表級別上建立。MySQL的主鍵名總是PRIMARY,
當建立主鍵約束時,系統預設會在所在的列和列組合上建立對應的唯一索引。
列模式:
create table temp(
/*主鍵約束*/
id int primary key,
name varchar(25)
);
create table temp2(
id int not null,
name varchar(25),
pwd varchar(15),
constraint pk_temp_id primary key(id)
);
組合模式:
create table temp2(
id int not null,
name varchar(25),
pwd varchar(15),
constraint pk_temp_id primary key(name, pwd)
);
alter刪除主鍵約束
alter table temp drop primary key;
alter新增主鍵
alter table temp add primary key(name, pwd);
alter修改列為主鍵
alter table temp modify id int primary key;
設定主鍵自增
create table temp(
id int auto_increment primary key,
name varchar(20),
pwd varchar(16)
);
auto_increment自增模式,設定自增後在插入資料的時候就不需要給該列插入值了。
4、 foreign key 約束
外來鍵約束是保證一個或兩個表之間的參照完整性,外來鍵是構建於一個表的兩個欄位或是兩個表的兩個欄位之間的參照關係。
也就是說從表的外來鍵值必須在主表中能找到或者為空。
當主表的記錄被從表參照時,主表的記錄將不允許刪除,如果要刪除資料,需要先刪除從表中依賴該記錄的資料,
然後才可以刪除主表的資料。還有一種就是級聯刪除子表資料。
注意:外來鍵約束的參照列,在主表中引用的只能是主鍵或唯一鍵約束的列,假定引用的主表列不是唯一的記錄,
那麼從表引用的資料就不確定記錄的位置。同一個表可以有多個外來鍵約束。
建立外來鍵約束:
主表
create table classes(
id int auto_increment primary key,
name varchar(20)
);
從表
create table student(
id int auto_increment,
name varchar(22),
constraint pk_id primary key(id),
classes_id int references classes(id)
);
通常先建主表,然後再建從表,這樣從表的參照引用的表才存在。
表級別建立外來鍵約束:
create table student(
id int auto_increment primary key,
name varchar(25),
classes_id int,
foreign key(classes_id) references classes(id)
);
上面的建立外來鍵的方法沒有指定約束名稱,系統會預設給外來鍵約束分配外來鍵約束名稱,命名為student_ibfk_n,
其中student是表名,n是當前約束從1開始的整數。
指定約束名稱:
create table student(
id int auto_increment primary key,
name varchar(25),
classes_id int,
/*指定約束名稱*/
constraint fk_classes_id foreign key(classes_id) references classes(id)
);
多列外來鍵組合,必須用表級別約束語法:
create table classes(
id int,
name varchar(20),
number int,
primary key(name, number)
);
create table student(
id int auto_increment primary key,
name varchar(20),
classes_name varchar(20),
classes_number int,
/*表級別聯合外來鍵*/
foreign key(classes_name, classes_number) references classes(name, number)
);
刪除外來鍵約束:
alter table student drop foreign key student_ibfk_1;
alter table student drop foreign key fk_student_id;
增加外來鍵約束
alter table student add foreign key(classes_name, classes_number) references classes(name, number);
自引用、自關聯(遞迴表、樹狀表)
create table tree(
id int auto_increment primary key,
name varchar(50),
parent_id int,
foreign key(parent_id) references tree(id)
);
級聯刪除:刪除主表的資料時,關聯的從表資料也刪除,則需要在建立外來鍵約束的後面增加on delete cascade
或on delete set null,前者是級聯刪除,後者是將從表的關聯列的值設定為null。
create table student(
id int auto_increment primary key,
name varchar(20),
classes_name varchar(20),
classes_number int,
/*表級別聯合外來鍵*/
foreign key(classes_name, classes_number) references classes(name, number) on delete cascade
);
5、 check約束
MySQL可以使用check約束,但check約束對資料驗證沒有任何作用。
create table temp(
id int auto_increment,
name varchar(20),
age int,
primary key(id),
/*check約束*/
check(age > 20)
);
上面check約束要求age必須大於0,但沒有任何作用。但是建立table的時候沒有任何錯誤或警告。
Ø 索引
索引是存放在模式(schema)中的一個數據庫物件,索引的作用就是提高對錶的檢索查詢速度,
索引是通過快速訪問的方法來進行快速定位資料,從而減少了對磁碟的讀寫操作。
索引是資料庫的一個物件,它不能獨立存在,必須對某個表物件進行依賴。
提示:索引儲存在information_schema資料庫裡的STATISTICS表中。
建立索引方式:
自動:當表上定義主鍵約束、唯一、外來鍵約束時,該表會被系統自動新增上索引。
手動:手動在相關表或列上增加索引,提高查詢速度。
刪除索引方式:
自動:當表物件被刪除時,該表上的索引自動被刪除
手動:手動刪除指定表物件的相關列上的索引
索引類似於書籍的目錄,可以快速定位到相關的資料,一個表可以有多個索引。
建立索引:
create index idx_temp_name on temp(name);
組合索引:
create index idx_temp_name$pwd on temp(name, pwd);
刪除索引:
drop index idx_temp_name on temp;
Ø 檢視
檢視就是一個表或多個表的查詢結果,它是一張虛擬的表,因為它並不能儲存資料。
檢視的作用、優點:
限制對資料的訪問
讓複雜查詢變得簡單
提供資料的獨立性
可以完成對相同資料的不同顯示
建立、修改檢視
create or replace view view_temp
as
select name, age from temp;
通常我們並不對檢視的資料做修改操作,因為檢視是一張虛擬的表,它並不儲存實際資料。如果想讓檢視不被修改,可以用with check option來完成限制。
create or replace view view_temp
as
select * from temp
with check option;
修改檢視:
alter view view_temp
as
select id, name from temp;
刪除檢視:
drop view view_temp;
顯示建立語法:
show create view v_temp;
Ø DML語句
DML主要針對資料庫表物件的資料而言的,一般DML完成:
插入新資料
修改已新增的資料
刪除不需要的資料
1、 insert into 插入語句
insert into temp values(null, ‘jack’, 25);
主鍵自增可以不插入,所以用null代替
指定列
insert into temp(name, age) values(‘jack’, 22);
在表面後面帶括號,括號中寫列名,values中寫指定列名的值即可。當省略列名就表示插入全部資料,
注意插入值的順序和列的順序需要保持一致。
Set方式插入,也可以指定列
insert into temp set id = 7, name = 'jason';
MySQL中外來鍵的table的外來鍵引用列可以插入資料可以為null,不參照主表的資料。
使用子查詢插入資料
insert into temp(name) select name from classes;
多行插入
insert into temp values(null, ‘jack’, 22), (null, ‘jackson’ 23);
2、 update 修改語句
update主要完成對資料的修改操作,可以修改一條或多條資料。修改多條或指定條件的資料,需要用where條件來完成。
修改所有資料
update temp set name = ‘jack2’;
所有的資料的name會被修改,如果修改多列用“,”分開
update temp set name = ‘jack’, age = 22;
修改指定條件的記錄需要用where
update temp set name = ‘jack’ where age > 22;
3、 delete 刪除語句
刪除table中的資料,可以刪除所有,帶條件可以刪除指定的記錄。
刪除所有資料
delete from temp;
刪除指定條件資料
delete from temp where age > 20;
Ø select 查詢、function 函式
select查詢語句用得最廣泛、功能也最豐富。可以完成單條記錄、多條記錄、單表、多表、子查詢等。
1、 查詢某張表所有資料
select * from temp;
*代表所有列,temp代表表名,不帶條件就查詢所有資料
2、 查詢指定列和條件的資料
select name, age from temp where age = 22;
查詢name和age這兩列,age 等於22的資料。
3、 對查詢的資料進行運算操作
select age + 2, age / 2, age – 2, age * 2 from temp where age – 2 > 22;
4、 concat函式,字串連線
select concat(name, ‘-eco’) from temp;
concat和null進行連線,會導致連線後的資料成為null
5、 as 對列重新命名
select name as ‘名稱’ from temp;
as也可以省略不寫,效果一樣
如果重新命名的列名出現特殊字元,如“‘”單引號,那就需要用雙引號引在外面
select name as “名’稱” from temp;
6、 也可以給table去別名
select t.name Name from temp as t;
7、 查詢常量
類似於SQL Server
select 5 + 2;
select concat('a', 'bbb');
8、 distinct 去掉重複資料
select distinct id from temp;
多列將是組合的重複資料
select distinct id, age from temp;
9、 where 條件查詢
大於>、大於等於>=、小於<、小於等於<=、等於=、不等於<>
都可以出現在where語句中
select * from t where a > 2 or a >= 3 or a < 5 or a <= 6 or a = 7 or a <> 0;
10、 and 並且
select * from temp where age > 20 and name = ‘jack’;
查詢名稱等於jack並且年齡大於20的
11、 or 或者
滿足一個即可
select * from tmep where name = ‘jack’ or name = ‘jackson’;
12、 between v and v2
大於等於v且小於等於v2
select * form temp where age between 20 and 25;
13、 in 查詢
可以多個條件 類似於or
select * from temp where id in (1, 2, 3);
查詢id在括號中出現的資料
14、 like 模糊查詢
查詢name以j開頭的
select * from temp where name like ‘j%’;
查詢name包含k的
select * from temp where name like ‘%k%’;
escape轉義
select * from temp where name like ‘\_%’ escape ‘\’;
指定\為轉義字元,上面的就可以查詢name中包含“_”的資料
15、 is null、is not null
查詢為null的資料
select * from temp where name is null;
查詢不為null的資料
select * from temp where name is not null;
16、 not
select * from temp where not (age > 20);
取小於等於20的資料
select * from temp where id not in(1, 2);
17、 order by
排序,有desc、asc升序、降序
select * from temp order by id;
預設desc排序
select * from temp order by id asc;
多列組合
select * from temp order by id, age;