Advertisement
trishLEX

BD.Lab6

Nov 2nd, 2017
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.64 KB | None | 0 0
  1. USE master
  2. GO
  3.  
  4. IF DB_ID (N'Shop') IS NOT NULL
  5.     DROP DATABASE Shop;
  6. GO
  7.  
  8. CREATE DATABASE Shop
  9.     ON (
  10.         name = Shop_dat,
  11.         filename = 'E:\Sorry\DBProjects\Lab6\Shop_dat.mdf',
  12.         size = 10,
  13.         maxsize = unlimited,
  14.         filegrowth = 5%
  15.         )
  16.     LOG ON (
  17.         name = Shop_log,
  18.         filename = 'E:\Sorry\DBProjects\Lab6\Shop_log.ldf',
  19.         size = 5,
  20.         maxsize = 25,
  21.         filegrowth = 5
  22.         ) ;
  23. GO
  24.  
  25. USE Shop
  26. GO
  27.  
  28. IF SCHEMA_ID(N'ShopSchema') IS NOT NULL
  29.     DROP SCHEMA ShopSchema;
  30.     GO
  31.  
  32. CREATE SCHEMA ShopSchema;
  33.  GO
  34.  
  35. CREATE TABLE Shop.ShopSchema.[shop]
  36. (
  37.     shopCode INT PRIMARY KEY NOT NULL IDENTITY(0, 1) ,
  38.     shopName VARCHAR(25) ,
  39.     isOutlet BIT DEFAULT 0 NOT NULL ,
  40.     address  VARCHAR(25)   NOT NULL
  41. )
  42.  GO
  43.  
  44. CREATE TABLE Shop.ShopSchema.shopMan
  45. (
  46.     shopmanCode UNIQUEIDENTIFIER PRIMARY KEY ROWGUIDCOL DEFAULT NEWID() NOT NULL,
  47.     firstName VARCHAR(10) NOT NULL,
  48.     lastName VARCHAR(10) NOT NULL,
  49.     middleName VARCHAR(10),
  50.     dateOfBirth DATE NOT NULL,
  51.     phone CHAR(11) NOT NULL,
  52.     position VARCHAR(15),
  53.     isFired BIT DEFAULT 0,
  54.  
  55.     shopCode INT NOT NULL,
  56.     FOREIGN KEY (shopCode) REFERENCES shop.ShopSchema.[shop](shopCode)
  57. )
  58.   GO
  59.  
  60. CREATE SEQUENCE [CountBy1]
  61.   START WITH 1
  62.   INCREMENT BY 1;
  63.  GO
  64.  
  65. CREATE TABLE Shop.ShopSchema.[check]
  66. (
  67.     checkID INT PRIMARY KEY NOT NULL,
  68.     date DATE NOT NULL,
  69.     totalCost MONEY NOT NULL,
  70.     typeOfPay BIT NOT NULL,
  71.     discount SMALLINT,
  72.     shopmanCode UNIQUEIDENTIFIER NOT NULL,
  73.     FOREIGN KEY (shopmanCode) REFERENCES shopMan (shopmanCode)
  74. )
  75.   GO
  76.  
  77. DECLARE @i INT
  78. SET @i = 0
  79.  
  80. WHILE @i < 3
  81.   BEGIN
  82. DECLARE @lastIdent INT
  83.  
  84. INSERT Shop.ShopSchema.[shop] (shopName, isOutlet, address)
  85.   VALUES ('MAVI Аутлет Белая Дача', 1, 'Новорязанское ш., 8')
  86.  
  87. SET @lastIdent = @@IDENTITY
  88.  
  89. DECLARE @shopmanCode UNIQUEIDENTIFIER
  90. SET @shopmanCode = newid()
  91.  
  92. INSERT Shop.ShopSchema.[shopMan] (shopmanCode, firstName, lastName, middleName, dateOfBirth, phone, position, shopCode)
  93.   VALUES (@shopmanCode, 'Иван', 'Иванович', NULL, '1997-01-01', '89161111111', 'Кассир', @lastIdent)
  94.  
  95. INSERT Shop.ShopSchema.[check] (checkID, date, totalCost, typeOfPay, discount, shopmanCode)
  96.   VALUES (NEXT VALUE FOR CountBy1, '2017-11-02', 1000, 0, 0, @shopmanCode)
  97.  
  98.   SET @i = @i + 1
  99.   END
  100. GO
  101.  
  102. -------------
  103.  
  104. ALTER TABLE Shop.ShopSchema.[check]
  105.     DROP FK__check__shopmanCo__35BCFE0A
  106.  
  107. ALTER TABLE Shop.ShopSchema.[check]
  108.     ADD CONSTRAINT FK__check__shopmanCo__35BCFE0A FOREIGN KEY (shopmanCode) REFERENCES Shop.ShopSchema.[shopMan](shopmanCode) ON DELETE NO ACTION
  109.  
  110. DELETE FROM Shop.ShopSchema.[check] WHERE checkID < 4
  111. GO
  112.  
  113. --------------
  114.  
  115. ALTER TABLE Shop.ShopSchema.[check]
  116.     DROP FK__check__shopmanCo__35BCFE0A
  117.  
  118. ALTER TABLE Shop.ShopSchema.[check]
  119.     ADD CONSTRAINT FK__check__shopmanCo__35BCFE0A FOREIGN KEY (shopmanCode) REFERENCES Shop.ShopSchema.[shopMan](shopmanCode) ON DELETE CASCADE
  120.  
  121. DELETE FROM Shop.ShopSchema.[shopMan] WHERE shopCode = 5
  122. GO
  123.  
  124. --------------
  125.  
  126. ALTER TABLE Shop.ShopSchema.[check]
  127.     DROP FK__check__shopmanCo__35BCFE0A
  128.  
  129. ALTER TABLE Shop.ShopSchema.[check]
  130.     ADD CONSTRAINT FK__check__shopmanCo__35BCFE0A FOREIGN KEY (shopmanCode) REFERENCES Shop.ShopSchema.[shopMan](shopmanCode) ON DELETE SET NULL
  131.  
  132. DELETE FROM Shop.ShopSchema.[shopMan] WHERE shopCode = 6
  133. GO
  134.  
  135. ---------------
  136.  
  137. ALTER TABLE Shop.ShopSchema.[check]
  138.     DROP FK__check__shopmanCo__35BCFE0A
  139.  
  140. ALTER TABLE Shop.ShopSchema.[check]
  141.     ADD CONSTRAINT FK__check__shopmanCo__35BCFE0A FOREIGN KEY (shopmanCode) REFERENCES Shop.ShopSchema.[shopMan](shopmanCode) ON DELETE SET DEFAULT
  142.  
  143. DELETE FROM Shop.ShopSchema.[shopMan] WHERE shopCode = 7
  144. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement