Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

//Create table commands:

create table Student (

student_id number primary key,

student_name varchar2(20) not null,

student_dob date,

student_age number

);

create table Passport (

passport_id number(6) primary key,

passport_name varchar2(20),

stud_passport_id number,

FOREIGN KEY (stud_passport_id) REFERENCES Student(student_id)

);

//Insert both ways:

insert into student values(1,'Rahul Kundal', to_date('2022-12-12','yyyy-mm-dd'), 22);

insert into student(student_id, student_name, student_dob) values(2,'Prakash', to_date('2022-12-


12','yyyy-mm-dd'));

insert into passport values(112345,'Rahul', 2);

insert into passport(PASSPORT_ID, PASSPORT_NAME, STUD_PASSPORT_ID) values(223457,'Prakash',


1);

//Update command: -

update Passport

set stud_passport_id = 1 where passport_id=112345;

Delete command: -

delete from passport where passport_id=223457;


//DDL Commands: -

The major difference between TRUNCATE and DROP is that truncate is used to delete the data inside
the table not the whole table.

whereas drop table delete data and structure of a table.

DROP TABLE table_name;

TRUNCATE TABLE table_name;

//Alter table commands: -

//Add a column:

ALTER TABLE Student

ADD Email varchar(255);

//drop a column from table:

ALTER TABLE Student

DROP COLUMN Email;

//Modify a column

ALTER TABLE student

MODIFY Email varchar2(200);

//Rename command: -

ALTER TABLE Student rename COLUMN student_name TO first_name;

//Adding the constraint:

ALTER TABLE CD

ADD FOREIGN KEY (cd_company_ID ) REFERENCES COMPANY (company_ID);


//Inner join:

select * from student s, passport passport p

where s.student_id = p.stud_passport_id;

Join: -

CREATE TABLE CUSTOMER (


Customer_No NUMBER PRIMARY KEY,
Customer_Name VARCHAR2(50) NOT NULL,
Customer_Address VARCHAR2(100),
Customer_Tel_No VARCHAR2(20)
);

-- Create CAR table


CREATE TABLE CAR (
Car_Reg VARCHAR2(10) PRIMARY KEY,
Car_Make VARCHAR2(50) NOT NULL,
Car_Model VARCHAR2(50) NOT NULL
);

-- Create BOOKING table


CREATE TABLE BOOKING (
Booking_No NUMBER PRIMARY KEY,
Booking_Date DATE,
Customer_No NUMBER,
Car_Reg VARCHAR2(10),
FOREIGN KEY (Customer_No) REFERENCES CUSTOMER (Customer_No),
FOREIGN KEY (Car_Reg) REFERENCES CAR (Car_Reg)
);
SELECT c.company_name, cd.cd_title, t.track_title, cat.cat_description
FROM company c
JOIN cd ON c.company_ID = cd.cd_company_ID
JOIN track t ON cd.cd_IDno = t.track_cd
JOIN category cat ON t.track_cat_ID = cat.cat_ID;

SELECT COUNT(*) AS total_tracks FROM track;

SELECT MAX(track_length) AS max_track_length, MIN(track_length) AS min_track_length


FROM track;

SELECT c.company_name as "Company", AVG(cd.cd_price) AS "Average CD Price of this


Company"
FROM company c
JOIN cd ON c.company_ID = cd.cd_company_ID
GROUP BY c.company_name
HAVING AVG(cd.cd_price) < 10;

You might also like