鏁版嵁搴擄細瀛樻斁鏁版嵁鐨勪粨搴擄紱
纭洏聽 杩愯鍐呭瓨
mysql 鐜板湪琚玱racle鏀惰喘浜�
oracle聽 oracle鍏徃
sqlServer 寰蒋鐨�
涓�鑸兘鏄叧绯诲瀷鏁版嵁搴撶鐞嗚蒋浠�
mysql(鏁版嵁搴撶鐞嗚蒋浠�)--鏁版嵁搴�--琛�---琛�
sql璇█锛歴tructured query language聽 缁撴瀯鍖栨煡璇㈣瑷�
鎴戜滑瀛︾殑鏄爣鍑唖ql聽 ,sql鐨勬柟瑷�琚悇涓暟鎹簱杞欢鍗曠嫭浣跨敤锛�
1.鐧诲綍mysql鏁版嵁搴擄細
鏂瑰紡1锛歮ysql -uroot -proot
鏂瑰紡2锛�
C:\Users\Administrator>mysql -uroot -p
Enter password: ****
閫�鍑烘暟鎹簱锛歲uit;聽 鎴栬�卐xit;
2.鏌ョ湅鎵�鏈夋暟鎹簱锛歮ysql> show databases;
3.閫夋嫨瑕佷娇鐢ㄧ殑鏁版嵁搴擄細mysql> use 鏁版嵁搴撳悕;
4.鏌ョ湅鎵�鏈夎〃锛歮ysql> show tables;
5.鍒涘缓鏁版嵁搴擄細mysql> create database; 鏂版暟鎹簱鍚�;
6.鍒犻櫎鏁版嵁搴擄細mysql> drop database; 琚垹闄ょ殑鏁版嵁搴撳悕;
7.鏂板缓涓�涓〃锛歮ysql> create table student(sid int,sname varchar(10));
8.鏌ョ湅琛ㄧ粨鏋勶細mysql> desc student;
鏁版嵁绫诲瀷锛�
鏂囨湰锛�
char(10):鍥哄畾闀垮害鐨勫瓧绗︿覆锛� char(10);寮�杈�10涓瓧绗︾殑鍐呭瓨绌洪棿锛�
varchar(n):鍙彉闀垮害鐨勫瓧绗︿覆锛屾渶澶氬彲浠ュ瓨n涓瓧绗︼紝鑻ュ疄闄呭瓨鐨勪笉鍒皀涓偅涔堝氨鍙紑杈熷疄闄呭瓨鍌ㄧ殑涓暟鐨勫ぇ灏忥紱
n鐨勬渶澶у�间负255锛�
瑕佺敤寮曞彿寮曡捣鏉�
鏁板瓧锛�
int:鏁存暟锛�
float(m,n):鍗曠簿搴﹀皬鏁�,m琛ㄧず鎬诲叡鏈夊灏戜綅锛宯琛ㄧず灏忔暟鏈夊嚑浣嶏紱
double(m,n):鍙岀簿搴﹀皬鏁�,m琛ㄧず鎬诲叡鏈夊灏戜綅锛宯琛ㄧず灏忔暟鏈夊嚑浣嶏紱
decimal(m,n):姣攄ouble鏇寸簿纭紝m琛ㄧず鎬诲叡鏈夊灏戜綅锛宯琛ㄧず灏忔暟鏈夊嚑浣嶏紱
鏃ユ湡锛�
date:鏃ユ湡銆傛牸寮忥細YYYY-MM-DD聽 娉ㄩ噴锛氭敮鎸佺殑鑼冨洿鏄粠 '1000-01-01' 鍒� '9999-12-31'
鎵�鏈夌殑鏃ユ湡閮界敤寮曞彿寮曡捣鏉�
DATETIME锛�*鏃ユ湡鍜屾椂闂寸殑缁勫悎銆傛牸寮忥細YYYY-MM-DD HH:MM:SS
娉ㄩ噴锛氭敮鎸佺殑鑼冨洿鏄粠 '1000-01-01 00:00:00' 鍒� '9999-12-31 23:59:59'
鍒涘缓琛ㄦ牸锛�
mysql> create table student2(sid int ,sname varchar(10),birthday date,
-> score double(5,2));
Query OK, 0 rows affected (0.01 sec)
鎻掑叆鏁版嵁锛歮ysql> insert into student2 (sid,sname,birthday,score) values(1001,'tom','2000-01-11',99.99);
鏌ョ湅琛ㄤ腑鐨勬墍鏈夋暟鎹細mysql> select * from student2;
+------+-------+------------+-------+
| sid聽 | sname | birthday聽 | score |
+------+-------+------------+-------+
| 1001 | tom聽 | 2000-01-11 | 99.99 |
| 1001 | bob聽 | 2000-01-11 | 99.99 |
| 1001 | lucy聽 | 2000-01-11 | 99.99 |
+------+-------+------------+-------+
3 rows in set (0.00 sec)
mysql Day2
璁剧疆鍛戒护琛屾樉绀烘椂鐨勭紪鐮佹牸寮忥細
鍛戒护琛岀獥鍙f槸windows鑷甫鐨勶紝瀹冪殑缂栫爜鏄痝bk;
鎵цset names gbk;鍛婅瘔mysql鎶婃暟鎹樉绀哄埌鍛戒护琛岀殑鏃跺�欑敤gbk鏄剧ず锛�
杩欎釜璇彞鍦ㄥ懡浠よ鍏抽棴鍚庡け鏁堬紝涓嬫閲嶆柊璁剧疆锛�
涓�銆佸琛ㄧ粨鏋勭殑鎿嶄綔锛�
1.澧炲姞涓�涓瓧娈�
璇硶锛歛lter table 琛ㄥ悕聽 add聽 column 鏂板瓧娈靛悕 瀛楁绫诲瀷锛�
mysql> alter table student2 add column sex varchar(10);
2.淇敼涓�涓瓧娈�
璇硶锛� alter table聽 琛ㄥ悕 modify 鍒楀悕聽 鏂版暟鎹被鍨嬶紱
mysql> alter table student2 modify sex int;
3.鏇存敼瀛楁鍚�
璇硶锛歛lter table 琛ㄥ悕 change 鍘熷垪鍚嵚� 鏂板垪鍚嵚� 鏁版嵁绫诲瀷锛�
mysql> alter table student2 change聽 sex gender varchar(10) ;
4. 鍒犻櫎琛ㄧ殑瀛楁
璇硶锛� alter table聽 琛ㄥ悕 drop聽 鍒楀悕锛�
mysql> alter table student2 drop gender;
5.閲嶅懡鍚嶈〃
璇硶锛歛lter table聽 琛ㄥ悕 rename to 鏂拌〃鍚�;
mysql> alter table student2 rename to stu;
6.澶嶅埗琛�
6.1澶嶅埗琛ㄧ粨鏋勫強鏁版嵁
璇硶锛歝reate table 鏂拌〃 select * from聽 鏃ц〃锛�
mysql> update student set sal=120 where sal is null;
mysql> select * from student;
6.2 鍙鍒惰〃缁撴瀯
璇硶锛歝reate table 鏂拌〃 select * from聽 鏃ц〃聽 where聽 1=0锛�
mysql> create table stu3 select * from stu where 1>9;
7.鍒犻櫎琛�
璇硶锛� drop table聽 琛ㄥ悕;
mysql> drop table student;
8.绱㈠紩锛氭煡璇㈠揩锛屽鍒犳參锛涗笉鍙互鐢紱
鍒涘缓绱㈠紩锛�
璇硶锛歝reate index 绱㈠紩鍚嵚� on 琛ㄥ悕(瀛楁鍚�);
mysql> create index聽 my_index1 on stu2(sname);
鏌ョ湅绱㈠紩锛�
mysql> show index from stu2;
鍒犻櫎绱㈠紩锛�
mysql> drop index my_index1 on stu2;
浜屻�乵ysql绾︽潫锛�
1.闈炵┖绾︽潫聽 not null锛氶檺鍒舵煇涓�鍒楃殑鍊间笉鑳戒负null;
鏂瑰紡1锛氬湪寤鸿〃璇彞涓瓧娈电被鍨嬬殑鍚庨潰鍔犱笂 not null;
mysql> create table emp2(empno int ,ename varchar(10) not null );
鏂瑰紡2锛�
mysql> alter table emp2 modify empno int not null;
鍒犻櫎闈炵┖绾︽潫锛�
mysql> alter table emp2 modify empno int null;
2.榛樿鍊� default锛氱粰鏌愪竴鍒楄缃粯璁ゅ�硷紱
榛樿鎯呭喌涓嬬殑榛樿鍊间负null;
鏂瑰紡1锛�
mysql> alter table emp modify empno int default 0;
鏂瑰紡2锛氬湪寤鸿〃璇彞涓瓧娈电被鍨嬬殑鍚庨潰鍔犱笂 default 榛樿鍊�;
mysql> create table emp2(empno int ,ename varchar(10) default '寮犱笁');
mysql> alter table emp2 modify ename varchar(10) default null;
3.鍞竴绾︽潫 unique锛氶檺鍒跺垪閲岄潰鐨勫�间笉鑳介噸澶嶏紱
鏂瑰紡1锛�
mysql> alter table emp2 modify empno int聽 unique;
鏂瑰紡2锛氬湪寤鸿〃璇彞涓瓧娈电被鍨嬬殑鍚庨潰鍔犱笂 unique
mysql> create table emp3(empno int unique);
鏂瑰紡3锛氳嚜瀹氫箟鍚嶅瓧鐨�
mysql> alter table emp3 add constraint my_unique unique(empno);
鍒犻櫎鍞竴绾︽潫锛�
璇硶锛歛lter聽 table 琛ㄥ悕聽 drop index 鍞竴绾︽潫鐨勭储寮曞悕锛堥粯璁や负鎵�鍦ㄧ殑瀛楁鍚嶏級;
mysql> alter聽 table emp3聽 drop index empno;
4.涓婚敭绾︽潫 primary key锛堥潪绌�+鍞竴锛�
鏂瑰紡1锛�
mysql> alter table emp modify empno int primary key;
鏂瑰紡2锛氬湪寤鸿〃璇彞涓瓧娈电被鍨嬬殑鍚庨潰鍔犱笂 primary key锛�
mysql> create table emp10(empno int primary key,ename varchar(10));
鏂瑰紡3锛氳嚜瀹氫箟鍚嶅瓧鐨�
mysql> create table emp11(empno int ,ename char(10),constraint my_key primary key(empno));
鍒犻櫎涓婚敭绾︽潫锛�
鏂瑰紡1锛歮ysql> alter table emp11 drop primary key;
5.涓婚敭鑷闀縜uto_increment
鏂瑰紡1锛氬湪寤鸿〃璇彞涓瓧娈电被鍨嬬殑鍚庨潰鍔犱笂 primary key auto_increment;
鏂瑰紡2锛歮ysql> alter table聽 emp12 modify empno int primary key auto_increment;
鍒犻櫎鑷闀匡細 alter table emp modify empno int;
6.澶栭敭绾︽潫 foreign key 涓嶅彲婊ョ敤
鏂瑰紡1锛�
alter table 琛ㄥ悕 add constraint 鑷畾涔夊閿悕 foreign key(鏈〃瀛楁)聽 references 琚弬鑰冪殑琛ㄥ悕(琚弬鑰冪殑瀛楁);
mysql> alter table emp add constraint fk_deptno foreign key(deptno)聽 references dept(deptno);
鏂瑰紡2锛�
鍦ㄥ缓琛ㄨ鍙ユ渶鍚庝竴涓瓧娈电殑鍚庨潰 constraint 鑷畾涔夊閿悕 foreign key(鏈〃瀛楁)聽 references 琚弬鑰冪殑琛ㄥ悕(琚弬鑰冪殑瀛楁)
鍒犻櫎澶栭敭锛�
璇硶锛歛lter table emp drop foreign 澶栭敭鍚�;
mysql> alter table emp drop foreign key fk_deptno;
mysql Day2 浣滀笟锛�
1.鍒涘缓瀛︾敓琛╯tudent锛氬鐢熺紪鍙穝id 涓婚敭鑷闀匡紝濮撳悕sname 闈炵┖锛屾�у埆sex聽 闈炵┖锛岀敓鏃irthday 锛屽勾榫刟ge ,鐝骇缂栧彿cid ;
mysql> create table student(sid int primary key auto_increment,sname varchar(10)
not null,sex varchar(10) not null,birthday date,age int,cid int);
Query OK, 0 rows affected (0.03 sec)
杩欐槸瀛︾敓琛╯tudent锛�
+----------+-------------+------+-----+---------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+---------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| sex聽 聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| age聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+---------+----------------+
2.鍒涘缓鐝骇琛╟lass: 鐝骇缂栧彿 cid 涓婚敭 锛岀彮绾у悕绉癱name 闈炵┖锛�
mysql> create table class(cid int primary key,cname varchar(10) not null);
Query OK, 0 rows affected (0.01 sec)
mysql> desc class;
+-------+-------------+------+-----+---------+-------+
| Field | Type聽 聽 聽 聽 | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| cid聽 | int(11)聽 聽 | NO聽 | PRI |聽 聽 聽 聽 |聽 聽 聽 |
| cname | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 |聽 聽 聽 |
+-------+-------------+------+-----+---------+-------+
3.缁欏鐢熻〃娣诲姞涓�涓瓧娈碉細浣忓潃address;
mysql> alter table student add column address varchar(10);
Query OK, 0 rows affected (0.09 sec)
Records: 0聽 Duplicates: 0聽 Warnings: 0
杩欐槸澧炲姞鐨勫瓧娈礱ddress锛�
mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+---------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| sex聽 聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| age聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
4.淇敼瀛︾敓琛ㄦ�у埆瀛楁涓篻ender;
mysql> alter table student change sex gender varchar(10);
Query OK, 0 rows affected (0.02 sec)
Records: 0聽 Duplicates: 0聽 Warnings: 0
淇敼鍚庣殑student琛細
mysql> desc student;
+----------+-------------+------+-----+---------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+---------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| gender聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| age聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+---------+----------------+
7 rows in set (0.01 sec)
5.缁檅irthday娣诲姞榛樿鍊间负2000-01-01锛�
mysql> alter table student modify birthday date default '2000-01-01';
Query OK, 0 rows affected (0.03 sec)
Records: 0聽 Duplicates: 0聽 Warnings: 0
鏀瑰彉birthday鐨勯粯璁ゅ�硷細
mysql> desc student;
+----------+-------------+------+-----+------------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default聽 聽 | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| gender聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | 2000-01-01 |聽 聽 聽 聽 聽 聽 聽 聽 |
| age聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
6.缁欐�у埆瀛楁娣诲姞榛樿鍊间负鐢凤紱
mysql> set names gbk;
Query OK, 0 rows affected (0.02 sec)
mysql> alter table student modify gender varchar(10) default '鐢�';
Query OK, 0 rows affected (0.04 sec)
Records: 0聽 Duplicates: 0聽 Warnings: 0
mysql> desc student;
+----------+-------------+------+-----+------------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default聽 聽 | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| gender聽 | varchar(10) | YES聽 |聽 聽 | 鐢仿� 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | 2000-01-01 |聽 聽 聽 聽 聽 聽 聽 聽 |
| age聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
7.鍒犻櫎age瀛楁锛�
mysql> alter table student drop age;
Query OK, 0 rows affected (0.01 sec)
mysql> desc student;
+----------+-------------+------+-----+------------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default聽 聽 | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| gender聽 | varchar(10) | YES聽 |聽 聽 | 鐢仿� 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | 2000-01-01 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
9.缁欑彮绾ц〃鎻掑叆3涓彮绾э紱
mysql> insert into class values(1001,'涓�鐝�');
Query OK, 1 row affected (0.03 sec)
mysql> insert into class values(1002,'浜岀彮');
Query OK, 1 row affected (0.00 sec)
mysql> insert into class values(1003,'涓夌彮');
Query OK, 1 row affected (0.00 sec)
mysql> select * from class;
+------+-------+
| cid聽 | cname |
+------+-------+
| 1001 | 涓�鐝� 聽 |
| 1002 | 浜岀彮聽 聽 聽 |
| 1003 | 涓夌彮聽 聽 聽 |
+------+-------+
10.鍦ㄥ鐢熻〃涓粰姣忎釜鐝骇娣诲姞3鍚嶅鐢熶俊鎭紱
mysql> insert into student values(1,'寮犱笁','鐢�','2012-01-02',1001,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(null,'鏉庡洓','濂�','2009-03-22',1002,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into student values(null,'鐜嬩簲','鐢�','2009-03-25',1003,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> select *from student;
+-----+-------+--------+------------+------+---------+
| sid | sname | gender | birthday聽 | cid聽 | address |
+-----+-------+--------+------------+------+---------+
|聽 1 | 寮犱笁聽 聽 聽 | 鐢仿� 聽 聽 | 2012-01-02 | 1001 | cq聽 聽 聽 |
|聽 2 | 鏉庡洓聽 聽 聽 | 濂陈� 聽 聽 | 2009-03-22 | 1002 | cq聽 聽 聽 |
|聽 3 | 鐜嬩簲聽 聽 聽 | 鐢仿� 聽 聽 | 2009-03-25 | 1003 | cq聽 聽 聽 |
+-----+-------+--------+------------+------+---------+
8.缁欏鐢熻〃鐨刢id涓婃坊鍔犲閿害鏉燂紝浠ュ弬鑰僣lass琛ㄤ腑鐨刢id瀛楁锛�
mysql> alter table student add constraint fk_cid foreign key(cid) references class(cid);
Query OK, 3 rows affected (0.02 sec)
Records: 3聽 Duplicates: 0聽 Warnings: 0
鍙楀埌绾︽潫鎶ラ敊锛�
mysql>聽 insert into student values(null,'鐜嬩簲','鐢�','2009-03-25',1004,'cq');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint f
ails (`c17/student`, CONSTRAINT `fk_cid` FOREIGN KEY (`cid`) REFERENCES `class`
(`cid`))
11.鍒犻櫎澶栭敭锛�
mysql> alter table student drop foreign聽 key fk_cid;
Query OK, 3 rows affected (0.02 sec)
Records: 3聽 Duplicates: 0聽 Warnings: 0
鍒犻櫎鍚庡彲浠ュ線student缁х画娣诲姞瀛︾敓淇℃伅锛�
mysql>聽 insert into student values(null,'鐜嬩簲','鐢�','2009-03-25',1004,'cq');
Query OK, 1 row affected (0.00 sec)
mysql> select *from student;
+-----+-------+--------+------------+------+---------+
| sid | sname | gender | birthday聽 | cid聽 | address |
+-----+-------+--------+------------+------+---------+
|聽 1 | 寮犱笁聽 聽 聽 | 鐢仿� 聽 聽 | 2012-01-02 | 1001 | cq聽 聽 聽 |
|聽 2 | 鏉庡洓聽 聽 聽 | 濂陈� 聽 聽 | 2009-03-22 | 1002 | cq聽 聽 聽 |
|聽 3 | 鐜嬩簲聽 聽 聽 | 鐢仿� 聽 聽 | 2009-03-25 | 1003 | cq聽 聽 聽 |
|聽 4 | 鐜嬩簲聽 聽 聽 | 鐢仿� 聽 聽 | 2009-03-25 | 1004 | cq聽 聽 聽 |
+-----+-------+--------+------------+------+---------+
4 rows in set (0.00 sec)
12.澶嶅埗student琛ㄥ埌student2锛�
mysql> create table student2 select * from student;
Query OK, 4 rows affected (0.01 sec)
Records: 4聽 Duplicates: 0聽 Warnings: 0
mysql> select *from student2;
+-----+-------+--------+------------+------+---------+
| sid | sname | gender | birthday聽 | cid聽 | address |
+-----+-------+--------+------------+------+---------+
|聽 1 | 寮犱笁聽 聽 聽 | 鐢仿� 聽 聽 | 2012-01-02 | 1001 | cq聽 聽 聽 |
|聽 2 | 鏉庡洓聽 聽 聽 | 濂陈� 聽 聽 | 2009-03-22 | 1002 | cq聽 聽 聽 |
|聽 3 | 鐜嬩簲聽 聽 聽 | 鐢仿� 聽 聽 | 2009-03-25 | 1003 | cq聽 聽 聽 |
|聽 4 | 鐜嬩簲聽 聽 聽 | 鐢仿� 聽 聽 | 2009-03-25 | 1004 | cq聽 聽 聽 |
+-----+-------+--------+------------+------+---------+
13.灏嗚〃student2鐨勫悕瀛楁敼涓簊tu;
鏀瑰墠鎵�鏈夌殑琛細
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class聽 聽 聽 聽 |
| student聽 聽 聽 |
| student2聽 聽 聽 |
+---------------+
鏀瑰悗鎵�鏈夌殑琛細
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class聽 聽 聽 聽 |
| stu聽 聽 聽 聽 聽 |
| student聽 聽 聽 |
+---------------+
14.鍒犻櫎stu琛ㄤ腑sid鐨勮嚜澧為暱锛�
mysql> desc stu;
+----------+-------------+------+-----+------------+----------------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default聽 聽 | Extra聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | NULL聽 聽 聽 | auto_increment |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| gender聽 | varchar(10) | YES聽 |聽 聽 | 鐢仿� 聽 聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | 2000-01-01 |聽 聽 聽 聽 聽 聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 聽 聽 聽 聽 聽 |
+----------+-------------+------+-----+------------+----------------+
mysql> alter table stu modify sid int;
Query OK, 3 rows affected (0.02 sec)
Records: 3聽 Duplicates: 0聽 Warnings: 0
mysql> desc stu;
+----------+-------------+------+-----+------------+-------+
| Field聽 聽 | Type聽 聽 聽 聽 | Null | Key | Default聽 聽 | Extra |
+----------+-------------+------+-----+------------+-------+
| sid聽 聽 聽 | int(11)聽 聽 | NO聽 | PRI | 0聽 聽 聽 聽 聽 |聽 聽 聽 |
| sname聽 聽 | varchar(10) | NO聽 |聽 聽 |聽 聽 聽 聽 聽 聽 |聽 聽 聽 |
| gender聽 | varchar(10) | YES聽 |聽 聽 | 鐢仿� 聽 聽 聽 聽 |聽 聽 聽 |
| birthday | date聽 聽 聽 聽 | YES聽 |聽 聽 | 2000-01-01 |聽 聽 聽 |
| cid聽 聽 聽 | int(11)聽 聽 | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 |
| address聽 | varchar(10) | YES聽 |聽 聽 | NULL聽 聽 聽 |聽 聽 聽 |
+----------+-------------+------+-----+------------+-------+
15.鍒犻櫎stu琛紱
鍒犻櫎鍓嶏細
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class聽 聽 聽 聽 |
| stu聽 聽 聽 聽 聽 |
| student聽 聽 聽 |
+---------------+
mysql> drop table stu;
Query OK, 0 rows affected (0.01 sec)
鍒犻櫎鍚庯細
mysql> show tables;
+---------------+
| Tables_in_c17 |
+---------------+
| class聽 聽 聽 聽 |
| student聽 聽 聽 |
+---------------+
2 rows in set (0.00 sec)