新增數(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)的順序與字段的順序無關(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條件];
設(shè)置服務(wù)器對(duì)客戶端的字符集,可以使用快捷方式:set names 字符集,例如:
set names gbk;
相當(dāng)于同時(shí)設(shè)置了character_set_client、character_set_results、character_set_connection
數(shù)據(jù)類型(列類型):對(duì)數(shù)據(jù)進(jìn)行統(tǒng)一的分類,從系統(tǒng)的角度出發(fā),為了能夠使用統(tǒng)一的方式進(jìn)行管理,更好地利用有限的空間
SQL中將數(shù)據(jù)類型分成三類
數(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ǔ)
小數(shù)型:帶有小數(shù)點(diǎn)或者范圍超出整型的數(shù)值類型
浮點(diǎn)型:小數(shù)點(diǎn)浮動(dòng),精度有限,而且會(huì)丟失精度
定點(diǎn)型:小數(shù)點(diǎn)固定,精度固定,不會(huì)丟失精度
datetime:時(shí)間日期,格式是YYYY-mm-dd HH:ii:ss,表示的范圍是從1000到9999年,有0值(0000-00-00 00:00:00)
date:日期,就是datetime中的date部分
time:時(shí)間(段),指定的某個(gè)區(qū)間之間,比如-時(shí)間到+時(shí)間,或者過去的某個(gè)時(shí)間到將來的某個(gè)時(shí)間
timestamp:時(shí)間戳,但并不是時(shí)間戳,只是從1970年開始的,YYYY-mm-dd HH:ii:ss格式與datetime完全一致
year:年份,有兩種形式:year(2)和year(4)
字符串類型分為:char、varchar、text、blob、enum、set
char(定長字符串):磁盤(二維表)在定義結(jié)構(gòu)的時(shí)候,就已經(jīng)確定了最終數(shù)據(jù)的存儲(chǔ)長度
char(L):L代表Length,可以存儲(chǔ)的長度,單位為字符,最大長度值可以為255
varchar(變長字符串):在分配空間的時(shí)候,按照最大的空間分配,但是實(shí)際上最終用了多少,是根據(jù)具體的數(shù)據(jù)來確定
varchar(L):L表示字符長度,理論長度是65536個(gè)字符,但是會(huì)多出1到2個(gè)字節(jié),來確定存儲(chǔ)的實(shí)際長度
文本字符串:如果數(shù)據(jù)量非常大,通常超過255個(gè)字符就會(huì)使用文本字符串
文本字符串根據(jù)存儲(chǔ)的數(shù)據(jù)的格式分為:
text:存儲(chǔ)文字、存儲(chǔ)二進(jìn)制數(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ù)
代碼例子
--創(chuàng)建整形表
create table my_int(
int_1 tinyint,
int_2 smallint,
int_3 int,
int_4 bigint
)charest utf8;
--插入數(shù)據(jù)
insert into my_int values(100,100,100,100);--有效數(shù)據(jù)
insert into my_int values("a","a","a","a");--無效數(shù)據(jù),類型限定
insert into my_int values(255,255,255,255);--錯(cuò)誤,超出范圍
-- 給表增加一個(gè)無符號(hào)類型
alter table my_int add int_5 tinyint unsigned;-- 無符號(hào)類型
--插入數(shù)據(jù)
insert into my_int values(127,10000,10000,255);--有效數(shù)據(jù)
--指定顯示寬度為127
alter table my_int add int_6 tinyint(1) unsigned;-- 無符號(hào)類型
--顯示寬度為2 0填充
alter table my_int add int_7 tinyint(2) zerofill;-- zerofill自動(dòng)變?yōu)闊o符號(hào)
--0填充 保證數(shù)據(jù)的樣式
--浮點(diǎn)數(shù)表
create table my_float(
f1 float,
f1 float(10,2),--10位在精度范圍之外
f1 float(6,2)--6位在精度范圍之內(nèi)
)charest utf8;
--插入數(shù)據(jù)
insert into my_float values(1000.10,10000.10,10000.10);
insert into my_float values(1234567890,12345678.90,1234.56);
insert into my_float values(3e38,3.0le7,1234.56);
insert into my_float values(999999999,9999999.99,9999.99);
--超出長度插入的數(shù)據(jù)
insert into my_float values(1000.11234567,10000.1234567,10000.1234567);--小數(shù)部分可以超出長度
insert into my_float 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)
)charest utf8;
--插入數(shù)據(jù)
insert into my_decimal values(12345678.90 , 12345678.90);--有效數(shù)據(jù)
insert into my_decimal values(1234.123456, 1234,1234567);--有效數(shù)據(jù)
--浮點(diǎn)數(shù)插入數(shù)據(jù)導(dǎo)致長度溢出沒有問題,但是定點(diǎn)數(shù)不可以,
--數(shù)據(jù)不精確使用浮點(diǎn)數(shù),數(shù)據(jù)精確用定點(diǎn)數(shù)
--查看警告
show warnings;
--創(chuàng)建一個(gè)時(shí)間日期表格
create table my_date(
d1 datetime,
d2 date,
d3 time,
d4 timestamp,
d5 year
)charest utf8;
--插入數(shù)據(jù)
insert into my_date values("2019-07-09 15:19:55" , "2019-07-09" , "15:19:55" , "2019-07-09 15:19:55" , 2019);
insert into my_date values("2019-07-09 15:19:55" , "2019-07-09" , "-2 15:19:55" , "2019-07-09 15:19:55" , 2019);--這里的-2 也就是過去2天 48小時(shí)
--year可以是2位或者4位
insert into my_date values('2019-07-09 15:19:55' ,'2019-07-09','-2 15:19:55',"2019-07-09 15:19:55" ,68);
--timestamp;修改記錄
update my_date set d1 = "2019-07-09 20:12
:14" where d5 =2068
--創(chuàng)建枚舉表
create table my_enum (
gender enum (('男','女','不男不女'))
--插入數(shù)據(jù)
insert into my_enum values(
'男')('保密');--有效數(shù)據(jù)
insert into my_enum values(
"male");--無效數(shù)據(jù)
--將字段結(jié)果取出進(jìn)行+0的計(jì)算
select gender + 0, gender from my_enum;
--數(shù)值插入枚舉元素
insert into my_enum values(1) , (2);