Professional Documents
Culture Documents
Database Concepts and Data Processingsql
Database Concepts and Data Processingsql
SQL
Introduction
The SQL is a two in one language containing both the
DDL and the DML.
The data definition language(DDL) is used define the
structure of the data , modify the data and specify
security constraints on the data.
The DML manipulates the data that is in database. For
instance one can use the DML to extract certain
information that he/she needs from a database.
DDL
The CREATE TABLE c0mmand is used to specify a
new relation by giving it a name and specifying its
attributes and constraints.
The attributes are specified first and each attribute is
given a name, a data type to specify its domain of
values and possible some constraint.
The key attributes , entity integrity and referential
integrity constraints are then specified.
DDL1
The data types available for the attributes include
numeric, character-string , bit string , date and time.
Numeric – INTEGER
DATE
TIME
DECIMAL (i,j) were i the total number of decimal
digits and j is the number of digits after the decimal
point.
SQL allows NULLs to be specified as attribute values, a
constant NOT NULL can be specified if NULL is not
permitted for that attribute.
DDL cont2
It is also possible to define a default value for an
attribute by appending the clause DEFAULT <value>
to an attribute definition.
The PRIMARY KEY clause specifies one or more
attributes that make up the primary key of a relation.
The UNIQUE clause specifies alternate keys
Referential integrity is specified via the FOREIGN key
clause .
A referential integrity can be violated when tuples are
inserted or deleted or when a foreign key attribute
value is modified.
Basic Queries in SQL
A database most often contains one or more tables
each identified by a name (relation).
The table consists of columns(attributes) and
rows(tuples).
Below is an example of a Table called PERSONS
SQL database tables
Person
LastName
Smith
Dube
Kelly
SQL data manipulation
language
SQL (Structured Query Language) is a syntax for executing
queries. But the SQL language also includes a syntax to
update, insert, and delete records.
These query and update commands together form the Data
Manipulation Language (DML) part of SQL:
LastName FirstName
Smith John
Dube Sam
Kelly Joseph
Select all columns
To select all columns from the "Persons" table, use a *
symbol instead of column names, like this:
SELECT * FROM Persons
The SELECT DISTINCT Statement
The DISTINCT keyword is used to return only distinct
(different) values.
The SELECT statement returns information from table
columns.
What if we only want to select distinct elements?
With SQL, all we need to do is to add a DISTINCT keyword to
the SELECT statement:
Syntax
SELECT DISTINCT column_name(s)
FROM table_name
Using the DISTINCT keyword
To select ALL values from the column named "Company" we use a SELECT
statement like this:
SELECT Company FROM Orders
Orders
Company
Sega
Company OrderNumber
W3Schools
Sega 3412
Trio
W3Schools 2312
W3Schools
Trio 4678
W3Schools 6798
Note that "W3Schools" is listed twice in the result-set.
To select only DIFFERENT values from the column named
"Company" we use a SELECT DISTINCT statement like this:
SELECT DISTINCT Company FROM Orders
Orders
Company
Sega
Company OrderNumber W3Schools
Sega 3412 Trio
W3Schools 2312
Trio 4678
W3Schools 6798
Select All Columns
The WHERE clause is used to specify a selection criterion.
The WHERE Clause
To conditionally select data from a table, a WHERE clause can be added to the
SELECT statement.
Syntax
SELECT column FROM table
WHERE column operator value
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
This is wrong:
SELECT *
FROM Person
WHERE FirstName=Sam
Cont
This is also incorrect
Select * Lastname, Firstname
From Person
Where city = ‘hre’
Syntax
INSERT INTO table_name
VALUES (value1, value2,....)
You can also specify the columns for which you want to insert data:
INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)
Insert a New Row
LastName FirstName Address City
We want to add a first name to the person with a last name of "Rasmussen":
UPDATE Person
SET FirstName = 'Nina'
WHERE LastName = 'Rasmussen'
We want to change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Update Person
Set city = ‘ Montrose’
Where Firstname = ‘Nina’
SQL The Delete Statement
The Delete Statement
The DELETE statement is used to delete rows in a table.
Syntax
DELETE FROM table_name
WHERE column_name = some_value