當前位置:首頁 > IT技術 > 數(shù)據(jù)庫 > 正文

mysql 索引操作
2021-09-23 13:54:17

索引

創(chuàng)建索引

-- 從表的角度創(chuàng)建索引
ALTER TABLE actor ADD UNIQUE INDEX  uniq_idx_firstname (first_name);
ALTER TABLE actor ADD INDEX  idx_lastname (last_name);
-- 從數(shù)據(jù)庫的角度創(chuàng)建索引
CREATE UNIQUE INDEX uniq_idx_firstname USING BTREE actor (first_name);

刪除索引

ALTER TABLE actor DROP INDEX uniq_idx_firstname;

DROP INDEX uniq_idx_firstname ON actor;

強制使用索引

select *
from actor 
force index (uniq_idx_firstname)
where first_name = 'jd'

約束

添加主鍵約束

ALTER TABLE employees_test
ADD CONSTRAINT PRIMARY KEY(id);

添加外鍵約束

ALTER TABLE audit
ADD CONSTRAINT
constraint_name
FOREIGN KEY(emp_no)
REFERENCES employees_test(id);

本文摘自 :https://www.cnblogs.com/

開通會員,享受整站包年服務立即開通 >