當前位置:首頁 » 編程語言 » sql查看約束

sql查看約束

發布時間: 2023-04-10 06:55:36

⑴ 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 你自己的約束名稱;

熱點內容
撕裂者哪個配置好 發布:2024-11-02 07:58:49 瀏覽:990
c編寫的程序經過編譯 發布:2024-11-02 07:41:18 瀏覽:941
mc伺服器靈魂綁定如何出售 發布:2024-11-02 07:39:36 瀏覽:583
cf伺服器不同如何一起玩游戲 發布:2024-11-02 07:38:24 瀏覽:945
手機壓縮游戲 發布:2024-11-02 07:27:03 瀏覽:451
c語言的四書五經 發布:2024-11-02 07:21:42 瀏覽:742
vbaexcel資料庫 發布:2024-11-02 07:16:09 瀏覽:11
java線程的sleep 發布:2024-11-02 07:15:18 瀏覽:845
手機緩存清理器 發布:2024-11-02 07:07:03 瀏覽:312
安卓手機怎麼安裝程序 發布:2024-11-02 07:02:22 瀏覽:732