# Constraint

  1. sys.key_constraints: 这个视图包含了主键约束和唯一键约束的信息。
  2. sys.check_constraints: 这个视图包含了检查约束的信息。
  3. sys.default_constraints: 这个视图包含了默认约束的信息。
  4. sys.foreign_keys: 这个视图包含了外键约束的信息。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
-- 查询表的主键约束信息
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
create table edi_test(
a varchar(100) not null,
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
alter table dbo.edi_test drop column b; --SQL Error [5074] [S1000]: The object 'DF__edi_test__b__2E1BDC42' is dependent on column 'b'.
alter table dbo.edi_test drop constraint DF__edi_test__b__2E1BDC42;
alter table dbo.edi_test drop column b;

alter table dbo.edi_test drop column a;

In Oracle not null constraint can not find by view and default is not a constraint.

1
2
3
4
5
6
7
create table edi_test(
a varchar2(100) not null,
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.

1
2
3
alter table edi_test drop column b; 

alter table edi_test drop column a;

# Custom Types

1
2
3
4
5
6
CREATE TYPE dbo.String FROM VARCHAR(255);

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'
ORDER BY SelectedType.[name];
SelectedTypeName BaseTypeName SelectedTypeMaxBytes SelectedTypePrecision SelectedTypeScale SelectedTypeId BaseTypeId
String varchar 255 0 0 0 0

# 创建 temp table

1
2
3
4
5
6
create table #edi_temp (
head_id varchar(25),
transfer_time varchar(100)
);

select * from #edi_temp;

# 批量插入数据

1
2
3
4
5
insert into #edi_temp values
('H00039827', '2019/3/27 14:15:13'),
('H00039871', '2019/5/13 13:10:49'),
('H00039872', '2019/5/13 13:10:49'),
('H00039873', '2019/5/13 13:10:49')

# 多表联合更新

1
2
3
update edi_head
set transfer_time = cast(b.transfer_time as datetime) from edi_head a, #edi_temp b
where a.head_id = b.head_id;

# 常用函数

# ISNULL

1
2
3
--isnull 类似于 Oracle nvl
select isnull(null, 'S'); --S
select isnull('R', 'S'); --R

# GETDATE

1
2
3
--getdate() 类似于 Oracle sysdate
--如下类似于 Oracle: select sysdate from dual;
select getdate();
Edited on