Advertisement
byu

many-to-many script

byu
Sep 22nd, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 1.99 KB | None | 0 0
  1. DROP table RentalItem;
  2. drop table item;
  3. drop table rental;
  4. drop table cust;
  5.  
  6. drop sequence REntityID;
  7. create sequence REntityID start with 1000 increment by 1;
  8.  
  9. create table cust(
  10.    custID int default next value for REntityID primary key,
  11.    fname varchar(50),
  12.    lname varchar(50)
  13. );
  14.  
  15. create table rental(
  16.    rentalID int default next value for REntityID primary key,
  17.    startdate datetime default getdate(),
  18.    custid int not null references cust
  19. );
  20.  
  21. Create table item(
  22.    itemID int default next value for REntityID primary key,
  23.    name varchar(60),
  24.    type varchar(30),
  25.    rentalPrice decimal(5,2), /* this is a comment */
  26. );
  27.  
  28. create table RentalItem(
  29.   rentalID int references rental,
  30.   itemID int references item,
  31.   ActualPrice decimal(5,2) ,
  32.   constraint RentalItem_PK primary key (rentalID, itemid),
  33. );
  34.  
  35. INSERT INTO cust(fname, lname)
  36. values('David','Patten'),
  37.       ('Orson','Pratt'),
  38.       ('Orson','Hyde');
  39.  
  40.  
  41. INSERT INTO item(name, type, rentalPrice)
  42. values('Unicycle1','cycle', 5),
  43.       ('Unicycle2','cycle', 5),
  44.       ('Bicycle1','cycle', 7),
  45.       ('Mountain Bike','cycle', 8),
  46.       ('Snowboard','Arctic', 5),
  47.       ('Helmet1','safety', 1),
  48.       ('Helmet2','safety', 1),
  49.       ('Helmet3','safety', 1),
  50.       ('Helmet4','safety', 1)
  51. ;
  52.  
  53. insert into rental(custid)
  54.   values((select custid from cust where lname='Patten'));
  55.  
  56. insert into rentalitem values(
  57.   (select max(rentalid) from rental),
  58.   (select itemid from item where name = 'Mountain Bike'),
  59.   (select rentalPrice *.95 from item where name = 'Mountain Bike')
  60. );
  61. insert into rentalitem values(
  62.   (select max(rentalid) from rental),
  63.   (select itemid from item where name = 'Helmet1'),
  64.   0
  65. );
  66.  
  67.  
  68. insert into rental(custid)
  69.   values((select custid from cust where lname='Patten'));
  70.  
  71. insert into rentalitem values(
  72.   (select max(rentalid) from rental),
  73.   (select itemid from item where name = 'Mountain Bike'),
  74.   (select rentalPrice *.95 from item where name = 'Mountain Bike')
  75. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement