0% found this document useful (0 votes)
137 views8 pages

Lab Manual 09 PDF

This document contains a lab manual on creating and manipulating databases. It discusses SELECT INTO statements to copy data from one table to another or create a new empty table. It also covers UNION, INTERSECT, and EXCEPT operators to combine or filter results from multiple queries. Finally, it provides examples of database updates using INSERT, UPDATE, and DELETE statements and assigns tasks for students to write queries using these techniques.

Uploaded by

Shahid Zikria
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)
137 views8 pages

Lab Manual 09 PDF

This document contains a lab manual on creating and manipulating databases. It discusses SELECT INTO statements to copy data from one table to another or create a new empty table. It also covers UNION, INTERSECT, and EXCEPT operators to combine or filter results from multiple queries. Finally, it provides examples of database updates using INSERT, UPDATE, and DELETE statements and assigns tasks for students to write queries using these techniques.

Uploaded by

Shahid Zikria
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/ 8

Department of Computer Science & Information Technology

Khwaja Fareed University of Engineering & Information Technology

Course: Database Systems

Lab Instructor:
Ms. Hira Kanwal

Student Name

Student Roll #

Department

Batch/Year/Section

For Lab. Instructor

Marks Signature

KFUEIT Department of CS/IT


Lab Manual # 09 Creating & Manipulating Databases

9.1. Objective
1. SELECT INTO Statement
2. Union
3. Intersect
4. Except Query
5. Database Updates

9.2. SELECT INTO

The SELECT INTO statement copies data from one table and inserts it into a new table.

SELECT *
Into [newtablename]
FROM [existingtablename]

OR

select *
into Students
from Std

The SELECT INTO statement can also be used to create a new, empty table using the schema
of another. Just add a WHERE clause that causes the query to return no data:

SELECT *
INTO newtable
FROM Student
WHERE 1=0

The SELECT INTO Statement is also used to combine data from two tables, and save them
in one new table:

select *
into StudentBackup
from Std join StdMarks
on Std.StdId = StdMarks.Roll#

9.3. UNION

The SQL UNION operator combines the result of two or more SELECT statements.

1. Notice that each SELECT statement within the UNION must have the same number
of columns.
2. The columns must also have similar data types.
3. Also, the columns in each SELECT statement must be in the same order.
4. The UNION operator selects only distinct values by default. To allow duplicate
values, use the ALL keyword with UNION.

Example:

KFUEIT Department of CS/IT


73
Lab Manual # 09 Creating & Manipulating Databases

select * from Emp


union
select * from Employee

Example:
select fax from customer
where fax is not null
union all
select fax from cust

9.4. Intersect

INTERSECT returns only common rows returned by the two SELECT statements.

Just as with the UNION operator, the same rules apply when using the INTERSECT
operator.

select * from Emp


intersect
select * from Employee

9.5. Except

EXCEPT returns only rows, which are not available in second SELECT statement.

select * from Emp


except
select * from Employee

9.6. Database Updates

9.6.1. Insert Statement

KFUEIT Department of CS/IT


74
Lab Manual # 09 Creating & Manipulating Databases

Add new rows of data to a table

Syntax:

INSERT INTO Emp (EmpId,BPS)


VALUES (7,19)

9.6.2. Update Statement

Modifies existing data in a table

Example:
Update Emp
Set Salary = Salary * 1.05
where EmpId = 1

9.6.3. Delete Statement

To delete all rows from table

Delete from Emp

To delete specific rows

Delete from Emp where EmpId=1

9.7. LAB TASKS


9.7.1. Write a query to copy the schema and data of customer table and packages table in to
a new table named ‘backuparea’.

9.7.2. Copy the packages table in to another table only for packages where speed is greater
than 2Mbps. Name the new table as highspeedpackages.

KFUEIT Department of CS/IT


75
Lab Manual # 09 Creating & Manipulating Databases

9.7.3. Create database ‘Orders’ with the following tables

Suppliers (SupplierID, Name, Contact, Address, State)

Employee (EmployeeID, Name, Contact, Address, State)

Customers (CustID, Name, Contact, Address, State)

Note: The below queries will use the database ‘orders’

9.7.4. Create a report showing the distinct contact name and phone numbers for all
employees, customers, and suppliers.

9.7.5. Write a query to show all the phone numbers from customers and suppliers, with
duplicate results if any.

KFUEIT Department of CS/IT


76
Lab Manual # 09 Creating & Manipulating Databases

9.7.6. Modify the above query to include only the values in phone numbers from customers
and suppliers table that match.

9.7.7. Modify the above query to include only the values in phone numbers from customers
table that donot match suppliers table.

9.7.8. In the orders database, select all data from those suppliers and customers who belong
to state USA.

KFUEIT Department of CS/IT


77
Lab Manual # 09 Creating & Manipulating Databases

9.7.9. Based on the Customer table in ACDB database, update the First_name to 'Rehman'
for all records whose customer id is 162.

9.7.10. Delete all those rows from packages table where speed = 1kbps.

9.7.11. Insert new record in all tables of ACDB database, and then back up all data of
customers table in to new table backupcustomers.

KFUEIT Department of CS/IT


78
Lab Manual # 09 Creating & Manipulating Databases

KFUEIT Department of CS/IT


79

You might also like