Sajmon

T-SQL - DAIS - create

Apr 11th, 2012
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 3.02 KB | None | 0 0
  1. CREATE TABLE Reservation
  2.     (
  3.      coachNumber INTEGER NOT NULL ,
  4.      seatNumber INTEGER NOT NULL ,
  5.      reservationDate DATETIME NOT NULL ,
  6.      paidDate DATETIME ,
  7.      canceledDate DATETIME ,
  8.      idTrainRide INTEGER NOT NULL ,
  9.      idUser INTEGER ,
  10.      CONSTRAINT Reservation_PK PRIMARY KEY NONCLUSTERED (coachNumber, seatNumber, idTrainRide)
  11.     )
  12. GO
  13.  
  14. CREATE TABLE Staff
  15.     (
  16.      idStaff INTEGER NOT NULL ,
  17.      fname VARCHAR (30) NOT NULL ,
  18.      lname VARCHAR (30) NOT NULL ,
  19.      position CHAR (1) NOT NULL ,
  20.      jobStart DATETIME NOT NULL ,
  21.      jobEnd DATETIME ,
  22.      salary INTEGER NOT NULL ,
  23.      CONSTRAINT Staff_PK PRIMARY KEY NONCLUSTERED (idStaff)
  24.     )
  25. GO    
  26.  
  27. CREATE TABLE Station
  28.     (
  29.      idStation INTEGER NOT NULL ,
  30.      name VARCHAR (60) NOT NULL ,
  31.      CONSTRAINT Station_PK PRIMARY KEY NONCLUSTERED (idStation)
  32.     )
  33. GO
  34.  
  35. CREATE TABLE Train
  36.     (
  37.      idTrain INTEGER NOT NULL ,
  38.      name VARCHAR (20) NOT NULL ,
  39.      coachCount INTEGER NOT NULL ,
  40.      price FLOAT NOT NULL ,
  41.      startTime TIME NOT NULL ,
  42.      endTime TIME NOT NULL ,
  43.      idStation INTEGER ,
  44.      idStation1 INTEGER ,
  45.      CONSTRAINT Train_PK PRIMARY KEY NONCLUSTERED (idTrain)
  46.     )
  47. GO
  48.  
  49.  
  50. CREATE TABLE TrainCrew
  51.     (
  52.      idStaff INTEGER NOT NULL ,
  53.      idTrainRide INTEGER NOT NULL ,
  54.      CONSTRAINT PK_TrainCrew PRIMARY KEY NONCLUSTERED (idStaff, idTrainRide)
  55.     )
  56. GO
  57.  
  58.  
  59. CREATE TABLE TrainRide
  60.     (
  61.      idTrainRide INTEGER NOT NULL ,
  62.      rideDate DATE NOT NULL ,
  63.      idTrain INTEGER ,
  64.      CONSTRAINT TrainRide_PK PRIMARY KEY NONCLUSTERED (idTrainRide)
  65.     )
  66. GO
  67.  
  68.  
  69. CREATE TABLE [User]
  70.     (
  71.      idUser INTEGER NOT NULL ,
  72.      email VARCHAR (30) NOT NULL ,
  73.      fname VARCHAR (40) ,
  74.      lname VARCHAR (40) ,
  75.      password VARCHAR (12) NOT NULL ,
  76.      CONSTRAINT User_PK PRIMARY KEY NONCLUSTERED (idUser)
  77.     )
  78. GO
  79.  
  80.  
  81. ALTER TABLE TrainCrew
  82.     ADD CONSTRAINT Relation_1 FOREIGN KEY
  83.     (
  84.      idStaff
  85.     )
  86.     REFERENCES Staff
  87.     (
  88.      idStaff
  89.     )
  90. GO
  91.  
  92. ALTER TABLE TrainCrew
  93.     ADD CONSTRAINT Relation_3 FOREIGN KEY
  94.     (
  95.      idTrainRide
  96.     )
  97.     REFERENCES TrainRide
  98.     (
  99.      idTrainRide
  100.     )
  101. GO
  102.  
  103. ALTER TABLE TrainRide
  104.     ADD CONSTRAINT Relation_4 FOREIGN KEY
  105.     (
  106.      idTrain
  107.     )
  108.     REFERENCES Train
  109.     (
  110.      idTrain
  111.     )
  112. GO
  113.  
  114. ALTER TABLE Reservation
  115.     ADD CONSTRAINT Relation_5 FOREIGN KEY
  116.     (
  117.      idTrainRide
  118.     )
  119.     REFERENCES TrainRide
  120.     (
  121.      idTrainRide
  122.     )
  123. GO
  124.  
  125. ALTER TABLE Reservation
  126.     ADD CONSTRAINT Relation_6 FOREIGN KEY
  127.     (
  128.      idUser
  129.     )
  130.     REFERENCES [User]
  131.     (
  132.      idUser
  133.     )
  134. GO
  135.  
  136. ALTER TABLE Train
  137.     ADD CONSTRAINT Relation_7 FOREIGN KEY
  138.     (
  139.      idStation
  140.     )
  141.     REFERENCES Station
  142.     (
  143.      idStation
  144.     )
  145. GO
  146.  
  147. ALTER TABLE Train
  148.     ADD CONSTRAINT Relation_8 FOREIGN KEY
  149.     (
  150.      idStation1
  151.     )
  152.     REFERENCES Station
  153.     (
  154.      idStation
  155.     )
  156. GO
Advertisement
Add Comment
Please, Sign In to add comment