-
Notifications
You must be signed in to change notification settings - Fork 0
/
GroceryAppSQL2.0.sql
201 lines (153 loc) · 4.66 KB
/
GroceryAppSQL2.0.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
198
199
200
201
-- Create new database called 'GroceriesDb'
-- Connect to the 'master' database to run this snippet
USE master
GO
-- Create the new database if it does not exist already
IF NOT EXISTS (
SELECT [name]
FROM sys.databases
WHERE [name] = N'GroceryListDB'
)
CREATE DATABASE GroceryListDB
GO
USE GroceryListDB
-- Create a table called '[Family]' in schema '[dbo]'
-- Drop the table if it already exists
IF OBJECT_ID('[DBO].[Family]', 'U') IS NOT NULL
DROP TABLE [dbo].Family
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[Family]
(
[Id] INT IDENTITY NOT NULL PRIMARY KEY, -- Primary Key Columnn,
[Name] NVARCHAR(255) NOT NULL,
[DateCreated] DATE NOT NULL
)
GO
-- Create a new table called '[User]' in schema '[dbo]'
-- Drop the table if it already exists
IF OBJECT_ID('[DBO].[User]', 'U') IS NOT NULL
DROP TABLE [dbo].[User]
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[User]
(
[Id] INT IDENTITY NOT NULL PRIMARY KEY, -- Primary Key Columnn
[FirstName] NVARCHAR(255) NOT NULL,
[LastName] NVARCHAR(255) NOT NULL,
[SignUpDate] DATETIME NOT NULL,
[Uid] NVARCHAR(255) NOT NULL,
[Email] NVARCHAR(255) NOT NULL,
[IsActive] BIT NOT NULL,
[FamilyId] INT NOT NULL
FOREIGN KEY (FamilyId)
REFERENCES Family (Id)
)
GO
-- Create a new table called '[GroceryList]' in schema '[dbo]'
-- Drop the table if it already exists
IF OBJECT_ID('[DBO].[GroceryList]', 'U') IS NOT NULL
DROP TABLE [dbo].[GroceryList]
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[GroceryList]
(
[Id] INT IDENTITY NOT NULL PRIMARY KEY, -- Primary Key Columnn,
[Name] NVARCHAR(255) NOT NULL,
[UserId] INT NOT NULL
FOREIGN KEY (UserId)
REFERENCES [User] (Id),
[DateCreated] DATE NOT NULL
)
GO
---- Create a table called '[Recipe]' in schema '[dbo]'
---- Drop the table if it already exists
--IF OBJECT_ID('[DBO].[Recipe]', 'U') IS NOT NULL
--DROP TABLE [dbo].Recipe
--GO
---- Create the table in the specified schema
--CREATE TABLE [dbo].[Recipe]
--(
-- [Id] INT IDENTITY NOT NULL PRIMARY KEY, -- Primary Key Columnn,
-- [Name] NVARCHAR(255) NOT NULL,
-- [Ingredients] NVARCHAR(MAX) NOT NULL,
-- [Instructions] NVARCHAR(MAX) NOT NULL,
-- [UserId] INT NOT NULL,
-- FOREIGN KEY (UserId)
-- REFERENCES [User] (Id),
-- [CookIt] BIT NOT NULL
--)
--GO
-- Create a table called '[GroceryStore]' in schema '[dbo]'
-- Drop the table if it already exists
IF OBJECT_ID('[DBO].[GroceryStore]', 'U') IS NOT NULL
DROP TABLE [dbo].GroceryStore
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[GroceryStore]
(
[Id] INT IDENTITY NOT NULL PRIMARY KEY, -- Primary Key Columnn,
[Name] NVARCHAR(255) NOT NULL
)
GO
-- Create a new table called '[Item]' in schema '[dbo]'
-- Drop the table if it already exists
IF OBJECT_ID('[dbo].[Item]', 'U') IS NOT NULL
DROP TABLE [dbo].[Item]
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].Item
(
[Id] INT IDENTITY NOT NULL PRIMARY KEY, -- Primary Key column
[Name] NVARCHAR(255) NOT NULL,
[GroceryListId] INT NOT NULL
FOREIGN KEY (GroceryListId)
REFERENCES GroceryList (Id),
[GroceryStoreId] INT NOT NULL
FOREIGN KEY (GroceryStoreId)
REFERENCES GroceryStore (Id)
);
GO
-- SEED DATA
USE GroceriesDb
GO
INSERT INTO Family (Name, DateCreated)
VALUES ('Pantana', '2019-12-03')
INSERT INTO [User] (FirstName, LastName, IsActive, FamilyId, [Uid], Email, SignUpDate)
VALUES ('Josh', 'Pantana', 1, 1, 'lsfjsf8s', '[email protected]', '1900-01-01 00:00:00')
INSERT INTO GroceryList (Name, UserId, DateCreated)
VALUES ('Our Groceries', 1, '2019-12-03')
INSERT INTO GroceryStore (Name)
VALUES ('Kroger')
INSERT INTO Item (Name, GroceryListId, GroceryStoreId)
VALUES ('Milk', 1, 1)
SELECT * from Item
SELECT * from [User]
SELECT * from GroceryStore
SELECT * from GroceryList
SELECT * from Family
--"FirstName": "Jason",
--"LastName": "Pantana",
--"Uid": "asfg",
--"email": "[email protected]"
ALTER TABLE [User] ALTER COLUMN FirstName NVARCHAR(255) NULL
ALTER TABLE [User] ALTER COLUMN LastName NVARCHAR(255) NULL
DELETE FROM [User] WHERE Id = 17
DELETE FROM [User] WHERE Id = 18
DELETE FROM [User] WHERE Id = 19
DELETE FROM [User] WHERE Id = 20
DELETE FROM [User] WHERE Id = 21
DELETE FROM [User] WHERE Id = 22
DELETE FROM [User] WHERE Id = 23
DELETE FROM [User] WHERE Id = 24
DELETE FROM [User] WHERE Id = 25
DELETE FROM [User] WHERE Id = 26
DELETE FROM [User] WHERE Id = 37
DELETE FROM [User] WHERE Id = 38
select * from [User]
DELETE FROM GroceryList WHERE Id = 14
DELETE FROM GroceryList WHERE Id = 13
DELETE FROM GroceryList WHERE Id = 15
DELETE FROM GroceryList WHERE Id = 17
DELETE From [Item]
Where [Id] = 9;