一、存儲引擎:是用於根據不一樣的機制處理不一樣的數據python
二、查看MySQL中全部的引擎sql
show engines;數據庫
三、存儲引擎幾種常見類型安全
插入數據驗證引擎的做用:服務器
使用不一樣引擎:session
use db1;3d
create table t1(id int)engine=innodb;blog
create table t2(id int)engine=myisam;排序
create table t3(id int)engine=blackhole;索引
create table t4(id int)engine=memory;
插入數據驗證:
insert into t1 values(111);
insert into t2 values(222);
insert into t3 values(333);
insert into t4 values(444);
關閉服務器重啓後
約束條件:無關緊要
寬度:限制某些數據類型的存儲大小
create table 表名(字段名1 字段類型(寬度) 約束條件,字段名2 字段類型(寬度) 約束條件);
初始約束條件:not null # 約束插入記錄時不能爲空
例:create table t5(id int not null,name varchar(16));
insert into t5 values(null,'張全蛋');
》》》ERROR 1048 (23000): Column 'id' cannot be null
insert into t5 values(1,'張全蛋');
注意:
一、建立表字段名不能重複
create table test(id int,id int);
二、建立最後一個字段不能在末尾加 ,號
create table test(id int,name varchar(18),);
三、字段名必需要有字段類型與寬度,整型默認寬度是11,char默認寬度是1
一、肯定表結構
二、字段與字段類型
一、整型:
tinyint:默認範圍(-128,127)
create table t5(id tinyint);
insert into t5 values(-128),(127);
插入id爲空嚴格模式下會報錯,非嚴格模式下不會報錯可是沒法插入數據,5.6.4版本爲非嚴格模式,插入會成功插入可是會自動將值限定在範圍內
insert into t5 values(-129);
insert into t5 values(128);
int:默認範圍是(-2147483648, 2147483647)
create table t6(id int);
insert into t6 values(-2147483648),( 2147483647);
插入id爲空嚴格模式下會報錯,非嚴格模式下不會報錯可是沒法插入數據,5.6.4版本爲非嚴格模式,插入會成功插入可是會自動將值限定在範圍內
insert into t6 values(-2147483649);
int默認寬度是11,可設置寬度,若插入記錄數據超過設置寬度,則正常顯示,插入不足則會以空格補全
二、浮點型
一、float(寬度最大255)
二、double(寬度最大255)
三、decimal (寬度最大65)
字段特色(255,30)前一個數255表明最大的長度(包括小數),30表明是小數的位數
這三種浮點型區別:精確度逐漸增高
create table t8(sal float(255,30));
create table t9(sal double(255,30));
create table t10(sal decimal(65,30));
insert into t8 values(1.111111111111111111111111111111);
insert into t9 values(1.1111111111111111111111111111);
insert into t10 values(1.1111111111111111111111111111);
三、字符類型
char(16);指定定長字符16
create table t11(name char(16));
insert into t11 values('沈'); # 沈+15個空格
優勢:存取速度快
缺點:浪費空間
varchar(16): 不定長字符
存幾個符,就是幾個字符的大小,每一個字符前都要有+1bytes
優勢:節省空間
create table t12(name varchar(4));
insert into t12 values('a') # 1bytes+a
四、日期類型
date:2019-12-11
datetime: 2019-12-10 11:11:11
time:11:11:11
year:2019
timestamp:時間戳,用null自動記錄當前時間,每次對那一條記錄操做則會改變
create table student(id int,name varchar(4),birth date,register datetime,work_time year,t_time time,update_time timestamp);
insert into student values(1,'張全蛋','2000-12-11','2010-11-11 11:11:11','2019','11:11:12',null);
注意:python插入時間數據時,轉成str類型
五、枚舉與集合
enum:能夠 多選一
create table t13(id int,name varchar(4),gender enum('male','female'));
insert into t13 values(1,'shen','male');
嚴格模式下,選擇枚舉之外的數據會報錯
set:能夠 多選一 或者多選多
create table t14(id int,name varchar(6),gender enum('male','female'),hobbies set('read','study','play'));
多選一
insert into t14 values(1,'tate','male','play');
多選多,順序可不一致,按照建立的順序自動排序
insert into t14 values(2,'shen','male','study,play,read');
嚴格模式下,選擇集合之外的數據會報錯
一、not null:設置不能爲空
create table u1(id int not null);
insert into u1(id) values(1);
insert into u1(id) values(null); >>> ERROR 1048 (23000): Column 'id' cannot be null
二、unique:設置爲惟一值(若是設置爲null,能夠重)
create table u2(id int unique,name varchar(4));
insert into u2 values(1,'shen');
insert into u2 values(1,'tate'); >>> ERROR 1062 (23000): Duplicate entry '1' for key 'id'
insert into u2 values(null,'shen');
insert into u2 values(null,'tate');
三、primary key ==》not null + unique :主鍵
pk就是表中的索引:能夠經過索引快速查找某些數據,提升查詢效率
將id設爲主鍵,非空且惟一
create table u3(id int primary key,name varchar(4));
insert into u3 values(1,'tank');
insert into u3 values(2,'sean');
四、auto_increment:自增
須要與key連用,將指定的字符段會自動排序,不寫指定的字符段會默認從1開始,指定一個值後面不寫會自增
與unique連用,將指定的字符段會自動排序,不寫指定的字符段會默認從1開始,指定一個值後面不寫會自增
與primary key連用,不寫指定的字符段會默認從1開始,也能夠指定一個值後面不寫會自增
create table u4(id int unique auto_increment,name varchar(4));
insert into u4(name) values('a');
insert into u4(name) values('c');
create table u5(id int primary key auto_increment,name varchar(4));
insert into u5(name) values('a');
insert into u5 values(3,'b');
insert into u5(name) values('c');
刪除記錄
delete from 表名;# 清空表中全部的記錄,下次添加數據id默認從清空前開始
delete from u5;
insert into u5(name) values('d');
truncate table 表名;# 清空全部的記錄,而且id重置爲0
truncate table u4;
insert into u4(name) values('f');
五、unsigned:無符號
create table u6(id int unsigned);
insert into u6 values(-10); >>>ERROR 1264 (22003): Out of range value for column 'id' at row 1
insert into u6 values(0);
六、zerofill:使用0填充空格
create table u7(id int zerofill);
insert into u7 values(123);
七、default:約束默認值
create table u8(pwd varchar(20) default '000000');
設置嚴格模式(管理員模式進入客戶端修改)
一、查看數據庫配置中變量名包含mode的配置參數:
show variables like "%mode%";
二、修改安全模式:
set session; # 局部有效,只在你當前操做的窗口有效
set global session; # 全局有效,永久有效
三、修改完以後退出當前客戶端從新登陸便可
set global sql_mode="strict_trans_tables,only_full_group_by";