MySQL瀛楁绫诲瀷 绾︽潫
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL瀛楁绫诲瀷 绾︽潫相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e8%bd%af%e4%bb%b6%e7%bb%84%e4%bb%b6' title='杞欢缁勪欢'>杞欢缁勪欢 nes val 澶嶆潅 缂虹偣 澶氫釜 骞朵笖 鏃堕棿 杩涘埗
鐩綍
mysql瀛樺偍寮曟搸
浠€涔堟槸瀛樺偍寮曟搸锛?/p>
鏁版嵁搴撳瓨鍌ㄥ紩鎿庢槸鏁版嵁搴撳簳灞傝蒋浠剁粍浠讹紝鏁版嵁搴撶鐞嗙郴缁熶娇鐢ㄦ暟鎹紩鎿庤繘琛屽垱寤恒€佹煡璇€佹洿鏂板拰鍒犻櫎鏁版嵁鎿嶄綔銆?/strong>鐢ㄤ簬鏍规嵁涓嶅悓鐨勬満鍒跺鐞嗕笉鍚岀殑鏁版嵁
鎻愮ず锛?code>InnoDB 浜嬪姟鍨嬫暟鎹簱鐨勯閫夊紩鎿庯紝鏀寔浜嬪姟瀹夊叏琛紙ACID锛夛紝琛岄攣瀹?鍜?澶栭敭銆傦紙 浜嬪姟涓昏鐢ㄤ簬澶勭悊鎿嶄綔閲忓ぇ锛屽鏉傚害楂樼殑鏁版嵁锛?/p>
MySQL
5.5.5 涔嬪悗锛?code>InnoDB 浣滀负榛樿瀛樺偍寮曟搸銆?/p>
鏌ョ湅MySQL
涓墍鏈夊紩鎿?/p>
show engines
create table t1(id int)engines=innodb; #鍒涘缓寮曟搸涓篿nnodb鐨勮〃
myisam
:5.5浠ュ墠鑰佺殑鐗堟湰浣跨敤鐨勫瓨鍌ㄥ紩鎿庯紝鎷ユ湁杈冮珮鐨勬彃鍏ワ紝鏌ヨ閫熷害锛屼絾涓嶆敮鎸佷簨鐗?/p>
blackhole
:绫讳技浜庨槦鍒椾腑鐨勬暟鎹紝瀛樿繘鍘绘秷澶?/p>
memory
:閫氱數鏁版嵁瀛樺湪锛屾柇鐢典涪澶?/p>
鍒涘缓琛ㄥ畬鏁寸殑璇硶
绾︽潫鏉′欢锛氬彲鏈夊彲鏃?/p>
瀹藉害锛氶檺鍒舵煇浜涙暟鎹被鍨嬬殑瀛樺偍澶у皬
闈炵┖绾︽潫
鍦ㄥ垱寤鸿〃鏃跺彲浠ョ敤NOT NULL鍏抽敭瀛楄缃潪绌虹害鏉?/p>
<瀛楁鍚?gt;<鏁版嵁绫诲瀷> NOT NULL
瀹炰緥锛?/p>
mysql> create table t6(
id int not null
);
Query OK, 0 rows affected (0.25 sec)
mysql> desc t6;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | NO | | NULL | |
+-------+---------+------+-----+---------+-------+
瀛楁绫诲瀷
鏁村舰绫诲瀷INT TINYINT
SMALLINT MEDIUMINT BIGINT
鏁村舰绫诲瀷鍙堢О鏁板€煎瀷鏁版嵁锛屼富瑕佺敤鏉ュ瓨鍌ㄦ暟瀛椼€?/p>
绫诲瀷鍚嶇О | 璇存槑 | 瀛樺偍闇€姹?/th> |
---|---|---|
TINYINT | 寰堝皬鐨勬暣鏁?/td> | 1涓瓧鑺?/td> |
SMALLINT | 灏忕殑鏁存暟 | 2涓畤鑺?/td> |
MEDIUMINT | 涓瓑澶у皬鐨勬暣鏁?/td> | 3涓瓧鑺?/td> |
INT (INTEGHR) | 鏅€氬ぇ灏忕殑鏁存暟 | 4涓瓧鑺?/td> |
BIGINT | 澶ф暣鏁?/td> | 8涓瓧鑺?/td> |
浠庝笂琛ㄤ腑鍙互鐪嬪埌锛屼笉鍚岀被鍨嬬殑鏁存暟瀛樺偍鎵€闇€鐨勫瓧鑺傛暟涓嶇浉鍚岋紝鍗犵敤瀛楄妭鏁版渶灏忕殑鏄?TINYINT 绫诲瀷锛屽崰鐢ㄥ瓧鑺傛渶澶х殑鏄?BIGINT 绫诲瀷锛屽崰鐢ㄧ殑瀛楄妭瓒婂鐨勭被鍨嬫墍鑳借〃绀虹殑鏁板€艰寖鍥磋秺澶с€?/p>
绫诲瀷鍚嶇О | 璇存槑 | 瀛樺偍闇€姹?/th> |
---|---|---|
TINYINT | -128?127 | 0 ?255 |
SMALLINT | -32768?32767 | 0?65535 |
MEDIUMINT | -8388608?8388607 | 0?16777215 |
INT (INTEGER) | -2147483648?2147483647 | 0?4294967295 |
BIGINT | -9223372036854775808?9223372036854775807 | 0?18446744073709551615 |
瀹炰緥锛歵inyint 鑼冨洿 -128?127
create table t7(
id tinyint
);
瀹炰緥锛歩nt 鑼冨洿 -2147483648?2147483647
create table t8(
id int
);
鑻ユ彃鍏ヨ秴杩囪瀹氬搴︼紝鍒欐甯告樉绀?/p>
鑻ユ彃鍏ヤ笉瓒筹紝鍒欎互绌烘牸涓嶅叏
娴偣绫诲瀷float
DOUBLE 鍙岀簿搴︽诞鐐规暟
DECIMAL 鍘嬬缉鐨勨€滀弗鏍尖€濆畾鐐规暟
涓夌娴偣鍨嬬殑鍖哄埆鍦ㄤ簬 绮剧‘鍒颁笉涓€鏍?/p>
搴旂敤鍦烘櫙锛氬伐璧勶紝韬珮锛屼綋閲嶃€傘€傘€?/p>
瀹炰緥锛?/p>
create table t9(sal float(255,30)) #鑼冨洿255鏄渶澶ч暱搴︼紙鍖呮嫭.灏忔暟锛夛紝30浠h〃鐨勬槸灏忔暟鐨勪綅鏁?/code>
瀛楃绫诲瀷char varchar
绫诲瀷鍚嶇О | 璇存槑 | 瀛樺偍闇€姹?/th> |
---|---|---|
CHAR(M) | 鍥哄畾闀垮害闈炰簩杩涘埗瀛楃涓?/td> | M 瀛楄妭锛?<=M<=255 |
VARCHAR(M) | 鍙橀暱闈炰簩杩涘埗瀛楃涓?/td> | L+1瀛楄妭锛屽湪姝わ紝L< = M鍜?1<=M<=255 |
TINYTEXT | 闈炲父灏忕殑闈炰簩杩涘埗瀛楃涓?/td> | L+1瀛楄妭锛屽湪姝わ紝L<2^8 |
TEXT | 灏忕殑闈炰簩杩涘埗瀛楃涓?/td> | L+2瀛楄妭锛屽湪姝わ紝L<2^16 |
MEDIUMTEXT | 涓瓑澶у皬鐨勯潪浜岃繘鍒跺瓧绗︿覆 | L+3瀛楄妭锛屽湪姝わ紝L<2^24 |
LONGTEXT | 澶х殑闈炰簩杩涘埗瀛楃涓?/td> | L+4瀛楄妭锛屽湪姝わ紝L<2^32 |
ENUM | 鏋氫妇绫诲瀷锛屽彧鑳芥湁涓€涓灇涓惧瓧绗︿覆鍊?/td> | 1鎴?涓瓧鑺傦紝鍙栧喅浜庢灇涓惧€肩殑鏁扮洰 (鏈€澶у€间负65535) |
SET | 涓€涓缃紝瀛楃涓插璞″彲浠ユ湁闆朵釜鎴?澶氫釜SET鎴愬憳 | 1銆?銆?銆?鎴?涓瓧鑺傦紝鍙栧喅浜庨泦鍚?鎴愬憳鐨勬暟閲忥紙鏈€澶?4涓垚鍛橈級 |
CHAR(M) 涓哄浐瀹氶暱搴﹀瓧绗︿覆,搴旂敤鍦烘櫙锛氭墜鏈哄彿锛岃韩浠借瘉锛岄摱琛屽崱鍙枫€傘€傘€?/p>
闀垮害鑼冨洿鏄?0~255. 褰撲綘鎯宠鍌ㄥ瓨涓€涓暱搴︿笉瓒?255 鐨勫瓧绗︽椂锛孧ysql 浼氱敤绌烘牸鏉ュ~鍏呭墿涓嬬殑瀛楃
浼樼偣锛氶€熷害蹇?/p>
缂虹偣锛氭氮璐圭┖闂?/p>
VARCHAR锛堬級涓哄彲鍙樺瓧绗︾被鍨?/strong>锛屽瓨鍑犱釜瀛楃锛屽氨鏄嚑涓瓧绗︾殑澶у皬锛屾瘡涓瓧绗﹀墠閮借+1bytes锛屾渶澶у疄闄呴暱搴︾敱鏈€闀跨殑琛岀殑澶у皬鍜屼娇鐢ㄧ殑瀛楃闆嗙‘瀹氾紝鑰屽疄闄呭崰鐢ㄧ殑绌洪棿涓哄瓧绗︿覆鐨勫疄闄呴暱搴﹀姞 1銆?/p>
浼樼偣锛氳妭鐪佺┖闂?/p>
TEXT() 淇濆瓨闈炰簩杩涘埗瀛楃涓诧紝濡傛枃绔犲唴瀹广€佽瘎璁虹瓑銆傚綋淇濆瓨鎴栨煡璇?TEXT 鍒楃殑鍊兼椂锛屼笉鍒犻櫎灏鹃儴绌烘牸銆?/p>
涓?char 鍜?varchar 涓嶅悓鐨勬槸锛宼ext 涓嶅彲浠ユ湁榛樿鍊硷紝鍏舵渶澶ч暱搴︽槸 2 鐨?16 娆℃柟-1 姣忎竴涓被鍨嬮兘鏈夊悎娉曠殑鍙栧€艰寖鍥达紝褰撴寚瀹氱‘瀹氫笉鍚堟硶鐨勫€兼椂锛岀郴缁熷皢鈥滈浂鈥濆€兼彃鍏ユ暟鎹簱涓€?/p>
date:2019-12-11 datetime:2019-12-11 15:50:55 time:15:50:55 year:2019 timestamp:鏃堕棿鎴?/p>
鏃堕棿鎴冲疄渚嬶細 python 鎻掑叆鏃堕棿鏁版嵁鏃讹紝杞垚str绫诲瀷 set锛氬閫変竴 鎴?澶氶€夊 unique灏嗘煇涓瓧娈佃缃负鍞竴鐨勫€?/p>
not + null + unique 鍙樹负涓婚敭 primary key-鈥?鈥?鈥?gt;not null unique 涓婚敭灏辨槸琛ㄤ腑鐨勭储寮曪細鍙互閫氳繃绱㈠紩蹇€熸煡鎵炬煇浜涙暟鎹?/p>
涓婚敭绾︽潫鍗冲湪琛ㄤ腑瀹氫箟涓€涓富閿潵鍞竴纭畾琛ㄤ腑姣忎竴琛屾暟鎹殑鏍囪瘑绗︺€備富閿彲浠ユ槸琛ㄤ腑鐨勬煇涓€鍒楁垨鑰呭鍒楃殑缁勫悎锛屽叾涓敱澶氬垪缁勫悎鐨勪富閿О涓哄鍚堜富閿€備富閿簲璇ラ伒瀹堜笅闈㈢殑瑙勫垯锛?/p>
涓婚敭瀹炰緥锛?/p>
涓婚敭鑷瀹炰緥锛歱rimary key auto_increment 鑷榛樿浠?寮€濮?/p>
鎻掑叆鏁板瓧鏄笉鑳藉甫绗﹀彿 delete 娓呯┖璁板綍,id杩樻槸鎺ョ潃娓呴櫎鍓嶇殑鏁板瓧 truncate娓呯┖澶氭湁璁板綍锛屽苟涓攊d閲嶇疆涓? 以上是关于MySQL瀛楁绫诲瀷 绾︽潫的主要内容,如果未能解决你的问题,请参考以下文章 DOM-Text绫诲瀷銆丆omment绫诲瀷銆丆DATASection绫诲瀷銆丏ocumentType绫诲瀷銆丏ocumentFragment绫诲瀷銆丄ttr绫诲瀷鏃ユ湡绫诲瀷
绫诲瀷鍚嶇О
鏃ユ湡鏍煎紡
鏃ユ湡鑼冨洿
瀛樺偍闇€姹?/th>
YEAR
YYYY
1901 ~ 2155
1 涓瓧鑺?/td>
TIME
HH:MM:SS
-838:59:59 ~ 838:59:59
3 涓瓧鑺?/td>
DATE
YYYY-MM-DD
1000-01-01 ~ 9999-12-3
3 涓瓧鑺?/td>
DATETIME
YYYY-MM-DD HH:MM:SS
1000-01-01 00:00:00 ~ 9999-12-31 23:59:59
8 涓瓧鑺?/td>
TIMESTAMP
YYYY-MM-DD HH:MM:SS
1980-01-01 00:00:01 UTC ~ 2040-01-19 03:14:07 UTC
4 涓瓧鑺?/td>
mysql> insert into t11 values(1,null);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t11;
+------+---------------------+
| id | create_time |
+------+---------------------+
| 1 | 2019-12-11 15:53:24 |
+------+---------------------+
create table student(
id int,
name varchar(4),
birth date,
register datetime,
work_time year,
t_time time,
update_time timestamp
);
鏋氫妇闆嗗悎
enum
:澶氶€変竴create table t12(
id int,
name varchar(10),
gender enum('male','female')
);
mysql> insert into t12 values(1,'letin','male');
Query OK, 1 row affected (0.05 sec)
create table t13(
id int,
name varchar(10),
hobbies set('swimming','game','study')
);
mysql> insert into t13 values(1,'letin','swimming,game,study');
Query OK, 1 row affected (0.06 sec)
mysql> select * from t13;
+------+-------+---------------------+
| id | name | hobbies |
+------+-------+---------------------+
| 1 | letin | swimming,game,read |
+------+-------+---------------------+
1 row in set (0.00 sec)
绾︽潫鏉′欢
create table t14(
id int not null unique,
name varchar(10)
);
mysql> insert into t14 values(1,'letin');
Query OK, 1 row affected (0.05 sec)
mysql> insert into t14 values(1,'leiting');
ERROR 1062 (23000): Duplicate entry '1' for key 'id' #id璁剧疆浜嗗敮涓€锛岄噸澶嶅悗浼氭姤閿?/code>
涓婚敭 鑷
mysql> create table t15(
-> id int primary key,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.19 sec)
mysql> create table t16(
-> id int primary key auto_increment,
-> name varchar(10)
-> );
Query OK, 0 rows affected (0.20 sec)
mysql> desc t16;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> insert into t16(name) values('letin');
Query OK, 1 row affected (0.04 sec)
mysql> create table t17(
-> id int unsigned
-> );
Query OK, 0 rows affected (0.19 sec)
zerofill浣跨敤0濉厖
mysql> create table t18(
-> id int zerofill
-> );
Query OK, 0 rows affected (0.22 sec)
mysql> insert into t18 values(100);
Query OK, 1 row affected (0.04 sec)
mysql> select * from t18;
+------------+
| id |
+------------+
| 0000000100 |
+------------+
1 row in set (0.00 sec)
鍒犻櫎璁板綍
mysql> delete from t18;
Query OK, 1 row affected (0.04 sec)
mysql> select * from t18;
Empty set (0.00 sec)
mysql> desc t18;
+-------+---------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------------------------+------+-----+---------+-------+
| id | int(10) unsigned zerofill | YES | | NULL | |
+-------+---------------------------+------+-----+---------+-------+
1 row in set (0.01 sec)
truncate table t20;