Advertisement
Shailrshah

Company Database

Aug 18th, 2014
421
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.99 KB | None | 0 0
  1. mysql mysql -u root -p
  2. CREATE DATABASE WORK;
  3. USE WORK;
  4. CREATE TABLE employee(
  5.     fname           VARCHAR(15)     NOT NULL,
  6.     minit           CHAR,
  7.     lname           VARCHAR(15)     NOT NULL,
  8.     ssn         CHAR(9)         NOT NULL,
  9.     bdate           DATE,
  10.     address         VARCHAR(30),
  11.     sex         CHAR,
  12.     salary          DECIMAL(10, 2),
  13.     super_ssn       CHAR(9),
  14.     dno         INT         NOT NULL,
  15.     PRIMARY KEY(ssn)
  16. );
  17.  
  18. CREATE TABLE department(
  19.     dname           VARCHAR(15)         NOT NULL,
  20.     dnumber         INT             NOT NULL,
  21.     mgr_ssn         CHAR(9)         NOT NULL,
  22.     mgr_start_date  DATE,
  23.     UNIQUE(dname),
  24.     PRIMARY KEY(dnumber),
  25.     FOREIGN KEY(mgr_ssn) REFERENCES employee(ssn)
  26. );
  27.  
  28. CREATE TABLE dept_locations(
  29.     dnumber         INT             NOT NULL,
  30.     dlocation       VARCHAR(15)         NOT NULL,
  31.     PRIMARY KEY(dnumber, dlocation),
  32.     FOREIGN KEY(dnumber) REFERENCES department(dnumber)
  33. );
  34.  
  35. CREATE TABLE project(
  36.     pname           VARCHAR(15)     NOT NULL    PRIMARY KEY,
  37.     pnumber         INT             NOT NULL    UNIQUE,
  38.     plocation       VARCHAR(15),
  39.     dnum            INT             NOT NULL,
  40.     FOREIGN KEY(dnum) REFERENCES department(dnumber)
  41. );
  42.  
  43. CREATE TABLE works_on(
  44.     essn            CHAR(9)         NOT NULL,
  45.     pno             INT             NOT NULL,
  46.     hours           DECIMAL(3,1)        NOT NULL,
  47.     PRIMARY KEY(essn, pno),
  48.     FOREIGN KEY(essn) REFERENCES employee(ssn),
  49.     FOREIGN KEY(pno) REFERENCES project(pnumber)
  50. );
  51.  
  52. CREATE TABLE dependent(
  53.     essn            CHAR(9)         NOT NULL,
  54.     dependent_name      VARCHAR(15)         NOT NULL,
  55.     sex             CHAR,
  56.     bdate           DATE,
  57.     relationship        VARCHAR(8),
  58.     PRIMARY KEY(essn, dependent_name),
  59.     FOREIGN KEY(essn) REFERENCES employee(ssn)
  60. );
  61.  
  62. INSERT INTO employee(fname, minit, lname, ssn, bdate, address, sex, salary, super_ssn, dno)
  63. VALUES
  64. ("John", "B", "Smith", 123456789, "1965-01-09", "731 Fonden, Houston, TX", "M", 30000, 333445555, 5),
  65. ("Franklin", "T", "Wong", 333445555, "1955-12-08", "638 Voss, Houston, TX", "M", 40000, 888665555, 5),
  66. ("Alicia", "J", "Zelaya", 999887777, "1968-01-19", "3321 Castle, Spring, TX", "F", 25000, 987654321, 4),
  67. ("Jennifer", "S", "Wallace", 987654321, "1941-06-20", "291 Berry, Bellaire, TX", "F", 43000, 888665555, 4),
  68. ("Ramesh", "K", "Narayan", 666884444, "1962-09-15", "975 Fire Oak, Humble, TX", "M", 38000, 333445555, 5),
  69. ("Joyce", "A", "English", 453453453, "1972-07-31", "5631 Rice, Houston, TX", "M", 25000, 333445555, 5),
  70. ("Ahmad", "V", "Jabbar", 987987987, "1969-03-29", "980 Dallas, Houston, TX", "M", 25000, 987654321, 4),
  71. ("James", "E", "Borg", 888665555, "1937-11-10", "450 Stone, Houston, TX", "M", 55000, NULL, 1);
  72.  
  73. INSERT INTO department(dname, dnumber, mgr_ssn, mgr_start_date)
  74. VALUES
  75. ("Research", 5, 333445555, "1988-05-22"),
  76. ("Administration", 4, 987654321, "1995-01-01"),
  77. ("Headquarters", 1, 888665555, "1981-06-19");
  78.  
  79. ALTER TABLE employee ADD FOREIGN KEY(super_ssn) REFERENCES employee(ssn);
  80. ALTER TABLE employee ADD FOREIGN KEY(dno) REFERENCES department(dnumber);
  81.  
  82. INSERT INTO dept_locations(dnumber, dlocation)
  83. VALUES
  84. (1, "Houston"),
  85. (4, "Stafford"),
  86. (5, "Bellaire"),
  87. (5, "Sugarland"),
  88. (5, "Houston");
  89.  
  90. INSERT INTO project(pname, pnumber, plocation, dnum)
  91. VALUES
  92. ("ProductX", 1, "Bellaire", 5),
  93. ("ProductY", 2, "Sugarland", 5),
  94. ("ProductZ", 3, "Houston", 5),
  95. ("Computerization", 10, "Stafford", 4),
  96. ("Reorganization", 20, "Houston", 1),
  97. ("Newbenefits", 30, "Stafford", 4);
  98.  
  99. INSERT INTO works_on(essn, pno, hours)
  100. VALUES
  101. (123456789, 1, 32.5),
  102. (123456789, 2, 7.5),
  103. (666884444, 3, 40.0),
  104. (453453453, 1, 20.0),
  105. (453453453, 2, 20.0),
  106. (333445555, 2, 10.0),
  107. (333445555, 3, 10.0),
  108. (333445555, 10, 10.0),
  109. (333445555, 20, 10.0),
  110. (999887777, 30, 30.0),
  111. (999887777, 10, 10.0),
  112. (987987987, 10, 35.0),
  113. (987987987, 30, 5.0),
  114. (987654321, 30, 20.0),
  115. (987654321, 20, 15.0),
  116. (888665555, 20, NULL);
  117.  
  118. INSERT INTO dependent(essn, dependent_name, sex, bdate, relationship)
  119. VALUES
  120. (333445555, "Alice", "F", "1986-04-05", "Daughter"),
  121. (333445555, "Theodore", "M", "1983-10-25", "Son"),
  122. (333445555, "Joy", "F", "1958-05-03", "Spouse"),
  123. (987654321, "Abner", "M", "1942-02-28", "Spouse"),
  124. (123456789, "Michael", "M", "1988-01-04", "Son"),
  125. (123456789, "Alice", "F", "1988-12-30", "Daughter"),
  126. (123456789, "Elizabeth", "F", "1967-05-05", "Spouse");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement