-
Notifications
You must be signed in to change notification settings - Fork 0
/
About-DatabaseAndTables.sql
197 lines (157 loc) · 5.21 KB
/
About-DatabaseAndTables.sql
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
-- USE AdventureWorks2019;
-- all system objects in the databse
select * from sys.objects;
-- all scheams in the database
select * from sys.schemas;
-- All tables in database
select * from sys.tables;
-- about the adventure works database objects and tables
-- count of each object type in the adventure works database
select
type_desc,
count(type_desc) total_count
from sys.objects
group by type_desc
order by 2 desc;
/*
we have total 71 user_tables.
71 primary_key_constraint
20 Views
10 stored procedures and triggers
*/
-- count of schemas, tables, and columns
select count(DISTINCT s.name) as SchemaName,
count(Distinct t.name) as TableName,
count(c.name) as ColumnName
from sys.tables t
JOIN sys.columns c on c.object_id = t.object_id
INNER JOIN sys.schemas s on s.schema_id = t.schema_id
/*
486 rows means columns in the database
71 tables
6 Schemas
*/
-- same query as above with INFORMATION_SCHEMA
select
count(distinct TABLE_SCHEMA) as Table_Schema,
count(distinct TABLE_NAME) as table_name,
count(COLUMN_NAME) as coulmn_name
from INFORMATION_SCHEMA.COLUMNS
where
TABLE_NAME not in(select TABLE_NAME from INFORMATION_SCHEMA.VIEWS)
/*
Same as above query
486 rows or columns in the database
71 tables
6 Schemas
*/
select s.name as SchemaName,
t.name as TableName,
c.name as ColumnName
from sys.tables t
JOIN sys.columns c on c.object_id = t.object_id
INNER JOIN sys.schemas s on s.schema_id = t.schema_id
/*
All column name inside each table name under each Schma
*/
-- Check constraints
select
t.name as TableName,
c.name as ColumnName,
cc.name as CheckConstraintName,
cc.definition as CheckConstraintsDefinition
from AdventureWorks2019.sys.check_constraints cc
inner join AdventureWorks2019.sys.tables t on t.object_id = cc.parent_object_id
left join AdventureWorks2019.sys.columns c on c.object_id = cc.parent_object_id
and c.column_id = cc.parent_column_id
/*
89 rows - constraints
left join and inner join give different number of constraint.
because some constraint are on tables only. or multiple constraint applies on columns.
TableName ColumnName CheckConstraintname Check Constraint header
SalesOrderHeader Freight CK_SalesOrderHeader_Freight ([Freight]>=(0.00))
EmployeePayHistory PayFrequency CK_EmployeePayHistory_PayFrequency ([PayFrequency]=(2) OR [PayFrequency]=(1))
EmployeePayHistory Rate CK_EmployeePayHistory_Rate ([Rate]>=(6.50) AND [Rate]<=(200.00))
...
*/
-- Analyzing default constraints
select
type_desc,
count(type_desc) total_count
from sys.objects
where
type_desc = 'DEFAULT_CONSTRAINT'
group by type_desc
order by 2 desc;
-- 152 constraints
-- tables and columns are these constraints on. And what are the default values?
select
s.name as schemaName,
t.name as tableName,
c.name as columnName,
dc.name as defaultConstraintName,
dc.definition as defaultConstraintDefinition
from sys.default_constraints dc
INNER JOIN sys.tables t on
dc.parent_object_id = t.object_id
inner join sys.schemas s on s.schema_id = dc.schema_id
inner join sys.columns c on c.column_id = dc.parent_column_id and c.object_id = dc.parent_object_id
-- defaultConstraintDefinition tells about the default values
-- find every column in the database that includes "rate" in the column name.
-- find every table in the database that includes "history" in the column name.
select
t.name as TableName,
c.name as ColumnName
from sys.tables t
inner join sys.columns c on t.object_id = c.object_id
where 1=1 and
-- t.name like '%history%';
c.name like '%rate%';
-- count of each data type in the AdventureWorks Database and finding most used data type.
select
data_type,
count(*) AS dataTypeCount
from
INFORMATION_SCHEMA.COLUMNS
group by
DATA_TYPE
order by count(*) desc;
-- Using a case statement create a data type grouping and summarizes each data type as one of the Groups
select
case when CHARACTER_MAXIMUM_LENGTH is not null then 'Character'
when NUMERIC_PRECISION is not null then ' Neumeric'
when DATETIME_PRECISION is not null then 'Date'
else null
end as 'DataTypeGroup',
count(*) as DataTypeCount
from INFORMATION_SCHEMA.COLUMNS
GROUP BY
case when CHARACTER_MAXIMUM_LENGTH is not null then 'Character'
when NUMERIC_PRECISION is not null then ' Neumeric'
when DATETIME_PRECISION is not null then 'Date'
else null
end
order by count(*) desc;
-- data types in null group above
select
distinct data_type
from
INFORMATION_SCHEMA.columns
where
CHARACTER_MAXIMUM_LENGTH is null
and NUMERIC_PRECISION is null
and DATETIME_PRECISION is null
-- check for column name and definition of any table in database
select
t.name as TableName,
c.name as ColumnName,
ep.value as Definition
from
sys.extended_properties ep
inner join sys.tables t
on t.object_id = ep.major_id
inner join sys.columns c
on c.object_id = ep.major_id
and c.column_id = ep.minor_id
where class = 1
and t.name = 'Person'