Question

In: Computer Science

Use a single SQL statement to create a relational table and to load into the table...

Use a single SQL statement to create a relational table and to load into the table department name, subject code, year of running and session of running that offered by the departments.

Note that a running subject offered by a department means a lecturer of the department has been assigned to teach the subject.

Next, enforce the appropriate consistency constraints on the new table.

   When ready use SELECT statement to list the contents of the relational table created and filled with data in the ascending order of the department name, then in the ascending order of code, year running, and session running.      

Finally, drop the relational table created in this subtask.  

Solutions

Expert Solution

Pre requisites

SQL> create table department(dept_name varchar(15) primary key, dept_location varchar(10));

Table created.

SQL> create table lecturer(lecturer_id varchar(3) primary key, name varchar(15),
dept_name varchar(15) references department(dept_name));

Table created.
SQL> create table coruse(course_code varchar(5) primary key, subject_code varchar(5),
dept_name varchar(15) references department(dept_name), year_of_running number(5),
session_of_running varchar(10), teacher varchar(3) references lecturer(lecturer_id));

Table created.

SQL> insert into department values('CSE', 'campus 1');

1 row created.

SQL> insert into department values('ECE', 'campus 1');

1 row created.

SQL> insert into department values('ME', 'campus 2');

1 row created.

SQL> insert into department values('Civil', 'campus 2');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from department;

DEPT_NAME DEPT_LOCAT
--------------- ----------
CSE campus 1
ECE campus 1
ME campus 2
Civil campus 2


SQL> insert into lecturer values('C01', 'Jack','CSE');

1 row created.

SQL> insert into lecturer values('C02', 'Jones','CSE');

1 row created.

SQL> insert into lecturer values('C03', 'Jenifer','CSE');

1 row created.

SQL> insert into lecturer values('C04', 'Keily','CSE');

1 row created.

SQL> insert into lecturer values('E01', 'Kashyap','ECE');

1 row created.

SQL> insert into lecturer values('E02', 'Karuna','ECE');

1 row created.

SQL> insert into lecturer values('E03', 'Susi','ECE');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from lecturer;

LEC NAME DEPT_NAME
--- --------------- ---------------
C01 Jack CSE
C02 Jones CSE
C03 Jenifer CSE
C04 Keily CSE
E01 Kashyap ECE
E02 Karuna ECE
E03 Susi ECE

7 rows selected.


SQL> desc coruse;
Name Null? Type
----------------------------------------- -------- ----------------------------
COURSE_CODE NOT NULL VARCHAR2(5)
SUBJECT_CODE VARCHAR2(5)
DEPT_NAME VARCHAR2(15)
YEAR_OF_RUNNING NUMBER(5)
SESSION_OF_RUNNING VARCHAR2(10)
TEACHER VARCHAR2(3)


SQL> insert into coruse values('CS11', 'Java', 'CSE', 2020, 'spring', 'C01');

1 row created.

SQL> insert into coruse values('CS21', 'DB', 'CSE', 2020, 'spring', 'C02');

1 row created.

SQL> insert into coruse values('CS23', 'CO', 'CSE', 2020, 'spring', 'C03');

1 row created.

SQL> insert into coruse values('EC23', 'MP', 'ECE', 2020, 'Summer', 'E01');

1 row created.

SQL> insert into coruse values('EC24', 'MC', 'ECE', 2019, 'Fall', 'E02');

1 row created.

SQL> select * from coruse;

COURS SUBJE DEPT_NAME YEAR_OF_RUNNING SESSION_OF TEA
----- ----- --------------- --------------- ---------- ---
CS11 Java CSE 2020 spring C01
CS21 DB CSE 2020 spring C02
CS23 CO CSE 2020 spring C03
EC23 MP ECE 2020 Summer E01
EC24 MC ECE 2019 Fall E02

The above table cretation and data populations should already exist in your database system in order to fulfull your question requirements. Following are the actual queries for your question.

Actual query for your question;

SQL> create table coursedept1
as(select coruse.dept_name, subject_code, year_of_running, session_of_running from coruse, lecturer
where coruse.teacher= lecturer.lecturer_id and coruse.dept_name=lecturer.dept_name);

Table created.

SQL> select * from coursedept1;

DEPT_NAME SUBJE YEAR_OF_RUNNING SESSION_OF
--------------- ----- --------------- ----------
CSE Java 2020 spring
CSE DB 2020 spring
CSE CO 2020 spring
ECE MP 2020 Summer
ECE MC 2019 Fall

SQL> select * from coursedept1 order by dept_name;

DEPT_NAME SUBJE YEAR_OF_RUNNING SESSION_OF
--------------- ----- --------------- ----------
CSE Java 2020 spring
CSE DB 2020 spring
CSE CO 2020 spring
ECE MP 2020 Summer
ECE MC 2019 Fall

SQL> select * from coursedept1 order by subject_code;

DEPT_NAME SUBJE YEAR_OF_RUNNING SESSION_OF
--------------- ----- --------------- ----------
CSE CO 2020 spring
CSE DB 2020 spring
CSE Java 2020 spring
ECE MC 2019 Fall
ECE MP 2020 Summer

SQL> select * from coursedept1 order by year_of_running;

DEPT_NAME SUBJE YEAR_OF_RUNNING SESSION_OF
--------------- ----- --------------- ----------
ECE MC 2019 Fall
CSE DB 2020 spring
ECE MP 2020 Summer
CSE Java 2020 spring
CSE CO 2020 spring

SQL> select * from coursedept1 order by session_of_running;

DEPT_NAME SUBJE YEAR_OF_RUNNING SESSION_OF
--------------- ----- --------------- ----------
ECE MC 2019 Fall
ECE MP 2020 Summer
CSE DB 2020 spring
CSE Java 2020 spring
CSE CO 2020 spring

SQL> drop table coursedept1;

Table dropped.


Related Solutions

do the following SQL programming tasks: Use the CREATE TABLE statement to build the sample table...
do the following SQL programming tasks: Use the CREATE TABLE statement to build the sample table (MODULE) Use the INSERT INTO statement to populate it - use either the data in the image or your own Write an SQL query to display the whole populated table Write an SQL query to display certain combinations of columns (use your imagination) Write an SQL query to extract certain combinations of columns and rows (imagination again!)
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table as if it were being added to MySQL (please give explanations for each line of SQL code and a copy of the code as it would be entered into the query by itself: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer...
DBMS Create/Insert/Update SQL I need the create, insert, and update SQL statement for this table: Customer PK Customer ID Text Phone Number int name text address ID int email text FK vendor ID int Vendor is the name of the table the FK comes from.
SQL This assignment aligns with the following objectives: Create a relational database containing at least three...
SQL This assignment aligns with the following objectives: Create a relational database containing at least three tables Populate the database using the INSERT command Create integrity constraints on the tables created Deli has hired you as a consultant to design a database for the deli. They have provided you with the following information: Every employee has a social security number, name, salary, and date of hire. The deli is organized into several departments. Each department has a unique name. Each...
1. Use SQL to create a polyinstantiated table including a primary key and a unique constraint...
1. Use SQL to create a polyinstantiated table including a primary key and a unique constraint 2.Use SQL to insert multiple records for each security classification with the same ID. You must have 4 classifications. 3.Use SQL to create 4 schemas, one for each security classification 4.Use SQL to create a view in each schema that restricts the records to those belonging to a particular security classification and restricts the columns to only those columns that have relevant data. 5.Select...
USE SQL CREATE TABLE IF NOT EXISTS football_games ( visitor_name VARCHAR(30),       /* Name of the visiting...
USE SQL CREATE TABLE IF NOT EXISTS football_games ( visitor_name VARCHAR(30),       /* Name of the visiting team                     */ home_score SMALLINT NOT NULL,   /* Final score of the game for the Buffs         */ visitor_score SMALLINT NOT NULL,/* Final score of the game for the visiting team */ game_date DATE NOT NULL,        /* Date of the game                              */ players INT[] NOT NULL,         /* This array consists of the football player ids (basically a foreign key to the football_player.id) */ PRIMARY KEY(visitor_name, game_date)...
Write a SQL statement which joins the parts table with the supplier table and lists the...
Write a SQL statement which joins the parts table with the supplier table and lists the part_name, supplier_name for all parts in the part table. The supplier_id column in the suppliers table is the primary key in the suppliers table, and this key has been exported to the parts table where it is a foreign key. You should use an inner join for this query. Write a SQL statement which joins the parts table with the suppliers table and lists...
Write a SQL statement which joins the parts table with the supplier table and lists the...
Write a SQL statement which joins the parts table with the supplier table and lists the part_name, supplier_name for all parts in the part table. The supplier_id column in the suppliers table is the primary key in the suppliers table, and this key has been exported to the parts table where it is a foreign key. You should use an inner join for this query. Write a SQL statement which joins the parts table with the suppliers table and lists...
Database questions: USE THE FOLLOWING SQL CODE TO SOLVE NEXT QUESTIONS: CREATE TABLE ROBOT ( Serial_no...
Database questions: USE THE FOLLOWING SQL CODE TO SOLVE NEXT QUESTIONS: CREATE TABLE ROBOT ( Serial_no INT NOT NULL, Model VARCHAR(20) NOT NULL, Manufacturer VARCHAR(20) NOT NULL, Price INT NOT NULL, PRIMARY KEY (Serial_no) ); INSERT INTO ROBOT VALUES (1, 'Scara','Epson', 23200); INSERT INTO ROBOT VALUES (2, 'ASSISTA','Mitsubishi', 17500); INSERT INTO ROBOT VALUES (3, 'Lego Mindstorm','NXT', 650); INSERT INTO ROBOT VALUES (4, 'Yumi','ABB', 40000); INSERT INTO ROBOT VALUES (5, 'Pepper','Foxconn', 1600); INSERT INTO ROBOT VALUES (6, 'Humanoid','Honda', 30000); SELECT *...
1. Write a SQL statement which joins the rider_student table with the rider_major table and lists...
1. Write a SQL statement which joins the rider_student table with the rider_major table and lists the rider student name and the name of the major (major_name) and the description of the major for which they are currently assigned. (You may use the SQL 'join' subclause, or simply express the join as part of the 'where' clause by indicating that you only want records where the primary key of the child table, rider_major, equals the corresponding foreign key of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT