?
一.存儲引擎
Innodb
目前5.1之后MySQL版本默認的存儲引擎
支持事務(support transactions),行鎖(row-level locking),外鍵(foreign key)
由于上面的支持 數(shù)據(jù)更安全
建表的時候innodb會產(chǎn)生兩個文件
一個是表結構文件
一個是存儲數(shù)據(jù)文件
MyIsam
5.1版本之前的MySQL的默認存儲引擎
查詢速度較于Innodb要快
建表的時候會產(chǎn)生三個文件
一個是表結構文件
一個是索引文件
索引你先把它理解成是書的目錄,能夠幫助你更快的查詢數(shù)據(jù)
一個是存儲數(shù)據(jù)文件
memory
將數(shù)據(jù)存放于內(nèi)存中
建表的時候都僅僅只有一個表結構文件
blackhole
任何寫入的數(shù)據(jù)都會消失
建表的時候都僅僅只有一個表結構文件
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine | Support | Comment | Transactions | XA | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED | NO | Federated MySQL storage engine | NULL | NULL | NULL |
| MRG_MYISAM | YES | Collection of identical MyISAM tables | NO | NO | NO |
| MyISAM | YES | MyISAM storage engine | NO | NO | NO |
| BLACKHOLE | YES | /dev/null storage engine (anything you write to it
disappears) | NO | NO | NO |
| CSV | YES | CSV storage engine | NO | NO | NO |
| MEMORY | YES | Hash based, stored in memory, useful for temporary tables |
NO | NO | NO |
| ARCHIVE | YES | Archive storage engine | NO | NO | NO |
| InnoDB | DEFAULT | Supports transactions, row-level locking, and foreign
keys | YES | YES | YES |
| PERFORMANCE_SCHEMA | YES | Performance Schema | NO | NO | NO |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
二、創(chuàng)建表的完整語法
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件],
字段名3 類型[(寬度) 約束條件]
);
注意:
1.字段名和字段類型是必須的 中括號內(nèi)的參數(shù)都是可選參數(shù)
2.同一張表中字段名不能重復
3.最后一個字段后面不能加逗號
create table t6(
id int,
name char,
);
寬度:
使用數(shù)據(jù)庫的準則:能盡量讓它少干活就盡量少干活
對存儲數(shù)據(jù)的限制
char(1) 只能存一個字符
如果超了 mysql會自動幫你截取
1.插入的時候 mysql自動截取
2.會直接報錯(mysql嚴格模式)
alter table t5 modify name char not null;
not null該字段不能插空
create table t1(id int)engine=innodb;
create table t2(id int)engine=myisam;
create table t3(id int)engine=memory;
create table t4(id int)engine=blackhole;
insert into t1 values(1);
insert into t2 values(2);
insert into t3 values(3);
insert into t4 values(4);
select * from t1; #
select * from t2;
select * from t3; #關閉服務端數(shù)據(jù)就消失
select * from t4; #寫什么,什么就消失
##建表
mysql> create table t5(id int,name char(2),password int,play char(6));
Query OK, 0 rows affected (0.31 sec)
mysql> desc t5;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
| password | int(11) | YES | | NULL | |
| play | char(6) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
##在t5表中插入數(shù)據(jù)
mysql> insert into t5 values(4,'hang',12344213789456,'粉紅色的健康很舒服');
Query OK, 1 row affected, 3 warnings (0.07 sec)
mysql> select * from t5;
+------+------+------------+--------------------+
| id | name | password | play |
+------+------+------------+--------------------+
| 1 | zg | 2147483647 | ballll |
| 1 | ha | 2147483647 | balsdf |
| 3 | zg | 2147483647 | ballll |
| 4 | ha | 2147483647 | 粉紅色的健康 |
+------+------+------------+--------------------+
在id=4時,我們可以看到char括號內(nèi)的數(shù)字限制了字符的個數(shù),不管字母或漢字或其他字符一個就算成一個字符,在我自定表字段的時候設置name
char(2),實際名字‘hang’是4個字符,被截取我們設定的字符個數(shù);password
#字段可以插空
mysql> desc t5;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | char(2) | YES | | NULL | |
| password | int(11) | YES | | NULL | |
| play | char(6) | YES | | NULL | |
+----------+---------+------+-----+---------+-------+
mysql> insert into t5 value(5,'an',2344789456,'NULL');
Query OK, 1 row affected, 1 warning (0.05 sec)
mysql> select * from t5;
+------+------+------------+--------------------+
| id | name | password | play |
+------+------+------------+--------------------+
| 1 | zg | 2147483647 | ballll |
| 1 | ha | 2147483647 | balsdf |
| 3 | zg | 2147483647 | ballll |
| 4 | ha | 2147483647 | 粉紅色的健康 |
| 5 | an | 2147483647 | NULL |
+------+------+------------+--------------------+
#更改表中的某個字段不能為空,設置完以后,
alter table t5 modify name char not null;
not null該字段不能插空如果再插入空,該字段會被插空的地方會被清空。
mysql> alter table t5 modify play char not null;
Query OK, 0 rows affected (0.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> update t5 set play=null where id=1;
Query OK, 2 rows affected, 2 warnings (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 2
mysql> select * from t5;
+------+------+------------+------+
| id | name | password | play |
+------+------+------------+------+
| 1 | z | 2147483647 | |
| 1 | h | 2147483647 | |
| 3 | z | 2147483647 | b |
| 4 | h | 2147483647 | 粉 |
| 5 | a | 2147483647 | N |
| 6 | z | 12345 | |
+------+------+------------+------+
6 rows in set (0.00 sec)
類型和中括號內(nèi)的約束
類型約束的是數(shù)據(jù)的存儲類型
而約束是基于類型之上的額外限制
#注意:
1. 在同一張表中,字段名不能相同
2. 寬度和約束條件可選,字段名和類型是必須的
3. 最后一個字段后不能加逗號!
# 補充:
# 1.寬度指的是對存儲數(shù)據(jù)的限制
create table userinfo(name char);
insert into userinfo values('jason');
"""
1.沒有安全模式的數(shù)據(jù)庫版本,能夠存放數(shù)據(jù)但是只會存進去一個j
2.最新數(shù)據(jù)庫版本直接報錯提示無法存儲:Data too long for column 'name' at row 1
"""
# 2.約束條件初識>>> null 與 not null
#設置not null后,char默認只能只能接受1個字符,即,char(1),多余的字符會被清楚掉。
create table t1(id int,name char not null);
insert into t1 values(1,'j'); # 正常存儲
insert into t1 values(2,null); # 報錯
# 總結 類型與約束條件區(qū)別
# 類型:限制字段必須以什么樣的數(shù)據(jù)類型存儲
# 約束條件:約束條件是在類型之外添加一種額外的限制
?
熱門工具 換一換