Tuesday, 2 October 2012

SQL LECTURE 5

Lecture 5


The UPDATE Statement

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

SQL UPDATE Syntax

UPDATE table_name

SET column1=value, column2=value2,...

WHERE some_column=some_value

Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

________________________________________

SQL UPDATE Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob
  

Now we want to update the person "Tjessem, Jakob" in the "Persons" table.

We use the following SQL statement:

UPDATE Persons

SET Address='Nissestien 67', City='Sandnes'

WHERE LastName='Tjessem' AND FirstName='Jakob'

The "Persons" table will now look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob Nissestien 67 Sandnes


________________________________________

SQL UPDATE Warning

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

UPDATE Persons

SET Address='Nissestien 67', City='Sandnes'

The "Persons" table would have looked like this:
P_Id LastName FirstName Address City
1 Hansen Ola Nissestien 67 Sandnes
2 Svendson Tove Nissestien 67 Sandnes
3 Pettersen Kari Nissestien 67 Sandnes
4 Nilsen Johan Nissestien 67 Sandnes
5 Tjessem Jakob Nissestien 67 Sandnes


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

________________________________________

The 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

Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

________________________________________

SQL DELETE Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Johan Bakken 2 Stavanger
5 Tjessem Jakob Nissestien 67 Sandnes


Now we want to delete the person "Tjessem, Jakob" in the "Persons" table.

We use the following SQL statement:

DELETE FROM Persons

WHERE LastName='Tjessem' AND FirstName='Jakob'

The "Persons" table will now look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Johan Bakken 2 Stavanger


________________________________________

Delete All Rows

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!

Practice Makes the man perfect

Test your SQL Skills

On this page you can test your SQL skills.

We will use the Customers table in the Northwind database:


CompanyName ContactName Address City
Alfreds Futterkiste  Maria Anders  Obere Str. 57  Berlin 
Berglunds snabbköp  Christina Berglund  Berguvsvägen 8  Luleå 
Centro comercial Moctezuma  Francisco Chang  Sierras de Granada 9993  México D.F. 
Ernst Handel  Roland Mendel  Kirchgasse 6  Graz 
FISSA Fabrica Inter. Salchichas S.A.  Diego Roel  C/ Moralzarzal, 86  Madrid 
Galería del gastrónomo  Eduardo Saavedra  Rambla de Cataluña, 23  Barcelona 
Island Trading  Helen Bennett  Garden House Crowther Way  Cowes 
Königlich Essen  Philip Cramer  Maubelstr. 90  Brandenburg 
Laughing Bacchus Wine Cellars  Yoshi Tannamuri  1900 Oak St.  Vancouver 
Magazzini Alimentari Riuniti  Giovanni Rovelli  Via Ludovico il Moro 22  Bergamo 
North/South  Simon Crowther  South House 300 Queensbridge  London 
Paris spécialités  Marie Bertrand  265, boulevard Charonne  Paris 
Rattlesnake Canyon Grocery  Paula Wilson  2817 Milton Dr.  Albuquerque 
Simons bistro  Jytte Petersen  Vinbæltet 34  København 
The Big Cheese  Liz Nixon  89 Jefferson Way Suite 2  Portland 
Vaffeljernet  Palle Ibsen  Smagsløget 45  Århus 
Wolski Zajazd  Zbyszek Piestrzeniewicz  ul. Filtrowa 68  Warszaw


To preserve space, the table above is a subset of the Customers table used in the example below.

________________________________________

Try it Yourself

To see how SQL works, you can copy the SQL statements below and paste them into the textarea, or you can make your own SQL statements.

SELECT * FROM customers

SELECT CompanyName, ContactName FROM customers

SELECT * FROM customers WHERE companyname LIKE 'a%'

SELECT CompanyName, ContactName

FROM customers

WHERE CompanyName > 'a'

 When using SQL on text data, "alfred" is greater than "a" (like in a dictionary).

SELECT CompanyName, ContactName

FROM customers

WHERE CompanyName > 'g'

AND ContactName > 'g'

Lecture 6

The TOP Clause

The TOP clause is used to specify the number of records to return.

The TOP clause can be very useful on large tables with thousands of records. Returning a large number of records can impact on performance.

Note: Not all database systems support the TOP clause.

SQL Server Syntax

SELECT TOP number|percent column_name(s)

FROM table_name

________________________________________

SQL SELECT TOP Equivalent in MySQL and Oracle

MySQL Syntax

SELECT column_name(s)

FROM table_name

LIMIT number

Example

SELECT *

FROM Persons

LIMIT 5

Oracle Syntax

SELECT column_name(s)

FROM table_name

WHERE ROWNUM <= number

Example

SELECT *

FROM Persons

WHERE ROWNUM <=5

________________________________________

SQL TOP Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Tom Vingvn 23 Stavanger


Now we want to select only the two first records in the table above.

We use the following SELECT statement:

SELECT TOP 2 * FROM Persons

The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


________________________________________

SQL TOP PERCENT Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger
4 Nilsen Tom Vingvn 23 Stavanger


Now we want to select only 50% of the records in the table above.

We use the following SELECT statement:

SELECT TOP 50 PERCENT * FROM Persons

The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


The LIKE operator is used in a WHERE clause to search for a specified pattern in a column.

________________________________________

The LIKE Operator

The LIKE operator is used to search for a specified pattern in a column.

SQL LIKE Syntax

SELECT column_name(s)

FROM table_name

WHERE column_name LIKE pattern

________________________________________

LIKE Operator Example

The "Persons" table:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger


Now we want to select the persons living in a city that starts with "s" from the table above.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City LIKE 's%'

The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.

The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger


Next, we want to select the persons living in a city that ends with an "s" from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City LIKE '%s'

The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


Next, we want to select the persons living in a city that contains the pattern "tav" from the "Persons" table.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City LIKE '%tav%'

The result-set will look like this:
P_Id LastName FirstName Address City
3 Pettersen Kari Storgt 20 Stavanger


It is also possible to select the persons living in a city that does NOT contain the pattern "tav" from the "Persons" table, by using the NOT keyword.

We use the following SELECT statement:

SELECT * FROM Persons

WHERE City NOT LIKE '%tav%'

The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes


SQL wildcards can be used when searching for data in a database.

________________________________________

No comments:

Post a Comment