+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++mysql 基礎操作<br />//主鍵549830479<br /> alter table tabelname add new_field_id int(5) unsigned default 0 not null auto_increment ,add primary key (new_field_id);<br />//增加一個新列549830479<br /> alter table t2 add d timestamp;<br />alter table infos add ex tinyint not null default '0';<br />//刪除列549830479<br /> alter table t2 drop column c;<br />//重新命名列549830479<br /> alter table t1 change a b integer;<br />//改變列的類型549830479<br /> alter table t1 change b b bigint not null;<br />alter table infos change list list tinyint not null default '0';<br />//重新命名表549830479<br /> alter table t1 rename t2;<br />加索引549830479<br /> mysql> alter table tablename change depno depno int(5) not null;<br />mysql> alter table tablename add index 索引名 (欄位名1[,欄位名2 …]);<br />mysql> alter table tablename add index emp_name (name);<br />加主關鍵字的索引549830479<br />mysql> alter table tablename add primary key(id);<br />加唯一限制條件的索引549830479<br /> mysql> alter table tablename add unique emp_name2(cardnumber);<br />刪除某個索引549830479<br /> mysql>alter table tablename drop index emp_name;<br />修改表:549830479<br />增加欄位:549830479<br /> mysql> ALTER TABLE table_name ADD field_name field_type;<br />修改原欄位名稱及類型:549830479<br /> mysql> ALTER TABLE table_name CHANGE old_field_name new_field_name field_type;<br />刪除欄位:549830479<br /> mysql> ALTER TABLE table_name DROP field_name; </p><p>+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++mysql 約束<br />--------添加主鍵約束(bookid作為主鍵)<br />alter table bookmessage<br />add constraint pk_bookid primary key(bookid)<br />--------添加唯一約束<br />alter table bookmessage<br />add constraint uq_bookid UNIQUE(bookid)<br />---------添加預設約束<br />alter table bookmessage<br />add constraint df_address DEFAUIT('地址不詳') for Address<br />--------添加檢出約束,要求年齡只能在15---40之間<br />alter table readermessage<br />add constraint CK_age CHECK(age BETWEEN 15 AND 40)<br />-------添加外鍵約束<br />alter table bookmessage<br />add constraint fk_bookid<br />foreign key(bookid)<外鍵> references readermessage<表>(readerid)<表中的主鍵><br />-------刪除約束<br />alter table 表名<br />drop constraint 約束名<br />+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br />