-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript2.sql
287 lines (271 loc) · 18.6 KB
/
script2.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
USE [master]
GO
/****** Object: Database [Capital Market] Script Date: 8/15/2018 5:35:55 PM ******/
CREATE DATABASE [Capital Market]
CONTAINMENT = NONE
ON PRIMARY
( NAME = N'Capital Market', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Capital Market.mdf' , SIZE = 8192KB , MAXSIZE = UNLIMITED, FILEGROWTH = 65536KB )
LOG ON
( NAME = N'Capital Market_log', FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\Capital Market_log.ldf' , SIZE = 8192KB , MAXSIZE = 2048GB , FILEGROWTH = 65536KB )
GO
ALTER DATABASE [Capital Market] SET COMPATIBILITY_LEVEL = 130
GO
IF (1 = FULLTEXTSERVICEPROPERTY('IsFullTextInstalled'))
begin
EXEC [Capital Market].[dbo].[sp_fulltext_database] @action = 'enable'
end
GO
ALTER DATABASE [Capital Market] SET ANSI_NULL_DEFAULT OFF
GO
ALTER DATABASE [Capital Market] SET ANSI_NULLS OFF
GO
ALTER DATABASE [Capital Market] SET ANSI_PADDING OFF
GO
ALTER DATABASE [Capital Market] SET ANSI_WARNINGS OFF
GO
ALTER DATABASE [Capital Market] SET ARITHABORT OFF
GO
ALTER DATABASE [Capital Market] SET AUTO_CLOSE OFF
GO
ALTER DATABASE [Capital Market] SET AUTO_SHRINK OFF
GO
ALTER DATABASE [Capital Market] SET AUTO_UPDATE_STATISTICS ON
GO
ALTER DATABASE [Capital Market] SET CURSOR_CLOSE_ON_COMMIT OFF
GO
ALTER DATABASE [Capital Market] SET CURSOR_DEFAULT GLOBAL
GO
ALTER DATABASE [Capital Market] SET CONCAT_NULL_YIELDS_NULL OFF
GO
ALTER DATABASE [Capital Market] SET NUMERIC_ROUNDABORT OFF
GO
ALTER DATABASE [Capital Market] SET QUOTED_IDENTIFIER OFF
GO
ALTER DATABASE [Capital Market] SET RECURSIVE_TRIGGERS OFF
GO
ALTER DATABASE [Capital Market] SET DISABLE_BROKER
GO
ALTER DATABASE [Capital Market] SET AUTO_UPDATE_STATISTICS_ASYNC OFF
GO
ALTER DATABASE [Capital Market] SET DATE_CORRELATION_OPTIMIZATION OFF
GO
ALTER DATABASE [Capital Market] SET TRUSTWORTHY OFF
GO
ALTER DATABASE [Capital Market] SET ALLOW_SNAPSHOT_ISOLATION OFF
GO
ALTER DATABASE [Capital Market] SET PARAMETERIZATION SIMPLE
GO
ALTER DATABASE [Capital Market] SET READ_COMMITTED_SNAPSHOT OFF
GO
ALTER DATABASE [Capital Market] SET HONOR_BROKER_PRIORITY OFF
GO
ALTER DATABASE [Capital Market] SET RECOVERY FULL
GO
ALTER DATABASE [Capital Market] SET MULTI_USER
GO
ALTER DATABASE [Capital Market] SET PAGE_VERIFY CHECKSUM
GO
ALTER DATABASE [Capital Market] SET DB_CHAINING OFF
GO
ALTER DATABASE [Capital Market] SET FILESTREAM( NON_TRANSACTED_ACCESS = OFF )
GO
ALTER DATABASE [Capital Market] SET TARGET_RECOVERY_TIME = 60 SECONDS
GO
ALTER DATABASE [Capital Market] SET DELAYED_DURABILITY = DISABLED
GO
EXEC sys.sp_db_vardecimal_storage_format N'Capital Market', N'ON'
GO
ALTER DATABASE [Capital Market] SET QUERY_STORE = OFF
GO
USE [Capital Market]
GO
ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET LEGACY_CARDINALITY_ESTIMATION = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET MAXDOP = 0;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET MAXDOP = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET PARAMETER_SNIFFING = ON;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET PARAMETER_SNIFFING = PRIMARY;
GO
ALTER DATABASE SCOPED CONFIGURATION SET QUERY_OPTIMIZER_HOTFIXES = OFF;
GO
ALTER DATABASE SCOPED CONFIGURATION FOR SECONDARY SET QUERY_OPTIMIZER_HOTFIXES = PRIMARY;
GO
USE [Capital Market]
GO
/****** Object: Table [dbo].[Client] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Client](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [varchar](500) NOT NULL,
[MobileNo] [varchar](500) NOT NULL,
[Email] [varchar](500) NOT NULL,
[LastName] [varchar](500) NOT NULL,
[UserName] [varchar](500) NOT NULL,
[QuestionID] [int] NOT NULL,
[Answer] [varchar](6500) NOT NULL,
[IsLocked] [int] NOT NULL,
[Password] [varchar](500) NOT NULL,
[UserTypeID] [int] NOT NULL,
CONSTRAINT [PK_Client] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Orders] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Orders](
[ClientID] [int] NOT NULL,
[OrderID] [int] IDENTITY(1,1) NOT NULL,
[OrderQuantity] [int] NOT NULL,
[StockID] [int] NOT NULL,
[OrderDate] [date] NOT NULL,
[OrderType] [int] NOT NULL,
[OrderPrice] [float] NOT NULL,
CONSTRAINT [PK_Order] PRIMARY KEY CLUSTERED
(
[OrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Question] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Question](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Question] [varchar](50) NOT NULL,
CONSTRAINT [PK_Question] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[ResetPasswordRequest] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ResetPasswordRequest](
[id] [int] IDENTITY(1,1) NOT NULL,
[RequestDate] [date] NOT NULL,
[Flag] [int] NOT NULL,
[ClientID] [int] NOT NULL,
CONSTRAINT [PK_ResetPasswordRequest] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[Stock] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Stock](
[CompanyName] [varchar](50) NOT NULL,
[Price] [float] NOT NULL,
[ID] [int] IDENTITY(1,1) NOT NULL,
[ImageURL] [varchar](50) NULL,
[CompanyNameAr] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_Stock] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[UserTypes] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserTypes](
[ID] [int] IDENTITY(1,1) NOT NULL,
[UserType] [nvarchar](50) NOT NULL,
CONSTRAINT [PK_UserType] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Client] ADD CONSTRAINT [DF_Client_IsLocked] DEFAULT ((0)) FOR [IsLocked]
GO
ALTER TABLE [dbo].[Orders] ADD CONSTRAINT [DF_Order_OrderDate] DEFAULT (getdate()) FOR [OrderDate]
GO
ALTER TABLE [dbo].[ResetPasswordRequest] ADD DEFAULT (getdate()) FOR [RequestDate]
GO
ALTER TABLE [dbo].[Client] WITH CHECK ADD CONSTRAINT [FK_Client_Question] FOREIGN KEY([UserTypeID])
REFERENCES [dbo].[UserTypes] ([ID])
GO
ALTER TABLE [dbo].[Client] CHECK CONSTRAINT [FK_Client_Question]
GO
ALTER TABLE [dbo].[Client] WITH CHECK ADD CONSTRAINT [FK_Client_Question1] FOREIGN KEY([QuestionID])
REFERENCES [dbo].[Question] ([ID])
GO
ALTER TABLE [dbo].[Client] CHECK CONSTRAINT [FK_Client_Question1]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Client] FOREIGN KEY([ClientID])
REFERENCES [dbo].[Client] ([ID])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_Orders_Client]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Stock] FOREIGN KEY([StockID])
REFERENCES [dbo].[Stock] ([ID])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_Orders_Stock]
GO
/****** Object: StoredProcedure [dbo].[GetClient] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[GetClient]
-- Add the parameters for the stored procedure here
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT
[dbo].[Client].[name], b.Ordq , b.totalvalue
from
[dbo].[Client]inner join(Select [ClientID] ,count([OrderQuantity]) as Ordq, sum([OrderCost]) as totalvalue from [dbo].[Orders] group by [ClientID] ) b
on [dbo].[Client].[ID]= b.ClientID
where
b.Ordq>5 and b.totalvalue>5000
END
GO
/****** Object: StoredProcedure [dbo].[GetMostFrequentCompanyByOrders] Script Date: 8/15/2018 5:35:55 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create Procedure [dbo].[GetMostFrequentCompanyByOrders]
as
SELECT top 1 [CompanyOfStock], count([OrderQuantity]) as Quantity
From
[dbo].[Orders]
group by [CompanyOfStock] Order by Quantity Desc
GO
USE [master]
GO
ALTER DATABASE [Capital Market] SET READ_WRITE
GO