Question

Task for today: 1. Create all the tables for Database company. Focus to the PK and FK 2. Practice INSERT/UPDATE/DELETE. 3. Te
0 0
Add a comment Improve this question Transcribed image text
Answer #1

1
-- Database: `company`
--

-- --------------------------------------------------------

--
-- Table structure for table `department`
--

CREATE TABLE `department` (
`dname` varchar(45) NOT NULL,
`dnumber` int(11) NOT NULL,
`mgr_ssn` int(11) NOT NULL,
`mgr_start_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `dependent`
--

CREATE TABLE `dependent` (
`essn` int(11) NOT NULL,
`dependent_name` varchar(45) NOT NULL,
`sex` varchar(45) NOT NULL,
`bdate` date NOT NULL,
`relationship` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `dept_locations`
--

CREATE TABLE `dept_locations` (
`dnumber` int(11) NOT NULL,
`dlocation` varchar(45) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `employee`
--

CREATE TABLE `employee` (
`fname` varchar(45) NOT NULL,
`minit` varchar(45) NOT NULL,
`lname` varchar(45) NOT NULL,
`ssn` int(11) NOT NULL,
`bdate` date NOT NULL,
`address` varchar(45) NOT NULL,
`sex` varchar(45) NOT NULL,
`salary` int(11) NOT NULL,
`super_ssn` int(11) NOT NULL,
`dno` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `project`
--

CREATE TABLE `project` (
`pname` varchar(45) NOT NULL,
`pnumber` int(11) NOT NULL,
`plocation` varchar(45) NOT NULL,
`dnum` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- --------------------------------------------------------

--
-- Table structure for table `works_on`
--

CREATE TABLE `works_on` (
`essn` int(11) NOT NULL,
`pno` int(11) NOT NULL,
`hours` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `department`
--
ALTER TABLE `department`
ADD PRIMARY KEY (`dnumber`),
ADD KEY `mgr_ssn` (`mgr_ssn`);

--
-- Indexes for table `dependent`
--
ALTER TABLE `dependent`
ADD PRIMARY KEY (`essn`,`dependent_name`);

--
-- Indexes for table `dept_locations`
--
ALTER TABLE `dept_locations`
ADD PRIMARY KEY (`dnumber`,`dlocation`);

--
-- Indexes for table `employee`
--
ALTER TABLE `employee`
ADD PRIMARY KEY (`ssn`),
ADD KEY `super_ssn` (`super_ssn`);

--
-- Indexes for table `project`
--
ALTER TABLE `project`
ADD PRIMARY KEY (`pnumber`),
ADD KEY `dnum` (`dnum`);

--
-- Indexes for table `works_on`
--
ALTER TABLE `works_on`
ADD PRIMARY KEY (`essn`,`pno`),
ADD KEY `pno` (`pno`);

--
-- Constraints for dumped tables
--

--
-- Constraints for table `department`
--
ALTER TABLE `department`
ADD CONSTRAINT `mgr_ssn` FOREIGN KEY (`mgr_ssn`) REFERENCES `employee` (`ssn`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `dependent`
--
ALTER TABLE `dependent`
ADD CONSTRAINT `fk_essn` FOREIGN KEY (`essn`) REFERENCES `employee` (`ssn`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `dept_locations`
--
ALTER TABLE `dept_locations`
ADD CONSTRAINT `dnumber` FOREIGN KEY (`dnumber`) REFERENCES `department` (`dnumber`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `employee`
--
ALTER TABLE `employee`
ADD CONSTRAINT `super_ssn` FOREIGN KEY (`super_ssn`) REFERENCES `employee` (`ssn`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `project`
--
ALTER TABLE `project`
ADD CONSTRAINT `dnum` FOREIGN KEY (`dnum`) REFERENCES `department` (`dnumber`) ON DELETE NO ACTION ON UPDATE NO ACTION;

--
-- Constraints for table `works_on`
--
ALTER TABLE `works_on`
ADD CONSTRAINT `essn` FOREIGN KEY (`essn`) REFERENCES `employee` (`ssn`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `pno` FOREIGN KEY (`pno`) REFERENCES `project` (`pnumber`) ON DELETE NO ACTION ON UPDATE NO ACTION;
COMMIT;

✓ O host/phpmyadmin/db_structure.php?server=1&db=company - Server: localhost » Database: company Structure SQL Search query E

########################################################

Give a ThumbsUp if you found this answer Helpful :)
#######################################################

In the Case of multiple questions,we(Experts) should answer only certain number of sub-parts\questions.So kindly ask the remaining sub-parts\questions as a new query. Sorry for the inconvenience caused.

################################################################
............ Still having trouble understanding the answer ? Feel free to ask in the comment section.......

Add a comment
Know the answer?
Add Answer to:
Task for today: 1. Create all the tables for Database company. Focus to the PK and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT