0% found this document useful (0 votes)
49 views10 pages

Lab - 2 SQL - Assignment - 2: Section1 - Mayank Smart Vehicle Database

The document provides instructions and examples for running complex SQL queries and using views on a smart vehicle database. It includes: I) Examples of queries using GROUP BY, JOIN, ORDER BY, LIMIT, and subqueries to find maximum records and values. II) Examples of creating views on tables and querying the views, including auto-updating views, manually updating view data, and errors from updating partial views without primary keys. The appendix provides the DDL scripts to create the tables in the smart vehicle schema used for the queries and views.

Uploaded by

Aditya Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
49 views10 pages

Lab - 2 SQL - Assignment - 2: Section1 - Mayank Smart Vehicle Database

The document provides instructions and examples for running complex SQL queries and using views on a smart vehicle database. It includes: I) Examples of queries using GROUP BY, JOIN, ORDER BY, LIMIT, and subqueries to find maximum records and values. II) Examples of creating views on tables and querying the views, including auto-updating views, manually updating view data, and errors from updating partial views without primary keys. The appendix provides the DDL scripts to create the tables in the smart vehicle schema used for the queries and views.

Uploaded by

Aditya Arya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 10

Lab – 2 SQL_Assignment_2

7-Sept-2021
IT214 Database Management System, Autumn’2021; Instructor: minal_bhise@daiict, TA: mayank@daiict

Objectives: I) Run complex queries.


II) Using & Updating VIEWS.
Submission: Each student needs to upload a single .pdf file, which will contain the following things
for all the queries listed in your specific section’s lab file. (Submission queries will be given on your
Lab day for your section datasets. Below given queries are for practice purpose.)
1) English query and SQL Query in the given sequence.
2) Screenshot of results.
3) Count of tuples in the results.

I. Run complex Queries – Consider Smart Vehicle Dataset’s Schema


(Avalailable in Lab2.zip) for the below-given examples.
Section1_Mayank Smart Vehicle Database
Vehicle_details
Sensors_details Reg_ID
S_id ChesisNo
S_name Equipped_Sensors Number_Plate
S_description Eq_S_ID Reg_State
S_Freq S_id Owner_name
Reg_ID Owner_city
Sensor_history VType_id
SH_id
Eq_S_ID
Sensor_value
Mesurement_Type
Reading_DateTime

Engine_Spec
Vehicle_types Engine_id
VType_id Max_power
Gearbox_details V_name No_of_cylinders
G_id V_model Fuel_type
G_type V_type Fuel_tank_size
No_of_gears V_colour Max_speed
City_mileage
Highway_mileage
Engine_id
Gearbox_id
a. Use of group by.
select count(*), reg_state from vehicle_details group by reg_state;

b. Use of JOIN & ORDER BY to find a maximum record with details.


SELECT v_name, engine_spec.max_speed
from
sv_db.engine_spec join sv_db.vehicle_types
on
vehicle_types.engine_id = engine_spec.engine_id
order by max_speed desc limit 1;

But it shows only the first record, not all the v_names having similar speeds.

c. Use of a subquery to find a maximum record with details.


Step1.
Write a subquery. Do not attempt to write a full query at once.

SELECT max(max_speed)
FROM sv_db.engine_spec;

Step2.
Write a subquery as a part of the main query to get the ID.

SELECT engine_id, engine_spec.max_speed


from
sv_db.engine_spec
where
engine_spec.max_speed = (select max(engine_spec.max_speed) from
engine_spec);

Step3.
Write a subquery as a part of the main query having joins.

SELECT vehicle_types.v_name, engine_spec.max_speed


from
sv_db.engine_spec join sv_db.vehicle_types
ON
vehicle_types.engine_id = engine_spec.engine_id
where
engine_spec.max_speed = (select max(engine_spec.max_speed) from
engine_spec);

d. Use of a limit & Order by & SubQuery as Tables to find a maximum


record with details.
Step1.
Write a subquery. Do not attempt to write a full query at once.

SELECT engine_id, engine_spec.max_speed


from
sv_db.engine_spec
order by max_speed desc limit 1;.

Step2.
Join the above query with another table.

select vehicle_types.v_name, T1.max_speed from vehicle_types join


(SELECT engine_id, engine_spec.max_speed
from
sv_db.engine_spec
order by max_speed desc limit 1) as T1
on
vehicle_types.engine_id = T1.engine_id;
II. Run complex Queries with VIEWs.

a. Use of a View as Tables to find a maximum record with details.


Step1.
First, create a view. Once executed
CREATE OR REPLACE VIEW MaxSpeed_Engine_VIEW as
SELECT engine_id, engine_spec.max_speed
from
sv_db.engine_spec
order by max_speed desc limit 1;

Step2.
Use VIEW as a table.
select vehicle_types.v_name, max_speed from
vehicle_types
join
MaxSpeed_Engine_VIEW
on
vehicle_types.engine_id = engine_id;

b. Auto-updates of VIEWs.
CREATE OR REPLACE VIEW AVG_Speed_VIEW as
Select AVG(max_speed) from
sv_db.engine_spec;
After creating a view, update the max_speed column by adding new records or
updating existing records, and see if the average gets updated or not.

INSERT INTO sv_db.view_engine_spec(


engine_id, max_power, no_of_cylinders, fuel_type, fuel_tank_size, max_speed)
VALUES
(22, 222, 22, 'Petrol', 30, 500);

The view will automatically update the data.

Select * from AVG_Speed_VIEW;

c. Updating VIEW data manually.

CREATE OR REPLACE VIEW VIEW_ENGINE_SPEC as


Select * from
sv_db.engine_spec;

INSERT INTO sv_db.view_engine_spec(


engine_id, max_power, no_of_cylinders, fuel_type, fuel_tank_size, max_speed)
VALUES
(25, 222, 22, 'Petrol', 30, 550);

Check if Engine_spec has new record or not. As it’s a simple view, it will update the
original table.

SELECT * FROM sv_db.engine_spec


ORDER BY engine_id ASC

d. Updating VIEW data manually.

CREATE OR REPLACE VIEW maxspeed_engine_view as


Select engine_id, max_speed from
sv_db.engine_spec limit 1;

INSERT INTO sv_db.maxspeed_engine_view(


engine_id, max_speed)
VALUES (2, 500);
As it’s not a simple view, it will not update the original table.

e. Updating Partial VIEW of a Table which has PK & FK attributes data


manually.

CREATE OR REPLACE VIEW sv_db.VIEW2_ENGINE_SPEC as


Select engine_id, max_speed from
sv_db.engine_spec;

Try adding new records to check the original table.


INSERT INTO sv_db.VIEW2_ENGINE_SPEC (
engine_id, max_speed)
VALUES (2, 500);
It will add NULL values in other columns.

f. Create & Update below given Partial VIEW of a Table which donot
include PK attributes.

CREATE OR REPLACE VIEW sv_db.VIEW3_ENGINE_SPEC as


Select max_power, max_speed from sv_db.engine_spec;
Try adding new records to check the original table.
INSERT INTO sv_db.view3_engine_spec(
max_power, max_speed)
VALUES (24, 530);
It will show an error.

Appendix A: DDL Scripts to create tables.


-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Equipped_Sensors
(
S_id integer NOT NULL,
Eq_S_ID integer NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START
1 MINVALUE 1 MAXVALUE 2147483647 CACHE 1 ),
Reg_ID integer,
CONSTRAINT Eq_S_ID_PK PRIMARY KEY (Eq_S_ID),
CONSTRAINT EqS_ID_FK FOREIGN KEY (S_id)
REFERENCES SV_DB.Sensors_details (S_id) ON DELETE CASCADE ON UPDATE
CASCADE,
CONSTRAINT V_D_Reg_ID_FK FOREIGN KEY (Reg_ID)
REFERENCES SV_DB.Vehicle_details (Reg_ID) ON DELETE CASCADE ON UPDATE
CASCADE
)
-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Engine_Spec
(
Engine_id bigint NOT NULL,
Max_power integer,
No_of_cylinders integer,
Fuel_type character varying(10),
Fuel_tank_size integer,
Max_speed integer,
CONSTRAINT E_ID_PK PRIMARY KEY (Engine_id)
)
-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Gearbox_details
(
G_id integer NOT NULL,
G_type character varying(10),
No_of_gears integer,
CONSTRAINT G_ID_PK PRIMARY KEY (G_id)
)
-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Sensor_history
(
SH_id integer NOT NULL,
Eq_S_id integer,
Sensor_value double precision,
Mesurement_Type character varying(10),
Reading_DateTime timestamp(6) without time zone,
CONSTRAINT Sh_ID_PK PRIMARY KEY (SH_id),
CONSTRAINT SH_Eq_S_ID_FK FOREIGN KEY (Eq_S_id)
REFERENCES SV_DB.Equipped_Sensors (Eq_S_ID) ON DELETE CASCADE ON UPDATE
CASCADE
)
-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Sensors_details
(
S_id integer NOT NULL,
S_name character varying(30),
S_description character varying(100),
S_Freq integer,
CONSTRAINT S_ID_PK PRIMARY KEY (S_id)
)
-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Vehicle_details
(
Reg_ID integer NOT NULL,
ChesisNo bigint,
Number_Plate character varying(10),
Reg_State character varying(30),
Owner_name character varying(30),
Owner_city character varying(20),
VType_id integer,
CONSTRAINT Reg_ID_PK PRIMARY KEY (Reg_ID),
CONSTRAINT VType_ID_FK FOREIGN KEY (VType_id)
REFERENCES SV_DB.Vehicle_types (VType_id) ON DELETE CASCADE ON UPDATE
CASCADE )
-----------------------------------------------------------------------------------------------------------------
CREATE TABLE SV_DB.Vehicle_types
(
VType_id integer NOT NULL,
V_name character varying(20),
V_model character varying(20),
V_type character varying(20),
V_colour character varying(20),
City_mileage integer,
Highway_mileage integer,
Engine_id integer,
Gearbox_id integer,
CONSTRAINT VType_ID_PK PRIMARY KEY (VType_id),
CONSTRAINT Engline_ID_FK FOREIGN KEY (Engine_id)
REFERENCES SV_DB.Engine_Spec (Engine_id) ON DELETE CASCADE ON UPDATE
CASCADE,
CONSTRAINT Gearbox_ID_FK FOREIGN KEY (Gearbox_id)
REFERENCES SV_DB.Gearbox_details (G_id) ON DELETE CASCADE ON UPDATE
CASCADE)
-----------------------------------------------------------------------------------------------------------------

You might also like