-- 查询表的主键约束信息 SELECT kc.name AS constraint_name, col.name AS column_name FROM sys.key_constraints kc JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id AND kc.unique_index_id = ic.index_id JOIN sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id WHERE kc.parent_object_id = OBJECT_ID('your_table_name') AND kc.type ='PK';
-- 查询表的唯一键约束信息 SELECT kc.name AS constraint_name, col.name AS column_name FROM sys.key_constraints kc JOIN sys.index_columns ic ON kc.parent_object_id = ic.object_id AND kc.unique_index_id = ic.index_id JOIN sys.columns col ON ic.object_id = col.object_id AND ic.column_id = col.column_id WHERE kc.parent_object_id = OBJECT_ID('your_table_name') AND kc.type ='UQ';
-- 查询表的检查约束信息 SELECT cc.name AS constraint_name, OBJECT_NAME(parent_object_id) AS table_name, cc.definition AS check_definition FROM sys.check_constraints cc WHERE cc.parent_object_id = OBJECT_ID('your_table_name');
-- 查询表的默认约束信息 SELECT dc.name AS constraint_name, OBJECT_NAME(parent_object_id) AS table_name, col.name AS column_name, dc.definition AS default_definition FROM sys.default_constraints dc JOIN sys.columns col ON dc.parent_object_id = col.object_id AND dc.parent_column_id = col.column_id WHERE dc.parent_object_id = OBJECT_ID('your_table_name');
-- 查询表的外键约束信息 SELECT fk.name AS constraint_name, OBJECT_NAME(fk.parent_object_id) AS parent_table, col.name AS parent_column, OBJECT_NAME(fk.referenced_object_id) AS referenced_table, rcol.name AS referenced_column FROM sys.foreign_keys fk JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id JOIN sys.columns col ON fkc.parent_object_id = col.object_id AND fkc.parent_column_id = col.column_id JOIN sys.columns rcol ON fkc.referenced_object_id = rcol.object_id AND fkc.referenced_column_id = rcol.column_id WHERE fk.parent_object_id = OBJECT_ID('your_table_name');
In SQLServer not null constraint can not find by view and default is a constraint.
1 2 3 4 5 6 7 8 9 10 11 12 13
createtable edi_test( a varchar(100) notnull, b varchar(100) default('-'), c varchar(100) );
SELECT dc.name AS constraint_name, OBJECT_NAME(parent_object_id) AS table_name, col.name AS column_name, dc.definition AS default_definition FROM sys.default_constraints dc JOIN sys.columns col ON dc.parent_object_id = col.object_id AND dc.parent_column_id = col.column_id WHERE dc.parent_object_id = OBJECT_ID('edi_test');
constraint_name
table_name
column_name
default_definition
DF__edi_test__b__2E1BDC42
edi_test
b
(’-’)
Drop column need drop default constraint firstly but not null constraint not need.
1 2 3 4 5
altertable dbo.edi_test dropcolumn b; --SQL Error [5074] [S1000]: The object 'DF__edi_test__b__2E1BDC42' is dependent on column 'b'. altertable dbo.edi_test dropconstraint DF__edi_test__b__2E1BDC42; altertable dbo.edi_test dropcolumn b;
altertable dbo.edi_test dropcolumn a;
In Oracle not null constraint can not find by view and default is not a constraint.
1 2 3 4 5 6 7
createtable edi_test( a varchar2(100) notnull, b varchar2(100) default('-'), c varchar2(100) );
select*from user_constraints where table_name ='EDI_TEST';
OWNER
CONSTRAINT_NAME
CONSTRAINT_TYPE
TABLE_NAME
SEARCH_CONDITION
R_OWNER
R_CONSTRAINT_NAME
DELETE_RULE
STATUS
DEFERRABLE
DEFERRED
VALIDATED
GENERATED
BAD
RELY
LAST_CHANGE
INDEX_OWNER
INDEX_NAME
INVALID
VIEW_RELATED
APPS
SYS_C001485655
C
EDI_TEST
“A” IS NOT NULL
ENABLED
NOT DEFERRABLE
IMMEDIATE
VALIDATED
GENERATED NAME
2024-03-30 03:52:07 PM
Drop column not need drop not null constraint firstly.
DECLARE@StringValue dbo.String ='some value less than 256 characters'; SELECT@StringValue;
SELECT*FROM sys.types;
name
system_type_id
user_type_id
schema_id
principal_id
max_length
precision
scale
collation_name
is_nullable
is_user_defined
is_assembly_type
default_object_id
rule_object_id
is_table_type
image
34
34
4
16
0
0
1
0
0
0
0
0
text
35
35
4
16
0
0
SQL_Latin1_General_CP1_CI_AS
1
0
0
0
0
0
uniqueidentifier
36
36
4
16
0
0
1
0
0
0
0
0
date
40
40
4
3
10
0
1
0
0
0
0
0
time
41
41
4
5
16
7
1
0
0
0
0
0
datetime2
42
42
4
8
27
7
1
0
0
0
0
0
datetimeoffset
43
43
4
10
34
7
1
0
0
0
0
0
tinyint
48
48
4
1
3
0
1
0
0
0
0
0
smallint
52
52
4
2
5
0
1
0
0
0
0
0
int
56
56
4
4
10
0
1
0
0
0
0
0
smalldatetime
58
58
4
4
16
0
1
0
0
0
0
0
real
59
59
4
4
24
0
1
0
0
0
0
0
money
60
60
4
8
19
4
1
0
0
0
0
0
datetime
61
61
4
8
23
3
1
0
0
0
0
0
float
62
62
4
8
53
0
1
0
0
0
0
0
sql_variant
98
98
4
8016
0
0
1
0
0
0
0
0
ntext
99
99
4
16
0
0
SQL_Latin1_General_CP1_CI_AS
1
0
0
0
0
0
bit
104
104
4
1
1
0
1
0
0
0
0
0
decimal
106
106
4
17
38
38
1
0
0
0
0
0
numeric
108
108
4
17
38
38
1
0
0
0
0
0
smallmoney
122
122
4
4
10
4
1
0
0
0
0
0
bigint
127
127
4
8
19
0
1
0
0
0
0
0
hierarchyid
240
128
4
892
0
0
1
0
1
0
0
0
geometry
240
129
4
-1
0
0
1
0
1
0
0
0
geography
240
130
4
-1
0
0
1
0
1
0
0
0
varbinary
165
165
4
8000
0
0
1
0
0
0
0
0
varchar
167
167
4
8000
0
0
SQL_Latin1_General_CP1_CI_AS
1
0
0
0
0
0
binary
173
173
4
8000
0
0
1
0
0
0
0
0
char
175
175
4
8000
0
0
SQL_Latin1_General_CP1_CI_AS
1
0
0
0
0
0
timestamp
189
189
4
8
0
0
0
0
0
0
0
0
nvarchar
231
231
4
8000
0
0
SQL_Latin1_General_CP1_CI_AS
1
0
0
0
0
0
nchar
239
239
4
8000
0
0
SQL_Latin1_General_CP1_CI_AS
1
0
0
0
0
0
xml
241
241
4
-1
0
0
1
0
0
0
0
0
sysname
231
256
4
256
0
0
SQL_Latin1_General_CP1_CI_AS
0
0
0
0
0
0
String
167
257
1
255
0
0
SQL_Latin1_General_CP1_CI_AS
1
1
0
0
0
0
1 2 3 4 5 6 7 8 9 10 11 12 13 14
SELECT SelectedType.[name] AS SelectedTypeName , BaseType.[name] AS BaseTypeName , SelectedType.max_length AS SelectedTypeMaxBytes , SelectedType.[precision] AS SelectedTypePrecision , SelectedType.scale AS SelectedTypeScale , SelectedType.default_object_id AS SelectedTypeId , BaseType.default_object_id AS BaseTypeId FROM sys.types SelectedType JOIN sys.types BaseType ON BaseType.system_type_id = SelectedType.system_type_id WHERE BaseType.is_user_defined =0 AND SelectedType.is_user_defined =1 AND BaseType.[name] <>'sysname' ORDERBY SelectedType.[name];