forked from SeemaSP/shoppingjquerycart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7 - Clustered Indexes.sql
48 lines (34 loc) · 1.09 KB
/
7 - Clustered Indexes.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
-- Demonstration 7 - Clustered Indexes
-- Step 1: Open a new query window against the tempdb database
USE tempdb;
GO
-- Create a table without a primary key
CREATE TABLE dbo.PhoneLog
( PhoneLogID int IDENTITY(1,1),
LogRecorded datetime2 NOT NULL,
PhoneNumberCalled nvarchar(100) NOT NULL,
CallDurationMs int NOT NULL
);
GO
-- Show execution plan for a scan
SELECT * FROM dbo.PhoneLog
-- Drop table
DROP TABLE dbo.PhoneLog
-- Step 2: Create a table with a primary key specified
CREATE TABLE dbo.PhoneLog
( PhoneLogID int IDENTITY(1,1) PRIMARY KEY,
LogRecorded datetime2 NOT NULL,
PhoneNumberCalled nvarchar(100) NOT NULL,
CallDurationMs int NOT NULL
);
GO
-- Show execution plan for a scan
SELECT * FROM dbo.PhoneLog
-- Step 3: Query sys.indexes to view the structure
-- (note also the name chosen by SQL Server for the constraint and index)
SELECT * FROM sys.indexes WHERE OBJECT_NAME(object_id) = N'PhoneLog';
GO
SELECT * FROM sys.key_constraints WHERE OBJECT_NAME(parent_object_id) = N'PhoneLog';
GO
-- Drop table
DROP TABLE dbo.PhoneLog