forked from shawlu95/Beyond-LeetCode-SQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLIndex.sql
More file actions
75 lines (57 loc) · 2.15 KB
/
Copy pathSQLIndex.sql
File metadata and controls
75 lines (57 loc) · 2.15 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
--CREATE TABLE Employees (
-- Id INT PRIMARY KEY IDENTITY,
-- [Name] NVARCHAR(50),
-- Email NVARCHAR(50),
-- Department NVARCHAR(50)
-- )
------------------------------------START-Index-------------------------------------------------
-- Create the Employees table if it doesn't exist
IF OBJECT_ID('Employees', 'U') IS NULL
BEGIN
CREATE TABLE Employees (
Id INT PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50),
Email NVARCHAR(50),
Department NVARCHAR(50)
)
END
-- Declare variables
DECLARE @Counter INT = 1
DECLARE @MaxRecords INT = 10000
-- Start inserting records
WHILE @Counter <= @MaxRecords
BEGIN
DECLARE @Name NVARCHAR(50)
DECLARE @Email NVARCHAR(50)
DECLARE @Department NVARCHAR(50)
-- Generate random values for Name, Email, and Department (you can replace this with your own logic)
SET @Name = 'Employee' + CAST(@Counter AS NVARCHAR(10))
SET @Email = 'email' + CAST(@Counter AS NVARCHAR(10)) + '@company.com'
SET @Department = 'Department' + CAST((@Counter % 10) + 1 AS NVARCHAR(10))
-- Insert the record
INSERT INTO Employees ([Name], Email, Department)
VALUES (@Name, @Email, @Department)
SET @Counter = @Counter + 1
END
Select * from Employees where Id=999
Select * from Employees where Name='Employee999'
----------Create--Name-as-Non-Clustered-Index------------
--USE [Testing]
--GO
--CREATE NONCLUSTERED INDEX [<Name of Missing Index, sysyname,>]
--ON [dbo].[Employees]([Name])
--GO
CREATE NONCLUSTERED INDEX IX_Employees_Name
ON [dbo].[Employees]([Name])
----------------------------------------------------------------------------------------
Select Name from Employees Where id= 536
Select Name, Email from Employees Where Name='Employee536'
Create Index IX_Employees_Name ON Employees (Name ASC)
Execute sp_helpindex Employees
create Clustered Index IX_Employees_Department_Email
ON Employees(Department DESC, Email Asc)
--drop index Employees.IX_Employees_Name
--Adding Unique constraint to non clustered index
ALTER TABLE Employees
ADD CONSTRAINT UQ_Employees_Name
UNIQUE (Name)