鏁版嵁绾︽潫

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鏁版嵁绾︽潫相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/table' title='table'>table   set   鏁版嵁鍐椾綑   浣滅敤   缂虹渷   鐙珛   闂   varchar   濉厖   

1 鏁版嵁绾︽潫
1.1浠€涔堟暟鎹害鏉?br /> 瀵圭敤鎴锋搷浣滆〃鐨勬暟鎹繘琛岀害鏉?br />1.2 榛樿鍊?缂虹渷绾︽潫)
浣滅敤锛?褰撶敤鎴峰浣跨敤榛樿鍊肩殑瀛楁涓嶆彃鍏ュ€肩殑鏃跺€欙紝灏变娇鐢ㄩ粯璁ゅ€笺€?br />娉ㄦ剰锛?   
  1锛夊榛樿鍊煎瓧娈垫彃鍏ull鏄彲浠ョ殑銆?br />  2锛夊榛樿鍊煎瓧娈靛彲浠ユ彃鍏ラ潪null
-- 1.1 榛樿鍊?br />CREATE TABLE student(
 id INT,
 NAME VARCHAR(20),
 address VARCHAR(20) DEFAULT 鈥樺北涓滄穭鍗氣€?nbsp; -- 榛樿鍊?br />)
DROP TABLE student;
-- 褰撳瓧娈垫病鏈夋彃鍏ュ€肩殑鏃跺€欙紝mysql鑷姩缁欒瀛楁鍒嗛厤榛樿鍊?br />INSERT INTO student(id,NAME) VALUES(1,鈥樺紶涓夆€?;
-- 娉ㄦ剰锛氶粯璁ゅ€肩殑瀛楁鍏佽涓簄ull
INSERT INTO student(id,NAME,address) VALUE(2,鈥樻潕鍥涒€?NULL);
INSERT INTO student(id,NAME,address) VALUE(3,鈥樼帇浜斺€?鈥樺北涓滄穭鍗氣€?;
1.3 闈炵┖ 
浣滅敤锛?闄愬埗瀛楁蹇呴』璧嬪€?br />娉ㄦ剰锛?br /> 1锛夐潪绌哄瓧绗﹀繀椤昏祴鍊?br /> 2锛夐潪绌哄瓧绗︿笉鑳借祴null
-- 1.2 闈炵┖
-- 闇€姹傦細 gender瀛楁蹇呴』鏈夊€硷紙涓嶄负null锛?br />CREATE TABLE student(
 id INT,
 NAME VARCHAR(20),
 gender VARCHAR(2) NOT NULL -- 闈炵┖
)
-- 闈炵┖瀛楁蹇呴』璧嬪€?br />INSERT INTO student(id,NAME) VALUES(1,鈥樻潕鍥涒€?;
-- 闈炵┖瀛楃涓嶈兘鎻掑叆null
INSERT INTO student(id,NAME,gender) VALUES(1,鈥樻潕鍥涒€?NULL);
1.4 鍞竴
浣滅敤锛?瀵瑰瓧娈电殑鍊间笉鑳介噸澶?br />娉ㄦ剰锛?br /> 1锛夊敮涓€瀛楁鍙互鎻掑叆null    
 2锛夊敮涓€瀛楁鍙互鎻掑叆澶氫釜null
  
-- 1.3 鍞竴
CREATE TABLE student(
 id INT UNIQUE, -- 鍞竴
 NAME VARCHAR(20)
)
INSERT INTO student(id,NAME) VALUES(1,鈥榸s鈥?;
INSERT INTO student(id,NAME) VALUES(1,鈥榣isi鈥?; -- ERROR 1062 (23000): Duplicate entry 鈥?鈥?for key 鈥榠d鈥?/div>
INSERT INTO student(id,NAME) VALUES(2,鈥榣isi鈥?;
1.5 涓婚敭
浣滅敤锛?闈炵┖+鍞竴
娉ㄦ剰锛?nbsp;  
 1锛夐€氬父鎯呭喌涓嬶紝姣忓紶琛ㄩ兘浼氳缃竴涓富閿瓧娈点€傜敤浜庢爣璁拌〃涓殑姣忔潯璁板綍鐨勫敮涓€鎬с€?br /> 2锛夊缓璁笉瑕侀€夋嫨琛ㄧ殑鍖呭惈涓氬姟鍚箟鐨勫瓧娈典綔涓轰富閿紝寤鸿缁欐瘡寮犺〃鐙珛璁捐涓€涓潪涓氬姟鍚箟鐨刬d瀛楁銆?br />-- 1.4 涓婚敭锛堥潪绌?鍞竴锛?br />DROP TABLE student;
CREATE TABLE student(
 id INT PRIMARY KEY, -- 涓婚敭
 NAME VARCHAR(20)
)
INSERT INTO student(id,NAME) VALUES(1,鈥樺紶涓夆€?;
INSERT INTO student(id,NAME) VALUES(2,鈥樺紶涓夆€?;
-- INSERT INTO student(id,NAME) VALUES(1,鈥樻潕鍥涒€?; -- 杩濆弽鍞竴绾︽潫锛?Duplicate entry 鈥?鈥?for key 鈥楶RIMARY鈥?/div>
-- insert into student(name) value(鈥樻潕鍥涒€?; -- 杩濆弽闈炵┖绾︽潫锛?ERROR 1048 (23000): Column 鈥榠d鈥?cannot be null
1.6 鑷闀?br />浣滅敤锛?鑷姩閫掑
  
-- 1.5 鑷闀?br />CREATE TABLE student(
 id INT(4) ZEROFILL PRIMARY KEY AUTO_INCREMENT, -- 鑷闀匡紝浠?寮€濮?nbsp; ZEROFILL 闆跺~鍏?br /> NAME VARCHAR(20)
)
-- 鑷闀垮瓧娈靛彲浠ヤ笉璧嬪€硷紝鑷姩閫掑
INSERT INTO student(NAME) VALUES(鈥樺紶涓夆€?;
INSERT INTO student(NAME) VALUES(鈥樻潕鍥涒€?;
INSERT INTO student(NAME) VALUES(鈥樼帇浜斺€?;
SELECT * FROM student;
-- 涓嶈兘褰卞搷鑷闀跨害鏉?br />DELETE FROM student;
-- 鍙互褰卞搷鑷闀跨害鏉?br />TRUNCATE TABLE student;
1.7 澶栭敭
浣滅敤锛氱害鏉熶袱绉嶈〃鐨勬暟鎹?br />  
鍑虹幇涓ょ琛ㄧ殑鎯呭喌锛?br /> 瑙e喅鏁版嵁鍐椾綑楂橀棶棰橈細 鐙珛鍑轰竴寮犺〃  
 渚嬪锛?鍛樺伐琛?nbsp; 鍜?nbsp; 閮ㄩ棬琛?br /> 闂鍑虹幇锛氬湪鎻掑叆鍛樺伐琛ㄦ暟鎹殑鏃跺€欙紝鍛樺伐琛ㄧ殑閮ㄩ棬ID瀛楁鍙互闅忎究鎻掑叆锛侊紒锛侊紒锛?nbsp;
 浣跨敤澶栭敭绾︽潫锛氱害鏉熸彃鍏ュ憳宸ヨ〃鐨勯儴闂↖D瀛楁鍊?/div>
 瑙e喅鍔炴硶锛?鍦ㄥ憳宸ヨ〃鐨勯儴闂↖D瀛楁娣诲姞涓€涓閿害鏉?br />  
-- 閮ㄩ棬琛紙涓昏〃锛?br />CREATE TABLE dept(
 id INT PRIMARY KEY,
 deptName VARCHAR(20)
)
-- 淇敼鍛樺伐琛紙鍓〃/浠庤〃锛?br />CREATE TABLE employee(
 id INT PRIMARY KEY,
 empName VARCHAR(20),
 deptId INT,-- 鎶婇儴闂ㄥ悕绉版敼涓洪儴闂↖D
 -- 澹版槑涓€涓閿害鏉?br /> CONSTRAINT emlyee_dept_fk FOREIGN KEY(deptId) REFERENCES dept(id)
 --           澶栭敭鍚嶇О                  澶栭敭               鍙傝€冭〃(鍙傝€冨瓧娈?
)
    娉ㄦ剰锛?br />   1锛夎绾︽潫鐨勮〃绉颁负鍓〃锛岀害鏉熷埆浜虹殑琛ㄧО涓轰富琛紝澶栭敭璁剧疆鍦ㄥ壇琛ㄤ笂鐨勶紒锛侊紒
   2锛変富琛ㄧ殑鍙傝€冨瓧娈甸€氱敤涓轰富閿紒
   3锛夋坊鍔犳暟鎹細 鍏堟坊鍔犱富琛紝鍐嶆坊鍔犲壇琛?br />   4锛変慨鏀规暟鎹細 鍏堜慨鏀瑰壇琛紝鍐嶄慨鏀逛富琛?br />   5锛夊垹闄ゆ暟鎹細 鍏堝垹闄ゅ壇琛紝鍐嶅垹闄や富琛?/div>
-- 1.6 澶栭敭绾︽潫
-- 鍛樺伐琛?br />CREATE TABLE employee(
 id INT PRIMARY KEY,
 empName VARCHAR(20),
 deptName VARCHAR(20) -- 閮ㄩ棬鍚嶇О
)
INSERT INTO employee VALUES(1,鈥樺紶涓夆€?鈥樿蒋浠跺紑鍙戦儴鈥?;
INSERT INTO employee VALUES(2,鈥樻潕鍥涒€?鈥樿蒋浠跺紑鍙戦儴鈥?;
INSERT INTO employee VALUES(3,鈥樼帇浜斺€?鈥樺簲鐢ㄧ淮鎶ら儴鈥?;
SELECT * FROM employee;
-- 娣诲姞鍛樺伐锛岄儴闂ㄥ悕绉扮殑鏁版嵁鍐椾綑楂?br />INSERT INTO employee VALUES(4,鈥橀檲鍏€?鈥樿蒋浠跺紑鍙戦儴鈥?;
-- 瑙e喅鏁版嵁鍐椾綑楂樼殑闂锛氱粰鍐椾綑鐨勫瓧娈垫斁鍒颁竴寮犵嫭绔嬭〃涓?br />-- 鐙珛璁捐涓€寮犻儴闂ㄨ〃
CREATE TABLE dept(
 id INT PRIMARY KEY,
 deptName VARCHAR(20)
)
DROP TABLE employee;
-- 淇敼鍛樺伐琛?br />CREATE TABLE employee(
 id INT PRIMARY KEY,
 empName VARCHAR(20),
 deptId INT,-- 鎶婇儴闂ㄥ悕绉版敼涓洪儴闂↖D
 -- 澹版槑涓€涓閿害鏉?br /> CONSTRAINT emlyee_dept_fk FOREIGN KEY(deptId) REFERENCES dept(id)
 --           澶栭敭鍚嶇О                  澶栭敭               鍙傝€冭〃(鍙傝€冨瓧娈?
)
INSERT INTO dept(id,deptName) VALUES(1,鈥樿蒋浠跺紑鍙戦儴鈥?;
INSERT INTO dept(id,deptName) VALUES(2,鈥樺簲鐢ㄧ淮鎶ら儴鈥?;
INSERT INTO dept(id,deptName) VALUES(3,鈥樼涔﹂儴鈥?;
INSERT INTO employee VALUES(1,鈥樺紶涓夆€?1);
INSERT INTO employee VALUES(2,鈥樻潕鍥涒€?1);
INSERT INTO employee VALUES(3,鈥樼帇浜斺€?2);
INSERT INTO employee VALUES(4,鈥橀檲鍏€?3);
-- 闂: 璇ヨ褰曚笟鍔′笂涓嶅悎娉曪紝鍛樺伐鎻掑叆浜嗕竴涓笉瀛樺湪鐨勯儴闂ㄦ暟鎹?br />INSERT INTO employee VALUES(5,鈥橀檲鍏€?4); -- 杩濆弽澶栭敭绾︽潫锛?Cannot add or update a child row: a foreign key constraint fails (`day16`.`employee`, CONSTRAINT `emlyee_dept_fk` FOREIGN KEY (`deptId`) REFERENCES `dept` (`id`))
-- 1锛夊綋鏈変簡澶栭敭绾︽潫锛屾坊鍔犳暟鎹殑椤哄簭锛?鍏堟坊鍔犱富琛紝鍐嶆坊鍔犲壇琛ㄦ暟鎹?br />-- 2锛夊綋鏈変簡澶栭敭绾︽潫锛屼慨鏀规暟鎹殑椤哄簭锛?鍏堜慨鏀瑰壇琛紝鍐嶄慨鏀逛富琛ㄦ暟鎹?br />-- 3锛夊綋鏈変簡澶栭敭绾︽潫锛屽垹闄ゆ暟鎹殑椤哄簭锛?鍏堝垹闄ゅ壇琛紝鍐嶅垹闄や富琛ㄦ暟鎹?br />-- 淇敼閮ㄩ棬(涓嶈兘鐩存帴淇敼涓昏〃)
UPDATE dept SET id=4 WHERE id=3;
-- 鍏堜慨鏀瑰憳宸ヨ〃
UPDATE employee SET deptId=2 WHERE id=4;
 
-- 鍒犻櫎閮ㄩ棬
DELETE FROM dept WHERE id=2;
-- 鍏堝垹闄ゅ憳宸ヨ〃
DELETE FROM employee WHERE deptId=2;
SELECT * FROM dept;
SELECT * FROM employee;

以上是关于鏁版嵁绾︽潫的主要内容,如果未能解决你的问题,请参考以下文章

绠楁硶鏁版嵁缁撴瀯01 /绠楁硶鏁版嵁缁撴瀯姒傝堪

MyCat瀹炵幇鏁版嵁搴撲笌鏁版嵁搴撲箣闂寸殑璇诲啓鍒嗙

灏哾ataGridView鏁版嵁杞垚DataTable

鏁版嵁鍙鍖?[娼滃叆娼滃嚭]

Mysql鏁版嵁搴撳熀纭€鐭ヨ瘑

Openldap 鏁版嵁鍚屾