0% found this document useful (0 votes)
2 views4 pages

Database Refactoring

The document outlines the process of refactoring a database schema by creating a new 'Unit' table to store unique unit information extracted from the 'Student_UnitCode' table. It details the steps taken, including inserting data, dropping a column, and adding a foreign key constraint. Two queries are provided to display the contents of both tables after the operations are performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Database Refactoring

The document outlines the process of refactoring a database schema by creating a new 'Unit' table to store unique unit information extracted from the 'Student_UnitCode' table. It details the steps taken, including inserting data, dropping a column, and adding a foreign key constraint. Two queries are provided to display the contents of both tables after the operations are performed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

**Schema (MySQL v5.

7)**

-- Create the original table

CREATE TABLE Student_UnitCode (

StudentId VARCHAR(10),

UnitCode VARCHAR(10),

UnitName VARCHAR(100)

);

-- Insert original data

INSERT INTO Student_UnitCode (StudentId, UnitCode, UnitName) VALUES

('0023765', 'UG45783', 'Advance Database'),

('0023765', 'UG45832', 'Network Systems'),

('0023765', 'UG45734', 'Multi-User Operating Systems'),

('0035643', 'UG45832', 'Network Systems'),

('0035643', 'UG45951', 'Project'),

('0061234', 'UG45783', 'Advance Database');

-- Step 1: Create Unit table

CREATE TABLE Unit (

UnitCode VARCHAR(10) PRIMARY KEY,

UnitName VARCHAR(100)

);

-- Step 2: Populate Unit table

INSERT INTO Unit (UnitCode, UnitName)

SELECT DISTINCT UnitCode, UnitName


FROM Student_UnitCode;

-- Step 3: Remove UnitName from original table

ALTER TABLE Student_UnitCode

DROP COLUMN UnitName;

-- Step 4: Add foreign key

ALTER TABLE Student_UnitCode

ADD FOREIGN KEY (UnitCode) REFERENCES Unit(UnitCode);

---

**Query #1**

-- Output: Select from both tables

SELECT * FROM Student_UnitCode;

| StudentId | UnitCode |

| --------- | -------- |

| 0023765 | UG45783 |

| 0023765 | UG45832 |

| 0023765 | UG45734 |

| 0035643 | UG45832 |

| 0035643 | UG45951 |
| 0061234 | UG45783 |

---

**Query #2**

SELECT * FROM Unit;

| UnitCode | UnitName |

| -------- | ---------------------------- |

| UG45734 | Multi-User Operating Systems |

| UG45783 | Advance Database |

| UG45832 | Network Systems |

| UG45951 | Project |

---

Query 1

Query 2
Operations performed

Created a new table to accommodate refactored data

Extracted all unique UnitCode and UnitName from the source table

Removed UnitName from the source table

Added foreign key for referential purposes

You might also like