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
如上可以看到該表的所有結構、約束、默認值等