Introduction To SQL
Introduction To SQL
Introduction To SQL
Introduction to SQL
What is SQL?
The category of DML contains four basic statements. These statements are used
most often by application developers.
1. SELECT - extracts data from a database
- Which retrieves rows from a table.
- The SELECT statement specifies which columns to include in the result
set.
- The vast majority of the SQL commands used in applications are
SELECT statements.
2. UPDATE - updates data in a database
- Which modifies existing rows in a table.
- In other words it changes an existing value in a column of a table.
3. DELETE - deletes data from a database
- which removes a specified row or a set of rows from a table.
4. INSERT INTO/ insert - inserts new data into a database
-Which adds rows to a table.
-INSERT is used to populate a newly-created table or to add a new row
(or rows) to an already-existing table.
- Creates a table with the column names the user provides. The user also
needs to specify the data type for each column. Unfortunately, data
types vary slightly from one RDBMS to another, so that user might
need metadata to establish the data types used for a particular
database.
- It is normally used less often than the data manipulation commands
because a table is created only once, whereas inserting and deleting
rows or changing individual values generally occurs more frequently.
4. ALTER TABLE - modifies a table
-Adds or removes a column from a table.
-This command is used in connection with ADD, MODIFY and
DROP.
5. DROP TABLE - deletes a table
-Deletes all rows and removes the table definition from the
database.
Statements : which may have a persistent effect on schemas and data, or which may
control transactions, program flow, connections, sessions, or diagnostics.
Queries: which retrieve data based on specific criteria.
Expressions: which can produce either scalar values or tables consisting of columns
and rows of data.
Predicates: specify conditions that can be evaluated to SQL three-valued logic
(3VL) Boolean truth values and which are used to limit the effects of statements and
queries, or to change program flow.
Clauses: are optional, constituent components of statements and queries.
Whitespace is generally ignored in SQL statements and queries, making it easier to
format SQL code for readability.
SQL statements also include the semicolon (";") statement terminator. Though not
required on every platform, it is defined as a standard part of the SQL grammar.
All SQL statements have the same basic form. i.e all SQL statements has similar
structures
Example:
Every SQL statement begins with a verb, a keyword that describes what the statement
does. CREATE, INSERT, and DELETE are typical verbs. The statement continues with
one or more clauses. A clause may specify the data to be acted upon by the statement, or
provide more detail about what the statement is supposed to do.
Every clause also begins with a keyword, such as WHERE, FROM, INTO, and HAVING.
Some clauses are optional; others are required.
The SQL keywords are words that are reserved words that are not used as a user defined
data. The most commonly used SQL key words according to ANSI/ISO SQL keywords are
as follows:
The ANSI/ISO SQL standard specifies tables names(which identify tables), column names
(which identify columns), and user names (which identify users of the database)
The SQL database Name should not be empty and special characters. The ANSI/ISO
standards specifies that SQL names must contain 1 to 18 characters, begin with a letter,
and my not contain any spaces or special punctuation characters.
2.6. Constants
In some SQL statements a numeric, character, or date data value must be expressed in
text form.
For example: INSERT statement, w/c adds a student to the database:
INSERT INTO student(Fname, SID, Dept, year)
VALUES (‘Alemayehu’, 200, ‘Comp Science’, 2)
The value for each column in the newly inserted row is specified in the VALUES clause.
Constant data values are also used in expression such as in the SELECT statement
SELECT city
FROM offices
WHERE TARGET >(1.1* SALES) +1000.00
If a single quotes is to be used included in the constant text, it is written within the constant
as two consecutive single quote characters. This is constant value:
Example: “I can’t”
2.7. Expressions
Expressions are used in the SQL Languages to calculate values that are retrieved from the
database and to calculate values used in searching the database.
Example1: The query that calculates sales of each offices as a percentage of its target:
Example2:
SELECT city
FROM offices
WHERE sales > target +50000.00
It is a value whose value is missing, unknown, or don’t apply. SQL supports missing,
unknown, or inapplicable data explicitly through the concepts of null values. A null Value is
an indicator that tells SQL (and the user) that the data is missing or not applicable.
The empty table can be filled with data with the INSERT INTO statement.
To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple
columns, use the following SQL syntax:
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
)
To create a FOREIGN KEY constraint on the "P_Id" column when the "Orders" table is
already created, use the following SQL::
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
To allow naming of a FOREIGN KEY constraint, and for defining a FOREIGN KEY
constraint on multiple columns, use the following SQL syntax:
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders
FOREIGN KEY (P_Id)
REFERENCES Persons(P_Id)
The DEFAULT constraint can also be used to insert system values, by using functions like
GETDATE():
We are using MS Access and SQL Server 2000 and we do not have to put a
semicolon after each SQL statement, but some database programs force to use it.
An SQL statement can be entered on one line or split across several lines for clarity.
For most systems SQL is not case sensitive. We can mix uppercase and lowercase
when referencing SQL keywords (such as SELECT and INSERT), tables names,
and column names.
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,...)
Now we want to select the content of the columns named "LastName" and "FirstName"
from the table above.
We use the following SELECT statement:
SELECT LastName,FirstName FROM Persons
The result-set will look like this:
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
SELECT * Example
Now we want to select all the columns from the "Persons" table.
We use the following SELECT statement:
SELECT * FROM Persons
Tip: The asterisk (*) is a quick way of selecting all columns!
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
Navigation in a Result-set
Most database software systems allow navigation in the result-set with programming
functions, like: Move-To-First-Record, Get-Record-Content, Move-To-Next-Record, etc.
ORDER BY 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 all the persons from the table above, however, we want to sort the
persons by their last name.
We use the following SELECT statement:
SELECT * FROM Persons
ORDER BY LastName
The result-set will look like this:
P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
4 Nilsen Tom Vingvn 23 Stavanger
3 Pettersen Kari Storgt 20 Stavanger
2 Svendson Tove Borgvn 23 Sandnes
The MS SQL Server uses the IDENTITY keyword to perform an auto-increment feature.
By default, the starting value for IDENTITY is 1, and it will increment by 1 for each new
record.
To specify that the "P_Id" column should start at value 10 and increment by 5, change the
identity to IDENTITY(10,5).
To insert a new record into the "Persons" table, we will not have to specify a value for the
"P_Id" column (a unique value will be added automatically):
INSERT INTO Persons (FirstName,LastName)
VALUES ('Lars','Monsen')
The SQL statement above would insert a new record into the "Persons" table. The "P_Id"
column would be assigned a unique value. The "FirstName" column would be set to "Lars"
and the "LastName" column would be set to "Monsen".
Note: COUNT(DISTINCT) works with ORACLE and Microsoft SQL Server, but not with
Microsoft Access.
which is the number of unique customers (Hansen, Nilsen, and Jensen) in the "Orders"
table.
Hansen 2000
Jensen 2000