對錶的列屬性、column進行的限制。例如:not null、unique、primary key、foreign key、check。sql
1.create table,add the attribute constraints.數據庫
create table user( id int, name varchar(40) not null, id_card varchar(20) not null unique, phone_number varchar(11) not null, age int(4), class_id int(4), constraint user_id_pk primary key(id), constraint user_phone_number_uk unique(phone_number), constraint user_class_id_fk foreign key(class_id) references `class`(id) ON DELETE CASCADE )
the " constraint " is table level constraints , the " not null " is column level constraints。性能
2.add and remove table constraints, after the new table, can only add and remove constraint, cannot be modifiedspa
2.1 add constraints設計
alter table user add constraint user_age_ck check(age < 100);
2.2 add "not null" constraintscode
alter table user modify(age not null);
2.3 disable and enable constraints對象
-- disable constraint user_age_ck alter table user disable constraint user_age_ck; -- enable constraint user_age_ck alter table user enable constraint user_age_ck;
copy table structure and data,but the destination table must not exist。索引
SELECT * INTO NEW_TABLE FROM OLD_TABLE;
注意:複製表的同時表的約定並不能複製過來。rem
若是是隻複製表結構、不復制數據,只須要在where子句中添加一個永遠不等於true的條件便可。io
SELECT * INTO NEW_TABLE FROM OLD_TABLE WHERE 1 = 0;
Copy table data to a new table
INSERT INTO NEW_TABLE SELECT * FROM OLD_TABLE;
當表結構不一樣或複製部分字段能夠設置插入相應的字段。例如:
INSERT INTO NEW_TABLE(name,age,address,phone) SELECT name,age,address,phone FROM OLD_TABLE;
索引是一個設計用來提供整個數據庫操做速度的數據庫對象,它經過建立一個內部的索引表來達到快速搜索的目的。
索引能夠下降insert、update和delete操做的性能,由於每次這些操做發生時,內部索引結構須要被更新(或者行被從新安排)。基於此緣由,太多的索引會下降數據庫的總體性能,所以要仔細調整。
當列被用於錶鏈接或者與其它表有外鍵關係,而且列出如今where或者order by子句中時,在列上設置索引,是一個通用的技巧規則。
索引能夠是惟一的和非惟一的。惟一索引不容許在索引列上出現重複值。
惟一索引一般建立在有主鍵或惟一約束的列上。
建立索引的關鍵字爲create index,在其後要指明建立的索引的名稱,在括號內列出索引表的字段(能夠爲多列)。
CREATE INDEX index_name ON table(column_1,column_2);
Create a non unique index
CREATE INDEX index_name ON table(column);
Create unique index,要添加unique關鍵字
CREATE UNIQUE INDEX index_name ON table(column);
Delete index
刪除索引的SQL命令是drop index
DROP INDEX table_name.index_name;