akevintg

SQLearning(CREATE, CONSTRAINT, ALTER, etc)

Mar 24th, 2015
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.71 KB | None | 0 0
  1. /*
  2. CREATE DATABASE Test03
  3. GO
  4. USE Test03
  5. */
  6.  
  7. /*
  8. 1. Create tables with the following descriptions :
  9. (create table, like,  in)
  10. Table’s Name  : MsStaff
  11. Primary Key : StaffId
  12.  
  13. StaffID CHAR    5   Cannot be empty, must be started with ‘SF’ and followed by 3 characters of number. Example: SF001
  14. StaffName   VARCHAR 20  Cannot be empty
  15. StaffBOD    DATE    -   -
  16. StaffGender VARCHAR 10  Must be ‘Male’ or ‘Female’
  17. StaffAddress    VARCHAR 100 -
  18. StaffEmail  VARCHAR 20  Format [email protected]
  19. StaffSalary INT -   -
  20. */
  21.  
  22. CREATE TABLE MsStaff(
  23.     StaffID CHAR(5) NOT NULL PRIMARY KEY,
  24.     StaffName VARCHAR(20) NOT NULL,
  25.     StaffBOD DATE,
  26.     StaffGender VARCHAR(10),
  27.     StaffAddress VARCHAR(100),
  28.     StaffEmail VARCHAR(40),
  29.     StaffSalary INT,
  30.     CONSTRAINT MsStaffValidation CHECK(
  31.         StaffID LIKE 'SF[0-9][0-9][0-9]' AND
  32.         (StaffGender LIKE 'MALE' OR StaffGender LIKE 'FEMALE') AND
  33.         StaffEmail LIKE'%@%.%'
  34.     )  
  35. )
  36.  
  37.  
  38. /*
  39. Table’s Name  : MsPaymentType
  40. Primary Key : PaymentTypeId
  41. Field Name  Data Type   Length  Description
  42. PaymentTypeId   CHAR    5   Cannot be empty, must be started with ‘PT’ and followed by 3 characters of number.
  43. Example: PT001
  44. PaymentTypeName VARCHAR 20  Cannot be empty
  45. */
  46.  
  47. CREATE TABLE MsPaymentType(
  48.     PaymentTypeID CHAR(5) NOT NULL PRIMARY KEY,
  49.     PaymentTypeName VARCHAR(20) NOT NULL,
  50.     CONSTRAINT MsPaymentTypeValidation CHECK(
  51.         PaymentTypeID LIKE 'PT[0-9][0-9][0-9]'
  52.     )
  53. )
  54.  
  55. /*
  56. Table’s Name  : MsCity
  57. Primary Key : CityId
  58. Field Name  Data Type   Length  Description
  59. CityId  CHAR    5   Cannot be empty, must be started with ‘CT’ and followed by 3 characters of number.
  60. Example: CT001
  61. CityName    VARCHAR 20  Cannot be empty
  62. */
  63.  
  64. CREATE TABLE MsCity(
  65.     CityID CHAR(5) NOT NULL PRIMARY KEY,
  66.     CityName VARCHAR(20) NOT NULL,
  67.     CONSTRAINT MsCityValidation CHECK (
  68.         CityID LIKE 'CT[0-9][0-9][0-9]'
  69.     )
  70. )
  71.  
  72. /*
  73. Table’s Name  : MsDestination
  74. Primary Key     : DestinationId
  75. Field Name  Data Type   Length  Description
  76. DestinationId   CHAR    5   Cannot be empty, must be started with ‘DT’ and followed by 3 characters of number.
  77. Example: DT001
  78. CityFromId  CHAR    5   Cannot be empty, references from MsCity.
  79. CityToId    CHAR    5   Cannot be empty, references from MsCity.
  80. Price   NUMERIC 11, 2   -
  81. */
  82.  
  83. CREATE TABLE MsDestination(
  84.     DestinationID CHAR(5) NOT NULL PRIMARY KEY,
  85.     CityFromID CHAR(5) REFERENCES MsCity ON UPDATE NO ACTION ON DELETE NO ACTION ,
  86.     CityToID CHAR(5) REFERENCES MsCity ON UPDATE NO ACTION ON DELETE NO ACTION ,
  87.     PRICE NUMERIC(11,2),
  88.     CONSTRAINT MsDetinationValidation CHECK(
  89.         DestinationID LIKE 'DT[0-9][0-9][0-9]'
  90.     )
  91. )
  92.  
  93. /*
  94. Table’s Name  : MsShipmentType
  95. Primary Key : ShipmentTypeId
  96. Field Name  Data Type   Length  Description
  97. ShipmentTypeId  CHAR    5   Cannot be empty, must be started with ‘ST’ and followed by 3 characters of number.
  98. Example: ST001
  99. ShipmentTypeName    VARCHAR 10  Cannot be empty
  100. ShipmentCharge  INT 5   -
  101. */
  102.  
  103. CREATE TABLE MsShipmentType(
  104.     ShipmentTypeID CHAR(5) NOT NULL PRIMARY KEY,
  105.     ShipmentTypeName VARCHAR(10) NOT NULL,
  106.     ShipmentCharge INT,
  107.     CONSTRAINT MsShipmentTypeValidation CHECK (
  108.         ShipmentTypeID LIKE 'ST[0-9][0-9][0-9]'
  109.     )
  110. )
  111.  
  112. /*
  113. 2.  Create a table with the following descriptions :
  114. Table’s Name  : HeaderShipment
  115. Field Name  Data Type   Length  Description
  116. ShipmentID  CHAR    5   Cannot be empty, must be started with ‘SH’ and followed by 3 characters of number.
  117. Example: SH001
  118. StaffID CHAR    5   Cannot be empty, references from MsStaff and if the referenced data is changed then the StaffID data in HeaderShipment will also be changed.
  119. DestinationID   CHAR    5   Cannot be empty, references from MsDestination and if the referenced data is changed then the DestinationID data in the HeaderShipment will also be changed.
  120. PaymentTypeID   CHAR    5   Cannot be empty, references from MsPaymentType and if the referenced data is changed then the PaymentTypeID data in HeaderShipment will also be changed.
  121. ShipmentTypeID  CHAR    5   Cannot be empty, references from MsShipmentType and if the referenced data is changed then the ShipmentTypeID data in HeaderShipment will also be changed.
  122. ReceiverName    VARCHAR 20  -
  123. ReceiverAddress VARCHAR 100 -
  124. Weight  NUMERIC 11,2    -
  125. ShipmentDate    DATE    -   -
  126.  
  127. Then, with alter syntax, change the ShipementID field to become primary key on that table.
  128. (create table, alter table, primary key)
  129. */
  130.  
  131. CREATE TABLE HeaderShipment(
  132.     ShipmentID CHAR(5) NOT NULL,
  133.     StaffID CHAR(5) NOT NULL REFERENCES MsStaff ON DELETE CASCADE ON UPDATE CASCADE,
  134.     DestinationID CHAR(5) NOT NULL REFERENCES MsDestination ON UPDATE CASCADE ON DELETE CASCADE,
  135.     PaymentTypeID CHAR(5) NOT NULL REFERENCES MsPaymentType ON UPDATE CASCADE ON DELETE CASCADE,
  136.     ShipmentTypeID CHAR(5) NOT NULL REFERENCES MsShipmentType ON UPDATE CASCADE ON DELETE CASCADE,
  137.     ReceiverName VARCHAR(20),
  138.     ReceiverAddress VARCHAR(100),
  139.     [Weight] NUMERIC(11,2),
  140.     ShipmentDate DATE,
  141.     CONSTRAINT HeaderShipmentValidation CHECK(
  142.         ShipmentID LIKE 'SH[0-9][0-9][0-9]'
  143.     )
  144. )
  145.  
  146. ALTER TABLE HeaderShipment ADD  PRIMARY KEY(ShipmentID)
  147.  
  148. /*
  149. 3.  Add a constraint for MsStaff table to validate that staff’s name must be between 5 and 20 characters
  150. and the previous data is not necessary to be checked. Then, delete the constraint.
  151. (alter table, with nocheck, add constraint, drop)
  152. */
  153.  
  154. ALTER TABLE MsStaff WITH NOCHECK
  155. ADD CONSTRAINT MsStaffValidasi2 CHECK(
  156.     LEN(StaffName) BETWEEN 5 AND 20
  157. )
  158.  
  159. ALTER TABLE MsStaff DROP CONSTRAINT MsStaffValidasi2
  160.  
  161. /*
  162. 4.  Add ‘Description’ as a new column on MsShipmentType table with varchar(100) data type. Then, delete
  163. the column.
  164. (alter table, add, drop column)
  165. */
  166.  
  167. ALTER TABLE MsShipmentType ADD [Description] VARCHAR(100)
  168. ALTER TABLE MsShipmentType DROP COLUMN [Description]
  169.  
  170. SELECT * FROM MsStaff
  171. SELECT * FROM MsCity
  172. SELECT * FROM MsDestination
  173. SELECT * FROM MsPaymentType
  174. SELECT * FROM MsShipmentType
  175. SELECT * FROM HeaderShipment
Advertisement
Add Comment
Please, Sign In to add comment