0% found this document useful (0 votes)
21 views

Assignment 2

1. The document describes creating and modifying database tables. It created 14 tables with primary keys and foreign keys. 2. It renamed the "Provider" table to "Supplier" and renamed the "country_name" column to "country". 3. Data types were changed and columns like "min_salary" and "max_salary" were dropped from tables.
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)
21 views

Assignment 2

1. The document describes creating and modifying database tables. It created 14 tables with primary keys and foreign keys. 2. It renamed the "Provider" table to "Supplier" and renamed the "country_name" column to "country". 3. Data types were changed and columns like "min_salary" and "max_salary" were dropped from tables.
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/ 9

1,2) Created all 14 tables already with primary key and made them all NOT NULL:

3) Added foreign keys:


4) SET NULL/NOT NULL/UNIQUE CONSTRAINT:

5) Renamed Provider to Supplier table

6) Renamed country_name column to country:


7) Changed data type from text to varchar(20):

8) Dropped min_salary and max_salary columns:


Code:

1.2.

CREATE TABLE Coutry (

country_id integer PRIMARY KEY,

country_name varchar(30) NOT NULL

);

CREATE TABLE City (

city_id integer PRIMARY KEY,

country_id integer NOT NULL,

city varchar(30) NOT NULL

);

CREATE TABLE Department (

dep_id integer PRIMARY KEY,

dep_name varchar(30) NOT NULL,

location_id integer NOT NULL

);

CREATE TABLE Jobs (

job_id integer PRIMARY KEY,

job_title varchar(30) NOT NULL,

min_salary numeric(8,2) NOT NULL,

max_salary numeric(8,2) NOT NULL

);

CREATE TABLE Employee (

emp_id SERIAL PRIMARY KEY,

emp_fname varchar(30) NOT NULL,


emp_lname varchar(30) NOT NULL,

email varchar(30) NOT NULL,

phone varchar(30) NOT NUll,

hire_date date NOT NULL,

job_id integer NOT NULL,

manager_id integer NOT NULL,

dep_id integer NOT NULL,

salary numeric,

birth_date date,

gender char(6)

);

CREATE TABLE Location (

location_id integer PRIMARY KEY,

city_id integer NOT NULL,

street_address varchar(60) NOT NULL,

postal_code varchar(20) NOT NULL

);

CREATE TABLE Customer (

cust_id integer PRIMARY KEY,

cust_fname varchar(30) NOT NULL,

cust_lname varchar(30) NOT NULL,

email varchar(30) NOT NULL,

gender char(6) NOT NULL,

phone varchar(30) NOT NULL,

location_id integer NOT NULL

);

CREATE TABLE work_on (

emp_id integer PRIMARY KEY,

proj_id integer NOT NULL,

hours_worked numeric(8,2)
);

CREATE TABLE project(

proj_id integer PRIMARY KEY,

proj_name text NOT NULL,

start_date date NOT NULL,

end_date date NOT NULL,

budget numeric(8)

);

CREATE TABLE Ord(

order_id SERIAL PRIMARY KEY,

cust_id integer NOT NULL,

emp_id integer NOT NULL,

order_date date NOT NULL,

shipping_date date NOT NULL

);

CREATE TABLE Product_in_order(

id SERIAL PRIMARY KEY,

product_id integer NOT NULL,

order_id integer NOT NULL,

sup_id integer NOT NULL,

qty_ordered integer NOT NULL,

discount integer NOT NULL

);

CREATE TABLE Product(

product_id SERIAL PRIMARY KEY,

category_id integer NOT NULL,


product_name varchar(30) NOT NULL,

product_price numeric(2,8)

);

CREATE TABLE Category(

category_id integer PRIMARY KEY,

category_name varchar(20) NOT NULL

);

CREATE TABLE Provider(

sup_id integer PRIMARY KEY,

name varchar(30) NOT NULL,

email varchar(30) NOT NULL,

phone varchar(30) NOT NULL,

location_id integer NOT NULL

);

3.

ALTER TABLE City

ADD FOREIGN KEY (country_id) REFERENCES Country(country_id);

ALTER TABLE Location

ADD FOREIGN KEY (city_id) REFERENCES City(city_id);

ALTER TABLE Department

ADD FOREIGN KEY (location_id) REFERENCES Location(location_id);

ALTER TABLE Customer

ADD FOREIGN KEY (location_id) REFERENCES Location(location_id);

ALTER TABLE Provider

ADD FOREIGN KEY (location_id) REFERENCES Location(location_id);

ALTER TABLE Employee

ADD FOREIGN KEY (dep_id) REFERENCES Department(dep_id);

ALTER TABLE Employee

ADD FOREIGN KEY (job_id) REFERENCES Jobs(job_id);

ALTER TABLE work_on


ADD FOREIGN KEY (emp_id) REFERENCES Employee(emp_id);

ALTER TABLE Ord

ADD FOREIGN KEY (emp_id) REFERENCES Employee(emp_id);

ALTER TABLE work_on

ADD FOREIGN KEY (proj_id) REFERENCES Project(proj_id);

ALTER TABLE product_in_order

ADD FOREIGN KEY (order_id) REFERENCES Ord(order_id);

ALTER TABLE product_in_order

ADD FOREIGN KEY (sup_id) REFERENCES Provider(sup_id);

ALTER TABLE product_in_order

ADD FOREIGN KEY (product_id) REFERENCES Product(product_id);

ALTER TABLE Product

ADD FOREIGN KEY (category_id) REFERENCES Category(category_id);

4.

ALTER TABLE Category ADD CONSTRAINT category_uniques UNIQUE (category_id, category_name);

ALTER TABLE City ADD CONSTRAINT city_uniques UNIQUE (city_id, city_name);

ALTER TABLE Country ADD CONSTRAINT country_uniques UNIQUE (country_id, country_name);

ALTER TABLE Customer ADD CONSTRAINT customer_uniques UNIQUE (cust_id, email, phone);

ALTER TABLE Department ADD CONSTRAINT dep_uniques UNIQUE (dep_id, dep_name);

ALTER TABLE Employee ADD CONSTRAINT emp_uniques UNIQUE (emp_id, email, phone);

ALTER TABLE Jobs ADD CONSTRAINT job_uniques UNIQUE (job_id, job_title);

ALTER TABLE Location ADD CONSTRAINT location_uniques UNIQUE (location_id);

ALTER TABLE Ord ADD CONSTRAINT order_uniques UNIQUE (order_id);

ALTER TABLE Product ADD CONSTRAINT product_uniques UNIQUE (product_id, product_name);

ALTER TABLE Provider ADD CONSTRAINT provider_uniques UNIQUE (sup_id, name, email, phone);

ALTER TABLE work_on ALTER COLUMN hours_worked DROP NOT NULL;

ALTER TABLE Project ALTER COLUMN end_date DROP NOT NULL;

ALTER TABLE Ord ALTER COLUMN shipping_date DROP NOT NULL;

ALTER TABLE product_in_order ALTER COLUMN discount DROP NOT NULL;

5.

ALTER TABLE Provider RENAME TO Supplier;

6.
ALTER TABLE Country RENAME COLUMN country_name TO country;

7.

ALTER TABLE Project

ALTER COLUMN proj_name TYPE varchar(20);

8.

ALTER TABLE Jobs DROP COLUMN min_salary;

ALTER TABLE Jobs DROP COLUMN max_salary;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy