USE MASTER GO IF db_id Civilizations IS NOT NULL DROP DATABASE Civiliz

  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
USE MASTER;
GO
IF db_id (N'Civilizations') IS NOT NULL
DROP DATABASE Civilizations;
GO
CREATE DATABASE Civilizations
ON (NAME = Civ_data, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Civ.mdf',
SIZE = 10MB, MAXSIZE = 50MB, FILEGROWTH = 5MB)
LOG ON (NAME = Civ_log, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Civ.ldf',
SIZE = 5MB, MAXSIZE = 25MB, FILEGROWTH = 5MB);
GO
USE Civilizations;
GO
CREATE TABLE country (
countryid int IDENTITY(1,1) Primary key NOT NULL,
namec nvarchar(30) NOT NULL,
unique_discription text NOT NULL DEFAULT 'описание отсутствует',
--flag image NOT NULL DEFAULT 'f'
);
GO
CREATE TABLE ruler (
rulerid int IDENTITY(1, 1) PRIMARY KEY NOT NULL,
namer nvarchar(30) NOT NULL,
Feature_I nvarchar(30) NOT NULL DEFAULT 'Черта отсутствует',
Feature_II nvarchar(30) NOT NULL DEFAULT 'Черта отсутствует',
ruler_country int FOREIGN KEY REFERENCES country(countryid));
GO
CREATE TABLE technology (
technologyid int IDENTITY(1, 1) PRIMARY KEY NOT NULL,
namet nvarchar(30) NOT NULL,
cost int NOT NULL DEFAULT 0,
research_time int NOT NULL DEFAULT 0,
research_direction int NOT NULL DEFAULT 0,
era nvarchar(30) NOT NULL DEFAULT 'stone age');
GO
CREATE TABLE building (
buildingid int IDENTITY(1, 1) PRIMARY KEY NOT NULL,
nameb nvarchar(30) NOT NULL,
happines int NOT NULL DEFAULT 0,
health int NOT NULL DEFAULT 0,
production int NOT NULL DEFAULT 0,
research int NOT NULL DEFAULT 0,
gold int NOT NULL DEFAULT 0);
GO
CREATE TABLE unit (
unitid int IDENTITY(1, 1) PRIMARY KEY NOT NULL,
poweru int NOT NULL DEFAULT 0,
speed int NOT NULL DEFAULT 0,
defense int NOT NULL DEFAULT 0,
health int NOT NULL DEFAULT 0,
prise int NOT NULL DEFAULT 0,
creation_time int NOT NULL DEFAULT 0);
GO
CREATE TABLE chariots (
chariotsid int PRIMARY KEY NOT NULL FOREIGN KEY REFERENCES unit(unitid),
nameu nvarchar(30) NOT NULL DEFAULT 'chariots',
three_horses bit NOT NULL DEFAULT (0),
blades bit NOT NULL DEFAULT 0);
GO
CREATE TABLE archers (
chariotsid int PRIMARY KEY NOT NULL FOREIGN KEY REFERENCES unit(unitid),
nameu nvarchar(30) NOT NULL DEFAULT 'archer',
firebolt bit NOT NULL DEFAULT (0),
stakes bit NOT NULL DEFAULT 0);
GO
CREATE TABLE CT(
technologyFK int NOT NULL FOREIGN KEY REFERENCES technology(technologyid),
countryFK int NOT NULL FOREIGN KEY REFERENCES country(countryid),
PRIMARY KEY (technologyFK, countryFK),
);
GO
CREATE TABLE BT(
technologyFK int NOT NULL FOREIGN KEY REFERENCES technology(technologyid),
buildingFK int NOT NULL FOREIGN KEY REFERENCES building(buildingid),
PRIMARY KEY (technologyFK, buildingFK),
);
GO
CREATE TABLE UT(
technologyFK int NOT NULL FOREIGN KEY REFERENCES technology(technologyid),
unitFK int NOT NULL FOREIGN KEY REFERENCES unit(unitid),
PRIMARY KEY (technologyFK, unitFK),
);
GO
CREATE TABLE UC(
countryFK int NOT NULL FOREIGN KEY REFERENCES country(countryid),
unitFK int NOT NULL FOREIGN KEY REFERENCES unit(unitid),
PRIMARY KEY (countryFK, unitFK),
);
GO
CREATE TABLE UU(
unitFK1 int NOT NULL FOREIGN KEY REFERENCES unit(unitid),
unitFK2 int NOT NULL FOREIGN KEY REFERENCES unit(unitid),
PRIMARY KEY (unitFK1, unitFK2),
);
GO
CREATE TABLE TT(
technologyFK1 int NOT NULL FOREIGN KEY REFERENCES technology(technologyid),
technologyFK2 int NOT NULL FOREIGN KEY REFERENCES technology(technologyid),
PRIMARY KEY (technologyFK1, technologyFK2),
);
GO
USE Civilizations;
GO
IF OBJECT_ID ('Civilization', 'view') IS NOT NULL
DROP VIEW Civilization;
GO
CREATE VIEW Civilization WITH SCHEMABINDING AS
SELECT a.namec, a.unique_discription, b.namer, b.Feature_I, b.Feature_II FROM
dbo.country a INNER JOIN dbo.ruler b ON a.countryid = b.ruler_country
GO
USE Civilizations;
GO
IF OBJECT_ID ('insert_Civilization','TR') IS NOT NULL
DROP TRIGGER insert_Civilization;
GO
CREATE TRIGGER insert_Civilization ON Civilization INSTEAD OF INSERT
AS
BEGIN
INSERT country(namec) SELECT DISTINCT namec FROM inserted WHERE namec NOT IN (SELECT namec FROM country);
UPDATE country SET unique_discription =a.unique_discription FROM inserted a WHERE (country.namec = a.namec AND a.unique_discription IS NOT NULL)
INSERT ruler(ruler_country, namer) SELECT DISTINCT a.countryid, b.namer FROM dbo.country a INNER JOIN inserted b ON a.namec = b.namec WHERE b.namer NOT IN (SELECT namer FROM ruler);
UPDATE ruler SET Feature_I = a.Feature_I FROM inserted a WHERE (ruler.namer = a.namer AND a.Feature_I IS NOT NULL);
UPDATE ruler SET Feature_II = a.Feature_II FROM inserted a WHERE (ruler.namer = a.namer AND a.Feature_II IS NOT NULL);
END;
GO
IF OBJECT_ID ('delete_Civilization','TR') IS NOT NULL
DROP TRIGGER delete_Civilization;
GO
CREATE TRIGGER delete_Civilization ON Civilization INSTEAD OF DELETE
AS
DELETE FROM ruler WHERE namer IN (SELECT namer FROM deleted);
DELETE FROM country WHERE countryid NOT IN (SELECT ruler_country FROM ruler);
GO
IF OBJECT_ID ('update_Civilization','TR') IS NOT NULL
DROP TRIGGER update_Civilization;
GO
CREATE TRIGGER update_Civilization ON Civilization INSTEAD OF UPDATE
AS
IF (UPDATE(namec))
BEGIN
INSERT country(namec) SELECT DISTINCT namec FROM inserted WHERE namec NOT IN (SELECT namec FROM country);
UPDATE country SET unique_discription =a.unique_discription FROM inserted a WHERE (country.namec = a.namec AND a.unique_discription IS NOT NULL)
UPDATE ruler SET ruler_country = countryid FROM country a INNER JOIN inserted b ON a.namec =b.namec WHERE ruler.namer = b.namer;
DELETE FROM country WHERE countryid NOT IN (SELECT ruler_country FROM ruler);
END;
IF (UPDATE(unique_discription))
BEGIN
CREATE TABLE #table3 (id1 int IDENTITY PRIMARY KEY, new_name text);
CREATE TABLE #table4 (id2 int IDENTITY PRIMARY KEY, old_name nvarchar(30));
INSERT #table3 (new_name) SELECT unique_discription FROM inserted;
INSERT #table4 (old_name) SELECT DISTINCT namec FROM deleted;
UPDATE country SET unique_discription = new_name FROM #table3 INNER JOIN #table4 ON #table3.id1 = #table4.id2 WHERE (country.namec = #table4.old_name AND #table3.new_name IS NOT NULL);
DROP TABLE #table3;
DROP TABLE #table4;
END;
IF (UPDATE(namer))
BEGIN
CREATE TABLE #table5 (id1 int IDENTITY PRIMARY KEY, new_name nvarchar(30));
CREATE TABLE #table6 (id2 int IDENTITY PRIMARY KEY, old_name nvarchar(30));
INSERT #table5 (new_name) SELECT DISTINCT namer FROM inserted;
INSERT #table6 (old_name) SELECT DISTINCT namer FROM deleted;
UPDATE ruler SET namer = new_name FROM #table5 INNER JOIN #table6 ON #table5.id1 = #table6.id2 WHERE (ruler.namer = #table6.old_name AND #table5.new_name NOT IN (SELECT namer FROM ruler));
DROP TABLE #table5;
DROP TABLE #table6;
END;
IF (UPDATE(Feature_I))
BEGIN
CREATE TABLE #table7 (id1 int IDENTITY PRIMARY KEY, new_name nvarchar(30));
CREATE TABLE #table8 (id2 int IDENTITY PRIMARY KEY, old_name nvarchar(30));
INSERT #table7 (new_name) SELECT DISTINCT Feature_I FROM inserted;
INSERT #table8 (old_name) SELECT DISTINCT namer FROM deleted;
UPDATE ruler SET Feature_I = new_name FROM #table7 INNER JOIN #table8 ON #table7.id1 = #table8.id2 WHERE (ruler.namer = #table8.old_name AND #table7.new_name IS NOT NULL);
DROP TABLE #table7;
DROP TABLE #table8;
END;
IF (UPDATE(Feature_II))
BEGIN
CREATE TABLE #table9 (id1 int IDENTITY PRIMARY KEY, new_name nvarchar(30));
CREATE TABLE #table0 (id2 int IDENTITY PRIMARY KEY, old_name nvarchar(30));
INSERT #table9 (new_name) SELECT DISTINCT Feature_II FROM inserted;
INSERT #table0 (old_name) SELECT DISTINCT namer FROM deleted;
UPDATE ruler SET Feature_II = new_name FROM #table9 INNER JOIN #table0 ON #table9.id1 = #table0.id2 WHERE (ruler.namer = #table0.old_name AND #table9.new_name IS NOT NULL);
DROP TABLE #table9;
DROP TABLE #table0;
END;
GO
USE Civilizations
GO
DELETE FROM ruler;
DELETE FROM country;
go
DECLARE @id varchar(30);
INSERT country (namec) VALUES ('Rome');
SET @id = SCOPE_IDENTITY();
INSERT ruler (ruler_country, namer) VALUES (@id, 'Caesar');
INSERT ruler (ruler_country, namer) VALUES (@id, 'Octavian');
INSERT country (namec) VALUES ('Greece');
SET @id = SCOPE_IDENTITY();
INSERT ruler (ruler_country, namer) VALUES (@id, 'Pericles');
GO
INSERT Civilization(namec, namer) VALUES ('Egypt', 'Ramses'), ('Egypt', 'Ramses II');
INSERT Civilization(namec, namer) VALUES ('Egypt', 'Ramses II');
INSERT Civilization(namec, namer) VALUES ('Egypt', 'Ramses III'), ('Laplandia', 'Santa'), ('Egypt', 'Ramses V'), ('Egypt', 'Ramses V');
GO
DELETE FROM Civilization WHERE namer = 'Caesar';
DELETE FROM Civilization WHERE namec = 'Egypt';
GO
INSERT Civilization(namec, namer, Feature_II) VALUES ('Greece', 'Caligula', '1');
GO
UPDATE Civilization SET namec = 'Rome II' WHERE namec = 'Rome';
UPDATE Civilization SET namec = 'Honduras' WHERE namer like 'Ca%'
UPDATE Civilization SET namer = 'Caesar II' WHERE namer = 'Santa';
GO
INSERT Civilization(namec, namer, Feature_I, Feature_II) VALUES ('1', '2', '3', '4');
GO
INSERT Civilization(namec, unique_discription, namer) VALUES ('1', '2', '3');
GO
UPDATE Civilization SET unique_discription = '12345 вышел зайчик погулять' WHERE namec = 'Rome II';
GO
SELECT countryid, namec FROM country;
GO
SELECT rulerid, namer, ruler_country FROM ruler;
GO
SELECT * FROM Civilization;
GO