In this lab you will learn some advanced usage of few DDL and DML commands.
You will have to learn about the following in this lab:
Create the table(s) for your semester project.
WITH FK_Hierarchy AS (
SELECT
fk.name AS FK_Name,
OBJECT_NAME(fk.parent_object_id) AS ChildTable,
c1.name AS ChildColumn,
OBJECT_NAME(fk.referenced_object_id) AS ParentTable,
c2.name AS ParentColumn
FROM sys.foreign_keys fk
INNER JOIN sys.foreign_key_columns fkc ON fk.object_id = fkc.constraint_object_id
INNER JOIN sys.columns c1 ON fkc.parent_object_id = c1.object_id AND fkc.parent_column_id = c1.column_id
INNER JOIN sys.columns c2 ON fkc.referenced_object_id = c2.object_id AND fkc.referenced_column_id = c2.column_id
)
SELECT * FROM FK_Hierarchy
WHERE ParentTable = 'YourTargetTable';
-- Object counts
SELECT
'Tables' AS ObjectType, COUNT(*) AS Count
FROM sys.tables
UNION ALL
SELECT
'Views', COUNT(*)
FROM sys.views
UNION ALL
SELECT
'Stored Procedures', COUNT(*)
FROM sys.procedures
UNION ALL
SELECT
'Functions', COUNT(*)
FROM sys.objects
WHERE type IN ('FN', 'IF', 'TF'); -- Scalar, Inline, and Table-valued functions
-- Table storage sizes
SELECT
s.name AS SchemaName,
t.name AS TableName,
SUM(p.rows) AS [RowCount],
CAST(SUM(a.total_pages) * 8 / 1024.0 AS DECIMAL(10,2)) AS TotalSpaceMB,
CAST(SUM(a.used_pages) * 8 / 1024.0 AS DECIMAL(10,2)) AS UsedSpaceMB,
CAST((SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024.0 AS DECIMAL(10,2)) AS UnusedSpaceMB
FROM
sys.tables t
JOIN sys.schemas s ON t.schema_id = s.schema_id
JOIN sys.indexes i ON t.object_id = i.object_id
JOIN sys.partitions p ON i.object_id = p.object_id AND i.index_id = p.index_id
JOIN sys.allocation_units a ON p.partition_id = a.container_id
WHERE
i.index_id <= 1 -- clustered index or heap
GROUP BY
s.name, t.name
ORDER BY
TotalSpaceMB DESC;
To earn PASS status, system is about to evaluate you using multiple choice questions (MCQs) and True/False type questions.