Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
200 views

SQL Lab 3

The document describes SQL commands like SELECT, DELETE, UPDATE, and INSERT used to work with data in a database. Examples are provided to SELECT data from a Customers table, INSERT a new row, DELETE a row where the CustomerName matches a value, and UPDATE rows in the table. The objective is to practice implementing these common SQL commands to manipulate data stored in database tables.

Uploaded by

Adnan Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
200 views

SQL Lab 3

The document describes SQL commands like SELECT, DELETE, UPDATE, and INSERT used to work with data in a database. Examples are provided to SELECT data from a Customers table, INSERT a new row, DELETE a row where the CustomerName matches a value, and UPDATE rows in the table. The objective is to practice implementing these common SQL commands to manipulate data stored in database tables.

Uploaded by

Adnan Asif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LAB # 3:

SQL SELECT, DELETE, UPDATE, INSERT INTO COMMANDS

OBJECTIVE (AIM) OF THE EXPERIMENT


To practice and implement SQL Commands (SELECT, DELETE, UPDATE, INSERT INTO).
EQUIPMENT USED
Sl. Facilities Required Quantity
No.
1 System 1
2 Operating System Windows 7
3 DBMS Sql Server Management Studio
2012

TASKS
The SQL SELECT Statement

The SELECT statement is used to select data from a database.

The result is stored in a result table, called the result-set.

SQL SELECT Syntax

SELECT column_name,column_name
FROM table_name;

and

SELECT * FROM table_name;

Demo Database

We will use the well-known Northwind sample database.

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución 2222 D.F.
helados

3 Antonio Moreno Antonio Mataderos 2312 México 05023 Mexico


Taquería Moreno D.F.
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds snabbköp Christina Berguvsvägen 8 Luleå S-958 22 Sweden


Berglund
Below is a selection from the "Customers" table:

SELECT Column Example

The following SQL statement selects the "CustomerName" and "City" columns from the
"Customers" table:

Example
SELECT CustomerName,City FROM Customers;

SELECT * Example

The following SQL statement selects all the columns from the "Customers" table:

Example
SELECT * FROM Customers;

The SQL INSERT INTO Statement

The INSERT INTO statement is used to insert new records in a table.

SQL INSERT INTO Syntax

It is possible to write the INSERT INTO statement in two forms.

The first form does not specify the column names where the data will be inserted, only their
values:

INSERT INTO table_name


VALUES (value1,value2,value3,...);

The second form specifies both the column names and the values to be inserted:

INSERT INTO table_name (column1,column2,column3,...)


VALUES (value1,value2,value3,...);

Demo Database

In this tutorial we will use the well-known Northwind sample database.

Below is a selection from the "Customers" table:


CustomerID CustomerName ContactName Address City PostalCode Country

87 Wartian Herkku Pirkko Torikatu 38 Oulu 90110 Finland


Koskitalo

88 Wellington Paula Parente Rua do Mercado, Resende 08737-363 Brazil


Importadora 12

89 White Clover Karl Jablonski 305 - 14th Ave. S. Seattle 98128 USA
Markets Suite 3B

90 Wilman Kala Matti Keskuskatu 45 Helsinki 21240 Finland


Karttunen

91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

INSERT INTO Example

Assume we wish to insert a new row in the "Customers" table.

We can use the following SQL statement:

Example
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');

The selection from the "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country

87 Wartian Herkku Pirkko Torikatu 38 Oulu 90110 Finland


Koskitalo

88 Wellington Paula Parente Rua do Mercado, Resende 08737-363 Brazil


Importadora 12

89 White Clover Karl Jablonski 305 - 14th Ave. Seattle 98128 USA
Markets S. Suite 3B

90 Wilman Kala Matti Keskuskatu 45 Helsinki 21240 Finland


Karttunen

91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

92 Cardinal Tom B. Skagen 21 Stavanger 4006 Norway


Erichsen

Insert Data Only in Specified Columns


It is also possible to only insert data in specific columns.

The following SQL statement will insert a new row, but only insert data in the
"CustomerName", "City", and "Country" columns (and the CustomerID field will of course
also be updated automatically):

Example
INSERT INTO Customers (CustomerName, City, Country)
VALUES ('Cardinal', 'Stavanger', 'Norway');

The selection from the "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country

87 Wartian Herkku Pirkko Torikatu 38 Oulu 90110 Finland


Koskitalo

88 Wellington Paula Parente Rua do Mercado, 12 Resende 08737-363 Brazil


Importadora

89 White Clover Markets Karl Jablonski 305 - 14th Ave. S. Seattle 98128 USA
Suite 3B

90 Wilman Kala Matti Keskuskatu 45 Helsinki 21240 Finland


Karttunen

91 Wolski Zbyszek ul. Filtrowa 68 Walla 01-012 Poland

92 Cardinal null null Stavanger null Norway

The SQL DELETE Statement

The DELETE statement is used to delete rows in a table.

SQL DELETE Syntax

DELETE FROM table_name


WHERE some_column=some_value;

Demo Database

In this tutorial we will use the well-known Northwind sample database.


CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución D.F.
helados 2222

3 Antonio Moreno Antonio Mataderos 2312 México 05023 Mexico


Taquería Moreno D.F.

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 22 Sweden


snabbköp Berglund
Below is a selection from the "Customers" table:

SQL DELETE Example

Assume we wish to delete the customer "Alfreds Futterkiste" from the "Customers" table.

We use the following SQL statement:

Example
DELETE FROM Customers
WHERE CustomerName='Alfreds Futterkiste' AND ContactName='Maria Anders';

The "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución 2222 D.F.
helados

3 Antonio Moreno Antonio Mataderos 2312 México 05023 Mexico


Taquería Moreno D.F.

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds snabbköp Christina Berguvsvägen 8 Luleå S-958 22 Sweden


Berglund

Delete All Data

It is possible to delete all rows in a table without deleting the table. This means that the
table structure, attributes, and indexes will be intact:
DELETE FROM table_name;

or

DELETE * FROM table_name;

Note: Be very careful when deleting records. You cannot undo this statement!

The SQL UPDATE Statement

The UPDATE statement is used to update existing records in a table.

SQL UPDATE Syntax

UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;

Notice the WHERE clause in the SQL UPDATE statement!


The WHERE clause specifies which record or records that should be updated. If you omit the WHERE
clause, all records will be updated!

Demo Database

In this tutorial we will use the well-known Northwind sample database.

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución 2222 D.F.
helados

3 Antonio Moreno Antonio Moreno Mataderos 2312 México 05023 Mexico


Taquería D.F.

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds snabbköp Christina Berguvsvägen 8 Luleå S-958 22 Sweden


Berglund
Below is a selection from the "Customers" table:

SQL UPDATE Example


Assume we wish to update the customer "Alfreds Futterkiste" with a new contact person and
city.

We use the following SQL statement:

Example
UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste';

The selection from the "Customers" table will now look like this:

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Alfred Schmidt Obere Str. 57 Hamburg 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México 05021 Mexico


Emparedados y Constitución 2222 D.F.
helados

3 Antonio Moreno Antonio Moreno Mataderos 2312 México 05023 Mexico


Taquería D.F.

4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK

5 Berglunds snabbköp Christina Berguvsvägen 8 Luleå S-958 22 Sweden


Berglund

Update Warning!

Be careful when updating records. If we had omitted the WHERE clause, in the example
above, like this:

UPDATE Customers
SET ContactName='Alfred Schmidt', City='Hamburg';

The "Customers" table would have looked like this:

CustomerID CustomerName ContactName Address City PostalCode Country

1 Alfreds Futterkiste Alfred Obere Str. 57 Hamburg 12209 Germany


Schmidt

2 Ana Trujillo Alfred Avda. de la Hamburg 05021 Mexico


Emparedados y Schmidt Constitución
helados 2222
3 Antonio Moreno Alfred Mataderos 2312 Hamburg 05023 Mexico
Taquería Schmidt

4 Around the Horn Alfred 120 Hanover Hamburg WA1 1DP UK


Schmidt Sq.

5 Berglunds Alfred Berguvsvägen 8 Hamburg S-958 22 Sweden


snabbköp Schmidt

EXPECTED DELIVERABLE

A spool file showing all executions of the above queries.

You might also like