Islamic University of Gaza (IUG)

Faculty of Engineering

Computer Engineering Department

Database Lab.

ECOM 4113

Lab 4

SQL Basics

Prepared by

Eng. Eman R. Al-Kurdi

October 23, 2010


Creating New Database :

You can create a new database using one of the following methods:

Method 1 :

Write the database Name as shown then press OK.

Now you can see that your created database is added.

Method 2 :

·  Select a new query.

·  From the menu you will find a drop down list that contains all the existed databases, select the master database.

·  Using the following SQL statement you can create a new database.

Create Database Database_name;

·  TO execute your command press the Execute button from the same menu.

·  After executing, refresh the databases by right click > refresh to see your created one.

Database Tables

A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data.

Below is an example ofa table called "Person":

P_Id / FName / LName / Address / City
1 / Hansen / Ola / Timoteivn 10 / Sandnes
2 / Svendson / Tove / Borgvn 23 / Sandnes
3 / Pettersen / Kari / Storgt 20 / Stavanger

The table above contains three records (one for each person) and five columns (P_Id, FName, LName, Address, and City).

SQL Statements

Most of the actions you need to perform on a database are done with SQL statements.

·  To create a table in the database > after opening a query > select the database to which you want to add the table instead of master > the use the following SQL Statement

CREATE TABLE table_name

(

column_name1 data_type,

column_name2 data_type,

column_name3 data_type,

....

)

In the next labs we will say how Constraints can be specified when a table is created (with the CREATE TABLE statement) or after the table is created (with the ALTER TABLE statement). This is just to show how to start.

We will focus on the following constraints:

·  NOT NULL

·  UNIQUE

·  PRIMARY KEY

·  FOREIGN KEY

·  CHECK

·  DEFAULT

Creating Person table :

Keep in Mind That...

·  SQL is not case sensitive

·  After executing a creating or inserting SQL statement you have to commented it or remove it.

·  Some database systems require a semicolon at the end of each SQL statement. We are using MS Access and SQL Server 2000 and we do not have to put a semicolon after each SQL statement

The following SQL statement will select all the records in the "Person" table:

SELECT * FROM Persons

SQL DML and DDL

SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data Definition Language (DDL).

The query and update commands form the DML part of SQL:

·  SELECT - extracts data from a database

·  UPDATE - updates data in a database

·  DELETE - deletes data from a database

·  INSERT INTO - inserts new data into a database

The DDL part of SQL permits database tables to be created or deleted. It also define indexes (keys), specify links between tables, and impose constraints between tables. The most important DDL statements in SQL are:

·  CREATE DATABASE - creates a new database

·  ALTER DATABASE - modifies a database

·  CREATE TABLE - creates a new table

·  ALTER TABLE - modifies a table

·  DROP TABLE - deletes a table

·  CREATE INDEX - creates an index (search key)

·  DROP INDEX- deletes an index

·  SQL INSERT INTO Statement

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

SQL INSERT INTO Syntax

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

The first form doesn't 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,...)

SQL INSERT INTO Example

We have the following "Person" table:

P_Id / FName / LName / Address / City
1 / Hansen / Ola / Timoteivn 10 / Sandnes
2 / Svendson / Tove / Borgvn 23 / Sandnes
3 / Pettersen / Kari / Storgt 20 / Stavanger

Now we want to insert the first row in the "Person" table.

We use the following SQL statement:

INSERT INTO Person
VALUES (1,' Hansen ', ' Ola ', ' Timoteivn 10, ' Sandnes ')

The "Person" table will now look like this:

After adding the second and third rows

Note :

If you leave the first insert statement without comment an error will occur ,since you are trying to insert a row that is already exist (conflict with uniqueness).

Insert Data Only in Specified Columns

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

The following SQL statement will add a new row, but only add data in the "P_Id", "FName" and the "LName" columns:

INSERT INTO Persons (P_Id, LName, FName)
VALUES (4, 'Tjessem', 'Jakob')

The "Person" table will now look like this:

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(s)
FROM table_name

and

SELECT * FROM table_name

Note: SQL is not case sensitive. SELECT is the same as select.

An SQL SELECT Example

The "Person" table:

P_Id / FName / LName / 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 content of the columns named "LName" and "FName" from the table above.

We use the following SELECT statement:

SELECT LName, FName FROM Person

The result-set will look like this:

SELECT * Example

Now we want to select all the columns from the "Person" table.

We use the following SELECT statement:

SELECT * FROM Person

Tip: The asterisk (*) is a quick way of selecting all columns!

The result-set will look like this:

The SQL SELECT DISTINCT Statement

In a table, some of the columns may contain duplicate values. This is not a problem, however, sometimes you will want to list only the different (distinct) values in a table.

The DISTINCT keyword can be used to return only distinct (different) values.

SQL SELECT DISTINCT Syntax

SELECT DISTINCT column_name(s)
FROM table_name

SELECT DISTINCT Example

The "Person" table:

Now we want to select only the distinct values from the column named "City" from the table above.

We use the following SELECT statement:

SELECT DISTINCT City FROM Person

The result-set will look like this:

SQL WHERE Clause

The WHERE clause is used to extract only those records that fulfill a specified condition.

SQL WHERE Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

WHERE Clause Example

Now we want to select only the persons living in the city "Sandnes" from the Person Table.

We use the following SELECT statement:

SELECT * FROM Person
WHERE City='Sandnes'

The result-set will look like this:

Quotes Around Text Fields

SQL uses single quotes around text values (most database systems will also accept double quotes).

Although, numeric values should not be enclosed in quotes.

For text values:

This is correct:
SELECT * FROM Person WHERE FirstName='Tove'
This is wrong:
SELECT * FROM Person WHERE FirstName=Tove

Operators Allowed in the WHERE Clause

With the WHERE clause, the following operators can be used:

Operator / Description
= / Equal
Not equal
Greater than
Less than
>= / Greater than or equal
<= / Less than or equal
BETWEEN / Between an inclusive range
LIKE / Search for a pattern
IN / If you know the exact value you want to return for at least one of the columns

Note: In some versions of SQL the > operator may be written as !=

The AND & OR Operators

The AND operator displays a record if both the first condition and the second condition is true.

The OR operator displays a record if either the first condition or the second condition is true.

AND Operator Example

Now we want to select only the persons with the last name equal to "Tove" AND the first name equal to "Svendson":

We use the following SELECT statement:

SELECT * FROM Person
WHERE LName='Tove'
AND FName='Svendson'

The result-set will look like this:

OR Operator Example

Now we want to select only the persons with the first name equal to " Ola " OR the first name equal to " Sevendson ":

We use the following SELECT statement:

SELECT * FROM Person
WHERE FName='Sevendson'
OR LName='Ola'

The result-set will look like this:

Combining AND & OR

You can also combine AND and OR (use parenthesis to form complex expressions).

Now we want to select only the persons with the first name equal to "Svendson" AND the last name equal to "Tove" OR to "Ola":

We use the following SELECT statement:

SELECT * FROM Person WHERE
FName='Svendson'
AND (LName='Tove' OR LName='Ola')

The result-set will look like this: