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

Dbms 1

The document describes the creation of database tables to model a banking scenario. The tables created are: branch, account, customer, depositor, loan, and borrower. Various operations are then performed on the tables like adding columns, primary keys, foreign keys, and inserting sample data. Finally, tables are created to model an employee payroll scenario.

Uploaded by

JESSEMAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Dbms 1

The document describes the creation of database tables to model a banking scenario. The tables created are: branch, account, customer, depositor, loan, and borrower. Various operations are then performed on the tables like adding columns, primary keys, foreign keys, and inserting sample data. Finally, tables are created to model an employee payroll scenario.

Uploaded by

JESSEMAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

NAME: Jesseman Devamirtham N

REG.NO: 22BRS1112

1. Create and describe the following tables:


A) NAME: branch
FIELDS DATATYPE
branch_name varchar2(30)
branch_city varchar2(30)
assets number(8,2)

CREATE TABLE branch_22brs1112 (branch_name VARCHAR(30), branch_city


VARCHAR(30), assets DECIMAL(8,2));
Query OK, 0 rows affected (0.01 sec)

B) NAME: account
FIELDS DATATYPE
account_no varchar2(11)
branch_name varchar2(30)
balance number(8)

CREATE TABLE account_22brs1112 (account_no VARCHAR(11), branch_name


VARCHAR(30), balance DECIMAL(8)); DESCRIBE account_22brs1112;
Query OK, 0 rows affected (0.01 sec)

C) NAME: customer
FIELD DATATYPE
customer_id varchar2(11)
customer_name varchar2(20)
customer_street varchar2(15)
customer_city varchar2(15)

CREATE TABLE customer_22brs1112 (customer_id VARCHAR(11), customer_name


VARCHAR(20), customer_street VARCHAR(15), customer_city VARCHAR(15)); DESCRIBE
customer_22brs1112;

D) NAME: depositor
FIELD DATATYPE
customer_id varchar2(11)
account_no varchar2(11)

CREATE TABLE depositor_22brs1112 (customer_id VARCHAR(11), account_no


VARCHAR(11)); DESCRIBE depositor_22brs1112;
E) NAME: loan
FIELDS DATATYPE
loan_no varchar2(4)
branch_name varchar2(30)
amount number(8,2)

CREATE TABLE loan_22brs1112 (loan_no VARCHAR(4), branch_name VARCHAR(30),


amount DECIMAL(8,2)); DESCRIBE loan_22brs1112;

F) NAME: borrower
FIELDS DATATYPE
customer_id varchar2(11)
loan_no varcahr2(4)

CREATE TABLE borrower_22brs1112 (customer_id VARCHAR(11), loan_no VARCHAR(4));


DESCRIBE borrower_22brs1112;
2. Describe the structure of all database schemas.

3. Alter the structure of the Database

a. Add a new column ‘account opening date’ in the account table.

ALTER TABLE account_22brs1112 ADD COLUMN account_opening_date DATE;


DESCRIBE account_22brs1112;
b. Increase the width of the column customer_street in table customer to
20.

ALTER TABLE customer_22brs1112 MODIFY COLUMN customer_street VARCHAR(20);


DESCRIBE customer_22brs1112;

4. Add primary keys to all the tables for the specified attributes

A) NAME: branch
FIELDS DATATYPE
branch_name varchar2(30) primary key
branch_city varchar2(30)
assets number(8,2)

ALTER TABLE branch_22brs1112 ADD PRIMARY KEY (branch_name);


DESCRIBE branch_22brs1112
B) NAME: account
FIELDS DATATYPE
account_no varchar2(11) primary key
branch_name varchar2(30)
balance number(8)

ALTER TABLE account_22brs1112 ADD PRIMARY KEY (account_no);


DESCRIBE account_22brs1112;

C) NAME: customer
FIELD DATATYPE
customer_id varchar2(11) primary key
customer_name varchar2(20)
customer_street varchar2(15)
customer_city varchar2(15)

ALTER TABLE customer_22brs1112 ADD PRIMARY KEY (customer_id);


DESCRIBE customer_22brs1112;
D) NAME: loan
FIELDS DATATYPE
loan_no varchar2(4) primary key
branch_name varchar2(30)
amount number(8,2)

ALTER TABLE loan_22brs1112 ADD PRIMARY KEY (loan_no);


DESCRIBE loan_22brs1112;

5. Add foreign keys to the following tables for the specified attributes with
mentioned
reference table
B) NAME: account
FIELDS DATATYPE
account_no varchar2(11) primary key
branch_name varchar2(30) references branch(branch_name)
balance number(8)

ALTER TABLE account_22brs1112


ADD FOREIGN KEY (branch_name) REFERENCES branch_22brs1112(branch_name);

DESCRIBE account_22brs1112;
C) NAME: depositor
FIELD DATATYPE
customer_id varchar2(11)references customer (customer_id)
account_no varchar2(11)references account (account_no)

ALTER TABLE depositor_22brs1112


ADD FOREIGN KEY (customer_id) REFERENCES customer_22brs1112(customer_id),
ADD FOREIGN KEY (account_no) REFERENCES account_22brs1112(account_no);
DESCRIBE depositor_22brs1112;

D) NAME: loan
FIELDS DATATYPE
loan_no varchar2(4) primary key
branch_name varchar2(30) references branch(branch_name)
(Create constraint with constraint name)
amount number(8,2)

ALTER TABLE loan_22brs1112


ADD FOREIGN KEY (branch_name) REFERENCES branch_22brs1112(branch_name),
ADD CONSTRAINT fk_loan_branch FOREIGN KEY (branch_name) REFERENCES
branch_22brs1112(branch_name),
ADD FOREIGN KEY (branch_name) REFERENCES branch_22brs1112(branch_name);

DESCRIBE loan_22brs1112;

6.Drop foreign key constraint from loan table

ALTER TABLE loan_22brs1112


DROP FOREIGN KEY fk_loan_branch;

DESCRIBE loan_22brs1112;
7.Set loan_no attribute of borrower table as foreign key with cascade
deletion,
which refers to loan table loan_no column.

ALTER TABLE borrower_22brs1112


ADD FOREIGN KEY (loan_no) REFERENCES loan_22brs1112(loan_no) ON DELETE
CASCADE;

DESCRIBE borrower_22brs1112;

8.Add foreign key for the customer_id of borrower table which refers to
customer table with
constraint name.

ALTER TABLE borrower_22brs1112 ADD CONSTRAINT fk_borrower_customer FOREIGN


KEY (customer_id) REFERENCES customer_22brs1112(customer_id);
DESCRIBE borrower_22brs1112;
9. Insert the following values into the tables
1. branch :
BRANCH_NAME BRANCH_CITY ASSETS
Perryridge Rye 5000000
Downtown Stamford 1000000
Brighton Paloalto 2500000
Redwood Harrison 1500000
Mianus Pitsfield 4500000
Roundhill Princeton 1500000

INSERT INTO branch_22brs1112 VALUES ('Perryridge', 'Rye', 5000000);


INSERT INTO branch_22brs1112 VALUES ('Downtown', 'Stamford', 1000000);
INSERT INTO branch_22brs1112 VALUES ('Brighton', 'Paloalto', 2500000);
INSERT INTO branch_22brs1112 VALUES ('Redwood', 'Harrison', 1500000);
INSERT INTO branch_22brs1112 VALUES ('Mianus', 'Pitsfield', 4500000);
INSERT INTO branch_22brs1112 VALUES ('Roundhill', 'Princeton', 1500000);
DESCRIBE branch_22brs1112;
2. account :
ACCOUNT_NO BRANCH_NAME BALANCE
019_28_3746 Perryridge 15000
182_73_6091 Downtown 23000
192_83_7465 Brighton 18000
321_12_3123 Redwood 5000
336_66_9999 Mianus 5000
963_96_3963 Roundhill 5000
376_66_9999 Mianus 9000
963_96_3964 Mianus 13000

INSERT INTO account_22brs1112 (account_no, branch_name, balance)


VALUES
('019_28_3746', 'Perryridge', 15000),
('182_73_6091', 'Downtown', 23000),
('192_83_7465', 'Brighton', 18000),
('321_12_3123', 'Redwood', 5000),
('336_66_9999', 'Mianus', 5000),
('963_96_3963', 'Roundhill', 5000),
('376_66_9999', 'Mianus', 9000),
('963_96_3964', 'Mianus', 13000);
3. loan :
LOAN BRANCH_NAME AMOUNT
1_11 Roundhill 9000
1_14 Downtown 15000
1_15 Perryridge 15000
1_16 Perryridge 13000
1_17 Downtown 10000
1_23 Redwood 20000
1_93 Mianus 500

INSERT INTO loan_22brs1112 VALUES ('1_11', 'Roundhill', 9000);


INSERT INTO loan_22brs1112 VALUES ('1_14', 'Downtown', 15000);
INSERT INTO loan_22brs1112 VALUES ('1_15', 'Perryridge', 15000);
INSERT INTO loan_22brs1112 VALUES ('1_16', 'Perryridge', 13000);
INSERT INTO loan_22brs1112 VALUES ('1_17', 'Downtown', 10000);
INSERT INTO loan_22brs1112 VALUES ('1_23', 'Redwood', 20000);
INSERT INTO loan_22brs1112 VALUES ('1_93', 'Mianus', 500);
DESCRIBE loan_22brs1112;
4. depositor
CUSTOMER_ID ACCOUNT_NO
c_08 182_73_6091
c_03 192_83_7465
c_05 321_12_3123
c_07 336_66_9999
c_08 963_96_3963
c_02 376_66_9999

INSERT INTO depositor_22brs1112 VALUES ('c_08', '182_73_6091');


INSERT INTO depositor_22brs1112 VALUES ('c_03', '192_83_7465');
INSERT INTO depositor_22brs1112 VALUES ('c_05', '321_12_3123');
INSERT INTO depositor_22brs1112 VALUES ('c_07', '336_66_9999');
INSERT INTO depositor_22brs1112 VALUES ('c_08', '963_96_3963');
INSERT INTO depositor_22brs1112 VALUES ('c_02', '376_66_9999');
DESCRIBE depositor_22brs1112;

5. customer
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_STREET
CUSTOMER_CITY
c_01 smith north rye
c_02 turner putnam stamford
c_03 johnson alma palo alto
c_04 curry north rye
c_05 jones main harrisdon
c_06 adoms spring pittsfield
c_07 lindsay park pittsfield
c_08 hayes main harrison
c_09 williams nassau Princeton

INSERT INTO customer_22brs1112 VALUES ('c_01', 'smith', 'north', 'rye');


INSERT INTO customer_22brs1112 VALUES ('c_02', 'turner', 'putnam', 'stamford');
INSERT INTO customer_22brs1112 VALUES ('c_03', 'johnson', 'alma', 'palo alto');
INSERT INTO customer_22brs1112 VALUES ('c_04', 'curry', 'north', 'rye');
INSERT INTO customer_22brs1112 VALUES ('c_05', 'jones', 'main', 'harrison');
INSERT INTO customer_22brs1112 VALUES ('c_06', 'adams', 'spring', 'pittsfield');
INSERT INTO customer_22brs1112 VALUES ('c_07', 'lindsay', 'park', 'pittsfield');
INSERT INTO customer_22brs1112 VALUES ('c_08', 'hayes', 'main', 'harrison');
INSERT INTO customer_22brs1112 VALUES ('c_09', 'williams', 'nassau', 'Princeton');

DESCRIBE customer_22brs1112;
6. borrower
CUSTOMER_ID LOAN_NO
c_01 1_11
c_01 1_23
c_03 1_93
c_05 1_17
c_03 1_16
c_05 1_14

INSERT INTO borrower_22brs1112 VALUES ('c_01', '1_11');


INSERT INTO borrower_22brs1112 VALUES ('c_01', '1_23');
INSERT INTO borrower_22brs1112 VALUES ('c_03', '1_93');
INSERT INTO borrower_22brs1112 VALUES ('c_05', '1_17');
INSERT INTO borrower_22brs1112 VALUES ('c_03', '1_16');
INSERT INTO borrower_22brs1112 VALUES ('c_05', '1_14');
DESCRIBE borrower_22brs1112;

10. Create the Database Schema for a Employee-pay scenario


a) employee(emp_id : integer, emp_name: string, address: string, city:
string)
CREATE TABLE employee (emp_id INT PRIMARY KEY, emp_name VARCHAR(255),
address VARCHAR(255), city VARCHAR(255));
DESCRIBE employee;

b) department(dept_id: integer, dept_name:string)

CREATE TABLE department (dept_id INT PRIMARY KEY, dept_name VARCHAR(255));


DESCRIBE department;

c) paydetails(emp_id : integer, dept_id: integer, basic: integer,


deductions: integer, additions: integer, DOJ: date)

CREATE TABLE paydetails (emp_id INT, dept_id INT, basic INT, deductions INT, additions
INT, DOJ DATE, PRIMARY KEY (emp_id, dept_id), FOREIGN KEY (emp_id)
REFERENCES employee(emp_id), FOREIGN KEY (dept_id) REFERENCES
department(dept_id));
DESCRIBE paydetails;
d) payroll(emp_id : integer, pay_date: date)

CREATE TABLE payroll (emp_id INT, pay_date DATE, PRIMARY KEY (emp_id, pay_date),
FOREIGN KEY (emp_id) REFERENCES employee(emp_id));
DESCRIBE payroll;

11.Create PRIMARY KEY for employee(emp_id) and


department(dept_id)

TABLE employee ADD PRIMARY KEY (emp_id);


ALTER TABLE department ADD PRIMARY KEY (dept_id);

12.Enforce NOT NULL constraint for emp_name.

ALTER TABLE employee MODIFY emp_name VARCHAR(255) NOT NULL;

13.Creates a DEFAULT constraint on the "City" column of employee


table.

ALTER TABLE employee MODIFY city VARCHAR(255) DEFAULT 'Unknown';


14. Create NOT NULL for dept_id on department table.

ALTER TABLE department MODIFY dept_id INT NOT NULL;

15. Create NOT NULL for basic in pay details.

ALTER TABLE paydetails MODIFY basic INT NOT NULL

16.Enforce CHECK constraints for (deductions > 780) on pay details.

ALTER TABLE paydetails ADD CHECK (deductions > 780);

You might also like