andreybotanic

DDL

Mar 5th, 2017
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 0.87 KB | None | 0 0
  1. USE tempdb;
  2.  
  3. IF OBJECT_ID('FK_Student_GroupStudent', 'F')IS NOT NULL
  4.   ALTER TABLE Student DROP CONSTRAINT FK_Student_GroupStudent;
  5. IF OBJECT_ID('GroupStudent', 'U')IS NOT NULL
  6.   DROP TABLE GroupStudent;
  7. IF OBJECT_ID('Student', 'U')IS NOT NULL
  8.   DROP TABLE Student;
  9. IF OBJECT_ID('Score', 'U')IS NOT NULL
  10.   DROP TABLE Score;
  11.  
  12. CREATE TABLE GroupStudent (
  13.   Id INT IDENTITY(1, 1),
  14.   Number VARCHAR(11) NOT NULL
  15. )
  16.  
  17. CREATE TABLE Student (
  18.   Id INT IDENTITY(1, 1),
  19.   GroupId INT NOT NULL,
  20.   FIO NVARCHAR(1024) NOT NULL
  21. )
  22.  
  23. ALTER TABLE GroupStudent ADD
  24.     CONSTRAINT PK_GroupStudent PRIMARY KEY  CLUSTERED
  25.     (
  26.         id
  27.     )  ON [PRIMARY]
  28.  
  29. ALTER TABLE Student ADD
  30.     CONSTRAINT FK_Student_GroupStudent FOREIGN KEY
  31.     (
  32.         GroupId
  33.     ) REFERENCES GroupStudent (
  34.         Id
  35.     )
  36.  
  37. CREATE TABLE Score (
  38.   StudentId INT NOT NULL,
  39.   date DATE NOT NULL,
  40.   Score TINYINT NOT NULL
  41. )
Advertisement
Add Comment
Please, Sign In to add comment