更新數(shù)據(jù)庫(kù)
- 數(shù)據(jù)庫(kù)名字不可以修改
- 數(shù)據(jù)庫(kù)的修改僅限庫(kù)選項(xiàng),即字符集和校對(duì)集(校對(duì)集依賴字符集)
alter database 數(shù)據(jù)庫(kù)名字[庫(kù)選項(xiàng)];
charset/character set[=]字符集
collate校對(duì)集
刪除數(shù)據(jù)庫(kù)
drop database數(shù)據(jù)庫(kù)名字;
表操作
新增數(shù)據(jù)表
create table[if not exists]表名(字段名字 數(shù)據(jù)類型),
········
字段名字 數(shù)據(jù)類型
)[表選項(xiàng)]
表選項(xiàng):控制表的表現(xiàn)
字符集:charset/character set具體字符集;--保證表中數(shù)據(jù)存儲(chǔ)的字符集
校對(duì)集:collate 具體校對(duì)集
存儲(chǔ)引擎:engine具體的存儲(chǔ)引擎(innodb和myisam)
方案一:
- 顯示地指定表所屬的數(shù)據(jù)庫(kù):
- create table 數(shù)據(jù)庫(kù)名.表明();
方案二:
- 隱式地指定表所屬數(shù)據(jù)庫(kù)
- 進(jìn)入數(shù)據(jù)庫(kù)環(huán)境:use 數(shù)據(jù)庫(kù)名字;
查看數(shù)據(jù)表
- 查看所有表:show tables;
- 查看部分表(模糊查詢):show tables like 'pattern';
pattern是匹配模式
%表示匹配多個(gè)字符
_表示匹配單個(gè)字符
查看表的創(chuàng)建語(yǔ)句:show create table表名;
查看表結(jié)構(gòu)(表中的字段信息):desc/describe/show columns from表名;
修改數(shù)據(jù)庫(kù)
修改表本身
修改表名;rename table舊表名to新表名;
修改表選項(xiàng)(字符集,校對(duì)集,存儲(chǔ)引擎都可以修改):alter table表名 表選項(xiàng)[=]值;
修改字段
刪除數(shù)據(jù)表:drop table表名1,表名2........;
字段操作
新增字段:alter table表名add[column]字段名數(shù)據(jù)類型[列屬性][位置];
first:第一個(gè)位置
after:在哪個(gè)字段之后:after 字段名;
修改字段:alter table 表名 modify 字段名數(shù)據(jù)類型 [列屬性] [位置];
重命名字段:alter table 表名 change 舊字段 新字段名 數(shù)據(jù)類型 [列屬性] [位置];
刪除字段:alter table 表名 drop 字段名;
數(shù)據(jù)操作
新增數(shù)據(jù)
方案一:
給全表字段插入數(shù)據(jù),不需要指定字段列表,要求數(shù)據(jù)的值出現(xiàn)的順序必須與表中設(shè)計(jì)的字段出現(xiàn)的順序一致,凡是非數(shù)值數(shù)據(jù),都需要使用引號(hào)(建議是單引號(hào))包裹insert into 表名 values(值列表)[,(值列表)];
方案二:
給部分字段插入數(shù)據(jù),需要選定字段列表,字段列表出現(xiàn)的順序與字段的順序無(wú)關(guān),但是值列表的順序必須與選定的字段順序一致insert into 表名(字段列表) values(值列表)[,(值列表)];
查看數(shù)據(jù)
查看所有數(shù)據(jù):select * from 表名[where 條件];
查看指定字段,指定條件的數(shù)據(jù):select 字段列表 from 表名 [where
條件];
更新數(shù)據(jù)
update 表名 set 字段 = 值[where 條件];
建議都有where,否則就是更新全部
刪除數(shù)據(jù)
刪除是不可逆的,謹(jǐn)慎刪除
delete from 表名 [where 條件];
中文根據(jù)問(wèn)題
設(shè)置服務(wù)器對(duì)客戶端的字符集,可以使用快捷方式;set names字符集,例如:
set names gbk;
相當(dāng)于同時(shí)設(shè)置了
character_set_client,
character_set_results,
character_set_connection
校對(duì)集問(wèn)題
校對(duì)集:數(shù)據(jù)比較的方式,有三種格式
_bin:binary,二進(jìn)制比較,就是取出二進(jìn)制位,一位一位的比較,區(qū)分大小寫
_cs:case sensitive,大小寫敏感,區(qū)分大小寫
_ci:case insensitice,大小寫不敏感,不區(qū)分大小寫
查看數(shù)據(jù)庫(kù)所支持的校對(duì)集:show collation;
校對(duì)集應(yīng)用
使用utf8的_bin和_ci來(lái)驗(yàn)證不同校對(duì)集的效果
根據(jù)某個(gè)字段進(jìn)行排序:order by 字段名 [asc/desc]
數(shù)據(jù)類型
數(shù)據(jù)類型(列類型):對(duì)數(shù)據(jù)進(jìn)行統(tǒng)一的分類,從系統(tǒng)的角度出發(fā),為了能夠使用統(tǒng)一的方式進(jìn)行管理,更好的利用有限的空間
SQL中將數(shù)據(jù)類型分為三類
數(shù)值類型
字符串類型
時(shí)間日期類型
數(shù)值型
整數(shù)型:存放整型數(shù)據(jù)
tinyint:迷你整型,使用1個(gè)字節(jié)存儲(chǔ),表示的狀態(tài)最多為256種
smallint:小整型,使用2個(gè)字節(jié)存儲(chǔ),表示的狀態(tài)最多為65536種
mediumint:中整型,使用3個(gè)字節(jié)存儲(chǔ)
int:標(biāo)準(zhǔn)整型,使用4個(gè)字節(jié)存儲(chǔ)
bigint:大整型,使用8個(gè)字節(jié)存儲(chǔ)
類型 字節(jié) 有符號(hào) 無(wú)符號(hào)
TINYINT 1 -128~127 0~255
SMALLINT 2 -2^15~2^15-1 0~2^16-1
MEDIUMINT 3 -2^23~2^23-1 0~2^24-1
INT/INTEGE 4 -2^31~2^31-1 0~2^32-1
BIGINT 8 -2^63~2^63-1 0~2^64-1
字符串類型
字符串類型分為:char,varchar,text,blob,enum,set
char(定長(zhǎng)字符串):磁盤(二維表)在定義結(jié)構(gòu)的時(shí)候,就已經(jīng)確定了最終數(shù)據(jù)的存儲(chǔ)長(zhǎng)度
char(L):L代表Length,可以存儲(chǔ)的長(zhǎng)度,單位為字符,最大長(zhǎng)度值可以為255
varchar(變長(zhǎng)字符串):在分配空間的時(shí)候,按照最大空間分配,但是實(shí)際上最終用了多少,是根據(jù)具體的數(shù)據(jù)來(lái)確定
varchar(L):L表示字符長(zhǎng)度,理論長(zhǎng)度是65536個(gè)字符,但是會(huì)多出1到2個(gè)字節(jié),來(lái)確定存儲(chǔ)的實(shí)際長(zhǎng)度
定長(zhǎng)與變長(zhǎng)的存儲(chǔ)實(shí)際空間(UTF8)
實(shí)際存儲(chǔ)數(shù)據(jù) char(4) varchar(4) char占用字節(jié)
ABCD ABCD ABCD 4*3=12
A A A 4*3=12
ABCDE —— —— 數(shù)據(jù)超過(guò)長(zhǎng)度
文本字符串:如果數(shù)據(jù)量非常大,通常超過(guò)255個(gè)字符就會(huì)使用文本字符串
文本字符串根據(jù)存儲(chǔ)的數(shù)據(jù)的格式分為:
text:存儲(chǔ)文字,存儲(chǔ)二進(jìn)制數(shù)據(jù)數(shù)據(jù)的文件路徑
blob:存儲(chǔ)二進(jìn)制數(shù)據(jù)(通常不用)
枚舉字符串(enum):事先把所有可能出現(xiàn)的結(jié)果都設(shè)計(jì)好,實(shí)際上存儲(chǔ)的數(shù)據(jù)必須是規(guī)定好的數(shù)據(jù)中的一個(gè)
枚舉的使用方式:(效率低)
enum(可能出現(xiàn)的元素列表);
例如:enum('男','女','不男不女','妖怪','保密');
使用:存儲(chǔ)數(shù)據(jù),只能存儲(chǔ)上面定義好的數(shù)據(jù)
類型 最大長(zhǎng)度 備注
enum 用1到2個(gè)字節(jié)存儲(chǔ)枚舉選項(xiàng)量(65535) 內(nèi)部存儲(chǔ)是整型表示,字段值只能是某一個(gè)
集合字符串:跟枚舉類似,實(shí)際存儲(chǔ)的是數(shù)值,而不是字符串,但集合是多選
集合使用方式:
set(元素列表)
可以使用元素列表中的多個(gè)元素,使用逗號(hào)分隔
類型 最大長(zhǎng)度
set 1,2,3,4,8元素?cái)?shù)量:64
-- 查看數(shù)據(jù)庫(kù)文件保存路徑
show variables like 'datadir';
-- 修改數(shù)據(jù)庫(kù)informationtest的字符集
alter database informationtest charset GBK;
-- 刪除數(shù)據(jù)庫(kù)
drop database informationtest;
-- 創(chuàng)建表
-- 顯示地將student表放在mydatabase數(shù)據(jù)庫(kù)下
create table if not exists mydatabase.student(
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;
-- 進(jìn)入數(shù)據(jù)庫(kù)
use mydatabase;
-- 創(chuàng)建表
create table class(
name varchar(10),
room varchar(10)
)charset utf8;
-- 查看所有表
show tables;
-- 查看以s結(jié)尾的表(這種方式少用,會(huì)降低索引效率)
show tables like '%s';
-- 查看創(chuàng)建表的語(yǔ)句
show create table student;
show create table student\g
show create table student\G -- 將查到的結(jié)果旋轉(zhuǎn)90度變成縱向
-- 查看表結(jié)構(gòu)
desc class;
describe class;
show columns from class;
-- 重命名表:student表 ->my_student
rename table student to my_student;
-- 修改表選項(xiàng):字符集
alter table my_student charset = GBK;
-- 查看表的字段
desc my_student;
-- 給學(xué)生增加ID,放到第一個(gè)位置
alter table my_student add column id int first;
-- 將學(xué)生表中number學(xué)號(hào)字段變成固定長(zhǎng)度,且放到第二位(id之后)
alter table my_student modify number char(10) after id;
-- 修改學(xué)生表中的gender字段為sex
alter table my_student change gender sex varchar(10);
-- 刪除學(xué)生表中的age年齡字段
alter table my_student drop age;
-- 刪除數(shù)據(jù)表
drop table class;
-- 插入數(shù)據(jù)
insert into my_student values(1,'bc20190001','Jim','male'),(2,'bc20190002','Lily','female');
-- 插入數(shù)據(jù):指定字段列表
insert into my_student (number,sex,name,id) values('bc20190003','male','Tom',3),('bc20190004','female','lucy',4);
-- 查看所有數(shù)據(jù)庫(kù)
select * from my_student;
-- 查看指定字段,指定條件的數(shù)據(jù):
select id,number,sex,name from my_student where id=1;
--查看滿足id為1的學(xué)生
-- 更新數(shù)據(jù)
uptate my_student set sex='female' where name='Jim';
-- 刪除數(shù)據(jù)
delete from my_student where sex='male';
-- 插入數(shù)據(jù)(帶中文)
insert into my_student values(5,'bc20190005','蔡徐坤','男');
-- 查看所有字符集
show character set;
--查看服務(wù)器默認(rèn)的對(duì)外處理的字符集
show variables like 'character_set%';
-- 修改服務(wù)器認(rèn)為的客戶端數(shù)據(jù)的字符集為utf8
set character_set_client=utf8;
-- 修改服務(wù)器給定數(shù)據(jù)的字符集為utf8
set character_set_results=utf8;
-- 快捷設(shè)置字符集
set names gbk;
-- 創(chuàng)建整型表
create table my_int(
int_1 tinyint,
int_2 smallint,
int_3 int,
int_4 bigint
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_int values(100,100,100,100);--有效數(shù)據(jù)
insert into my_int values('a','b','199','f');-- 無(wú)效數(shù)據(jù):類型限定
insert into my_int values(255,10000,100000,1000000);-- 錯(cuò)誤:超出范圍
-- 給表增加一個(gè)無(wú)符號(hào)類型
alter table my_int add int_5 tinyint unsigned;-- 無(wú)符號(hào)類型
-- 插入數(shù)據(jù)
insert into my_int values(127,1000,10000,100000,255);
-- 指定顯示寬度為1
alter table my_int add int_6 tinyint(1) unsigned;
-- 插入數(shù)據(jù)
insert into my_int values(127,10000,10000,1000000,255);
-- 顯示寬度為2,0填充
alter table my_int add int_7 tinyint(2) zerofill;
-- 插入數(shù)據(jù)
insert into my_int values(1,1,1,1,1,1,1,1);
insert into my_int values(100,100,100,100,100,100,100,100);
-- 浮點(diǎn)數(shù)表
create table my_float(
f1,float,
f1 float(10,2),-- 10位在精度范圍之外
f3 float(6,2) -- 6位在精度范圍之內(nèi)
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_float values(1000.10,1000.10,1000.10);
insert into my_float values(1234567890,12345678.90,1234.56);
insert into my_float values(3e38,3.01e7,1234.56);
insert into my_float values(99999999999,9999999999.99,9999.99);
-- 超出長(zhǎng)度插入數(shù)據(jù)
insert into my_float values(123456,1234.12345678,123.987654321);-- 小數(shù)部分可以超出長(zhǎng)度
insert into my_flaot values(123456,1234.12,12345.56);-- 最后一個(gè)整數(shù)部分超出
-- 創(chuàng)建定點(diǎn)數(shù)表
create table my_decimal(
f1 float(10,2),
d1 decimal(10,2)
)charset utf8;
-- 插入數(shù)據(jù)
insert into my_decimal values(12345678.90,12345678.90); -- 有效數(shù)據(jù)
insert into my_decimal values(1234.123456,1234.123456);
-- 查看警告
show warnings;
-- 插入數(shù)據(jù)
insert into my_decimal values(99999999.99,99999999.99);-- 沒(méi)有問(wèn)題
insert into my_decimal values(99999999.99,99999999.999)
-- 創(chuàng)建時(shí)間日期表
create table my_date(
d1 datetime,
d2 date,
d3 time,
d4 timetamp,
d5 year
)
-- 插入數(shù)據(jù)
insert into my_date values(
'2019-07-09 15:19:36','2019-07-09','15:19:36','2019-07-09 15:19:36',2019
);
--時(shí)間使用負(fù)數(shù)
insert into my_date values(
'2019-07-09 15:19:36','2019-07-09','-15:19:36','2019-07-09 15:19:36',2019
);
insert into my_date values(
'2019-07-09 15:19:36','2019-07-09','-215:19:36','2019-07-09 15:19:36',2019
);
insert into my_date values(
'2019-07-09 15:19:36','2019-07-09','-2 15:19:36','2019-07-09 15:19:36',2019
);-- -2表示過(guò)去2天,就是48小時(shí)
-- year可以使用2位或4位
insert into my_date values(
'2019-07-09 15:19:36','2019-07-09','-215:19:36','2019-07-09 15:19:36',69
);
insert into my_date values(
'2019-07-09 15:19:36','2019-07-09','-215:19:36','2019-07-09 15:19:36',70
);
-- timestamp:修改記錄
update my_date set d1='2019-07-09 20:13:15' where d5=2069;
-- 創(chuàng)建枚舉表
create table my_enum(
gender enum('男','女','保密')
);
-- 插入數(shù)據(jù)
insert into my_enum values('男'),('保密');--有效數(shù)據(jù)
insert into my_enum values('male');