sql查看约束
⑴ plsql有约束条件提示,如何查这个约束条件在哪个table上
主键约束核敬
SELECT
USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约束名,
USER_CONS_COLUMNS.TABLE_NAME AS 表名,
USER_CONS_COLUMNS.COLUMN_NAME AS 列名,
USER_CONS_COLUMNS.POSITION AS 位置
FROM
USER_CONSTRAINTS
JOIN USER_CONS_COLUMNS
ON (USER_CONSTRAINTS.CONSTRAINT_NAME
= USER_CONS_COLUMNS.CONSTRAINT_NAME)
WHERE
CONSTRAINT_TYPE = 'P';
外键约束
SELECT
USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约改困慎束名,
USER_CONS_COLUMNS.TABLE_NAME AS 子表名,
USER_CONS_COLUMNS.COLUMN_NAME AS 子表列名,
USER_CONS_COLUMNS.POSITION AS 位尺册置,
USER_INDEXES.TABLE_NAME AS 主表名,
USER_IND_COLUMNS.COLUMN_NAME AS 主表列名
FROM
USER_CONSTRAINTS
JOIN USER_CONS_COLUMNS
ON (USER_CONSTRAINTS.CONSTRAINT_NAME
= USER_CONS_COLUMNS.CONSTRAINT_NAME)
JOIN USER_INDEXES
ON (USER_CONSTRAINTS.R_CONSTRAINT_NAME
= USER_INDEXES.INDEX_NAME)
JOIN USER_IND_COLUMNS
ON (USER_INDEXES.INDEX_NAME = USER_IND_COLUMNS.INDEX_NAME)
WHERE
CONSTRAINT_TYPE = 'R';
其他约束
SELECT
USER_CONS_COLUMNS.CONSTRAINT_NAME AS 约束名,
USER_CONS_COLUMNS.TABLE_NAME AS 表名,
USER_CONS_COLUMNS.COLUMN_NAME AS 列名,
USER_CONS_COLUMNS.POSITION AS 位置,
CONSTRAINT_TYPE,
SEARCH_CONDITION
FROM
USER_CONSTRAINTS
JOIN USER_CONS_COLUMNS
ON (USER_CONSTRAINTS.CONSTRAINT_NAME
= USER_CONS_COLUMNS.CONSTRAINT_NAME)
WHERE
CONSTRAINT_TYPE IN ('C', 'V', 'O');
⑵ sqlserver数据库里怎么查找一个表里的约束,要查出 约束名、列名、约束类型用sql语句查询
主键约束
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)
⑶ sql 约束名
SQL 约束(Constraints)
SQL 约束用于规定表中的数据规则。
如果存在违反约束的数据行为,行为会被约束终止。
约束可以在创建表时规定(通过 CREATE TABLE 语句),或者在表创建之后规定(通过 ALTER TABLE 语句)。
⑷ Sql Server 2000 数据库,如何查看表的相关详细信息。如:主键、索引、约束、是否空值、默认值等等。谢谢
我提供几点建议,供参考:
1.到系统表中查锋和看相关约束。(这种方法不明显,因为要看表里的内容,不直观)
2.分两步.
第一步,在企业管理器中用鼠标右击数银迹盯据库,选择生成脚本,然后在脚本(。SQL)文件中查看。从这州或里能看到创建表示定义的主要主键、索引、约束、是否空值、默认值等等。
第二步,在企业管理器中左击数据库,点开约束项,看是否有定义的约束条件。
先写这两点,水平有限了。
⑸ sql server怎么查询某一表的某一字段是否有约束
示例:
CREATE TABLE AA (
BB INT UNIQUE
);
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)
WHERE
tab.name = 'AA'
结果:
表名 约束名称 约束列名
AA UQ__AA__3214B5C34460231C BB
⑹ 如何在SQL表中的性别进行check约束
1、下顷蠢罩面打开SQL SERVER查看一下要操作的表的数据结构。
⑺ sql server 查询约束键在哪个表的脚本
查询一个表的所有外键
SELECT主键列ID=b.rkey
,主键列名=(=b.rkeyANDid=b.rkeyid)
,外键表ID=b.fkeyid
,外键表名称=object_name(b.fkeyid)
,外键列ID=b.fkey
,外键列名=(=b.fkeyANDid=b.fkeyid)
,级联更新=ObjectProperty(a.id,'CnstIsUpdateCascade')
,级联删除=ObjectProperty(a.id,'CnstIsDeleteCascade')
FROMsysobjectsa
joinsysforeignkeysbona.id=b.constid
joinsysobjectscona.parent_obj=c.id
wherea.xtype='f'ANDc.xtype='U'
andobject_name(b.rkeyid)='titles'
SELECT*
FROMinformation_schema.columns
WHERETABLE_CATALOG='数据库名'
ANDTABLE_NAME='表名'
ANDCOLUMN_NAME='列名'
select*
fromsyscolumns
whereid=object_id('tableName')andname='fieldName'
⑻ 如何查看SQL表中都存在哪些约束
alt+F1就可以查看该表的表结构以及各种约束
⑼ sql查询表中的约束
exec sp_helpconstraint S
ALTER TABLE S DROP CONSTRAINT 你自己的约束名称;