-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathW02 Exploring the Data Dictionary.sql
More file actions
143 lines (122 loc) · 3.77 KB
/
Copy pathW02 Exploring the Data Dictionary.sql
File metadata and controls
143 lines (122 loc) · 3.77 KB
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
-- Week 2 Assignment
-- SHOW 1: Your queries and query results. The results should include the names of
-- all user tables in each database, when each was created, and last modified date.
-- WideWorldImporters, BowlingLeagueExample, SchoolScheduling
USE WideWorldImporters
SELECT
name,
create_date,
modify_date
FROM
sys.tables;
USE BowlingLeagueExample
SELECT
name,
create_date,
modify_date
FROM
sys.tables;
USE SchoolSchedulingExample
SELECT
name,
create_date,
modify_date
FROM
sys.tables;
-- SHOW 2: Your queries and query results for each of the three databases. The
-- results should include: the name of the column, the name of the table the
-- column belongs to, and the current maximum length for that column.
USE WideWorldImporters
SELECT
c.name AS ColumnName,
t.name AS TableName,
c.max_length AS MaximumLength
FROM
sys.columns AS c
JOIN
sys.tables AS t ON c.object_id = t.object_id
WHERE
c.name LIKE '%name%';
USE BowlingLeagueExample
SELECT
c.name AS ColumnName,
t.name AS TableName,
c.max_length AS MaximumLength
FROM
sys.columns AS c
JOIN
sys.tables AS t ON c.object_id = t.object_id
WHERE
c.name LIKE '%name%';
USE SchoolSchedulingExample
SELECT
c.name AS ColumnName,
t.name AS TableName,
c.max_length AS MaximumLength
FROM
sys.columns AS c
JOIN
sys.tables AS t ON c.object_id = t.object_id
WHERE
c.name LIKE '%name%';
-- SHOW 3
-- Part i: Queries and results which list the file name, file location, and file size
-- (as listed in the database_files catalog view without conversion) of any file
-- greater than or equal to size 1024.
USE master
SELECT
mf.name,
d.name,
mf.physical_name AS FileLocation,
mf.size,
(mf.size * 8) / 1024 AS FileSize
FROM
sys.master_files AS mf
JOIN sys.databases AS d ON d.database_id = mf.database_id
WHERE 1=1
AND mf.size >= 1024
AND d.name IN ('WideWorldImporters', 'BowlingLeagueExample', 'SchoolSchedulingExample');
-- Part ii: Queries and results which list the full size of each database in MB.
-- You will have to add the size for each database file using the SUM functionLinks
-- to an external site. and then include the calculations from the hint above.
-- (Video reviewLinks to an external site. on using math in your SQL.)
USE master
SELECT
d.name,
SUM(( mf.size * 8) / 1024) AS FileSize
FROM
sys.master_files AS mf
JOIN sys.databases AS d ON d.database_id = mf.database_id
WHERE
d.name IN ('WideWorldImporters', 'BowlingLeagueExample', 'SchoolSchedulingExample')
GROUP BY d.name;
-- Part iii: Show the screen in your Windows explorer where you navigate to the folder
-- which holds the files (listed in your query from part i). Identify them and compare
-- them to your results from steps i and ii. Your calculations from step ii should
-- match what you see in Windows!
USE master
SELECT
mf.name,
d.name,
mf.physical_name AS FileLocation,
mf.size,
(mf.size * 8) / 1024 AS FileSize
FROM
sys.master_files AS mf
JOIN sys.databases AS d ON d.database_id = mf.database_id
WHERE 1=1
AND mf.size >= 1024
AND d.name IN ('WideWorldImporters', 'BowlingLeagueExample', 'SchoolSchedulingExample');
-- SHOW 4 : The two items you discovered in the data dictionary and the corresponding
-- query results. Explain why you believe these would be important to keep an eye on.
-- sys.objects
USE SchoolSchedulingExample
SELECT object_id, name, type, create_date, modify_date
FROM sys.objects
WHERE type = 'U'
ORDER BY object_id;
-- is_nullable and information_schema.columns
USE SchoolSchedulingExample;
SELECT table_name, column_name, is_nullable
FROM information_schema.columns
ORDER BY table_name;