==========================================================
----查找索引---- use database_name EXEC Sp_helpindex table_name use TeamaxShop EXEC Sp_helpindex UserBankCard ----修改索引---- use database_name exec sp_rename ‘table_name.old_name’‘new_name’ ----删除索引---- drop Index table_name.index_name,.....n ----注:drop index语句不能删除通过PRINARY KEY和UNIQUE约束创建的索引, ----若要删除该约束相应的索引,请使用带有DROP CONSTRAINT子句的ALTER TABLE.
=========================================================
----在执行CREATE TABLE语句时可以创建索引,也可以单独用CREATE INDEX或ALTER TABLE来为表增加索引。 ----1.ALTER TABLE ALTER TABLE用来创建普通索引、UNIQUE索引或PRIMARY KEY索引。 ALTER TABLE table_name ADD INDEX index_name (column_list) ALTER TABLE table_name ADD UNIQUE (column_list) ALTER TABLE table_name ADD PRIMARY KEY (column_list) ----2.CREATE INDEX CREATE INDEX可对表增加普通索引或UNIQUE索引。 CREATE INDEX index_name ON table_name (column_list) CREATE UNIQUE INDEX index_name ON table_name (column_list) ----table_name、index_name和column_list具有与ALTER TABLE语句中相同的含义,索引名不可选。 ----另外,不能用CREATE INDEX语句创建PRIMARY KEY索引。 ----3.索引类型 ----在创建索引时,可以规定索引能否包含重复值。如果不包含,则索引应该创建为PRIMARY KEY或UNIQUE索引。 ----对于单列惟一性索引,这保证单列不包含重复的值。对于多列惟一性索引,保证多个值的组合不重复。 ----PRIMARY KEY索引和UNIQUE索引非常类似。事实上,PRIMARY KEY索引仅是一个具有名称PRIMARY的UNIQUE索引。 ----这表示一个表只能包含一个PRIMARY KEY,因为一个表中不可能具有两个同名的索引。 ----下面的SQL语句对students表在sid上添加PRIMARY KEY索引。 ALTER TABLE students ADD PRIMARY KEY (sid) ----4.删除索引 ----可利用ALTER TABLE或DROP INDEX语句来删除索引。 ----类似于CREATE INDEX语句,DROP INDEX可以在ALTER TABLE内部作为一条语句处理,语法如下。 DROP INDEX index_name ON talbe_name ALTER TABLE table_name DROP INDEX index_name ALTER TABLE table_name DROP PRIMARY KEY ----其中,前两条语句是等价的,删除掉table_name中的索引index_name。 ----第条语句只在删除PRIMARY KEY索引时使用,因为一个表只可能有一个PRIMARY KEY索引,因此不需要指定索引名。 ----如果没有创建PRIMARY KEY索引,但表具有一个或多个UNIQUE索引,则MySQL将删除第一个UNIQUE索引。 ----如果从表中删除了某列,则索引会受到影响。对于多列组合的索引, ----如果删除其中的某列,则该列也会从索引中删除。如果删除组成索引的所有列,则整个索引将被删除。
=========================================================
----新增约束---- 字段一加字段二成为唯一约束 alter table 表名add constraint 约束名字unique(字段一,字段二) alter table Students add constraint Stu_Edition unique(StuName,StuAge) ----查找所有约束---- SELECT * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
----干掉约束: alter table 表名drop constraint 约束名 alter table Students drop constraint Stu_Edition
----sql server 约束查找 ----主键约束 SELECT tab.name AS [表名], idx.name AS [主键名称], col.name AS [主键列名] FROM sys.indexes idx JOIN sys.index_columns idxCol ON (idx.object_id = idxCol.object_id AND idx.index_id = idxCol.index_id AND idx.is_primary_key = 1) JOIN sys.tables tab ON (idx.object_id = tab.object_id) JOIN sys.columns col ON (idx.object_id = col.object_id AND idxCol.column_id = col.column_id); ----唯一约束 SELECT tab.name AS [表名], idx.name AS [约束名称], col.name AS [约束列名] FROM sys.indexes idx JOIN sys.index_columns idxCol ON (idx.object_id = idxCol.object_id AND idx.index_id = idxCol.index_id AND idx.is_unique_constraint = 1) JOIN sys.tables tab ON (idx.object_id = tab.object_id) JOIN sys.columns col ON (idx.object_id = col.object_id AND idxCol.column_id = col.column_id); ----外键约束 select oSub.name AS [子表名称], fk.name AS [外键名称], SubCol.name AS [子表列名], oMain.name AS [主表名称], MainCol.name AS [主表列名] from sys.foreign_keys fk JOIN sys.all_objects oSub ON (fk.parent_object_id = oSub.object_id) JOIN sys.all_objects oMain ON (fk.referenced_object_id = oMain.object_id) JOIN sys.foreign_key_columns fkCols ON (fk.object_id = fkCols.constraint_object_id) JOIN sys.columns SubCol ON (oSub.object_id = SubCol.object_id AND fkCols.parent_column_id = SubCol.column_id) JOIN sys.columns MainCol ON (oMain.object_id = MainCol.object_id AND fkCols.referenced_column_id = MainCol.column_id) ----Check约束 SELECT tab.name AS [表名], chk.name AS [Check约束名], col.name AS [列名], chk.definition FROM sys.check_constraints chk JOIN sys.tables tab ON (chk.parent_object_id = tab.object_id) JOIN sys.columns col ON (chk.parent_object_id = col.object_id AND chk.parent_column_id = col.column_id)
搜索
复制
标签:sys,name,object,sqlserver,约束,----,索引,id From: https://www.cnblogs.com/jankie1122/p/11108817.html