sql查看表约束
1. 如何查看sql表中都存在哪些约束
alt+F1就可以查看该表的表结构以及各种约束
2. sql中如何查看已定义表的完整性约束
SQL Server 2008 下可用
SELECT
tab.nameAS[表名],
chk.nameAS[Check约束名],
col.nameAS[列名],
chk.definition
FROM
sys.check_constraintschk
JOINsys.tablestab
ON(chk.parent_object_id=tab.object_id)
JOINsys.columnscol
ON(chk.parent_object_id=col.object_id
ANDchk.parent_column_id=col.column_id)
3. sql查询表中的约束
exec sp_helpconstraint S
ALTER TABLE S DROP CONSTRAINT 你自己的约束名称;
4. SQL中用语句查看表列的约束以及解除约束
SELECT
USER_CONS_COLUMNS.CONSTRAINT_NAMEAS约束名,
USER_CONS_COLUMNS.TABLE_NAMEAS表名,
USER_CONS_COLUMNS.COLUMN_NAMEAS列名,
USER_CONS_COLUMNS.POSITIONAS位置,
CONSTRAINT_TYPE,
SEARCH_CONDITION
FROM
USER_CONSTRAINTS
JOINUSER_CONS_COLUMNS
ON(USER_CONSTRAINTS.CONSTRAINT_NAME
=USER_CONS_COLUMNS.CONSTRAINT_NAME)
WHERE
CONSTRAINT_TYPEIN('C','V','O');
其实主要就是看
USER_CONSTRAINTS与 USER_CONS_COLUMNS 这2个视图.
数据库管理员的话
可以看
DBA_CONSTRAINTS与 DBA_CONS_COLUMNS 这2个视图.
5. SQL Server 2008 如何查看与创建约束
SQL Server中有五种约束类型,分别是 PRIMARY KEY约束、FOREIGN KEY约束、UNIQUE约束、DEFAULT约束、和CHECK约束。查看或者创建约束都要使用到 Microsoft SQL Server Managment Studio。 1. PRIMARY KEY约束 在表中常有一列或多列的组合,其值能唯一标识表中的每一行。这样的一列或多列成为表的主键(Primary Key)。一个表只能有一个主键,而且主键约束中的列不能为空值。 查看PRIMARY KEY约束可以在object explorer中依次展开Databases – 选择你要查看的数据库(在我的例子中是testdatabase) – Tables – 你要查看的表(在我的例子中是company)- Columns 如上图所示,Primary Key 有一把金色的小钥匙。companyid 即为company表的primary key。 创建PRIMARY KEY约束可以右键点击表,然后选择Design,打开表设计器 选中column,点击上面的金色小钥匙,来创建Primary Key。 也可以右键点击column,然后选择Set Primary Key。
6. 如何写SQL查询一个约束条件的内容
在oracle中有两个系统表可以查询
user_constraints约束表,记录该用户的所有约束。如非空,索引等等。
user_cons_columns约束字段表,记录每个约束涉及到的所有字段。
7. 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
8. 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)
9. sql语句,怎么查看一个表中的所有约束
sp_mshelpcolumns student
如上可以看到该表的所有结构、约束、默认值等