Professional Documents
Culture Documents
SQL Binary Relational Operation
SQL Binary Relational Operation
SQL
9/8/2019
SEQUEL
• Structured English QUEry Language
1. implemented by System R
2. easy to learn & use
3. based on familiar English keywords &
avoided difficult relational algebra concepts
such as the division operator
4. also included update operators and a few
other concepts such as view creation &
definition of constraints
• SEQUEL was found to be a trademark for
something else …
– so, the name was changed to SQL: Structured
Query Language
• First product based on SQL was called
Oracle (1979) by a company called
Relational Software, Inc.
• IBM gets in the game in 1981 with
SQL/Data System
• SQL was implemented by all major
relational database suppliers & is now the
world’s most widely used database
language.
• ANSI project to develop SQL standards
– ANSI SQL, SQL-92 (SQL2) and SQL3
What is SQL?
9/8/2019
Concept of SQL
Create
Alter Insert commit
Grant
Drop Delete Rollback
Revoke
Truncate Update savepoint
Rename Select
2 Basic structure of an SQL
query
General SELECT, ALL / DISTINCT, *,
Structure AS, FROM, WHERE
Union UNION
9/8/2019
DATA TYPES
• Character
• char ( length)
• ex: char(10)
• Character
• varchar2(length)
• ex: varchar2(5)
• Number
• number(3) eg., values should be 123, 467
• number (4,1) eg., values should be 123.4, 124.6, 22.5
• Date
• Date
• Eg., 22-oct-82 or 22-oct-1982
One preliminary note..
• SQL is case insensitive!!
CREATE TABLE = create table = CreAte TAblE
4. DROP
• Removes the rows and table definition
• DROP TABLE <tablename>;
5. RENAME
• Changes table name
• RENAME < old tablename> to < new tablename>;
DML STATEMENT
1. The INSERT INTO Statement
The INSERT INTO statement is used to insert new rows into
a table.
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 change the address and add the name of the city:
UPDATE Person
SET Address = 'Stien 12', City = 'Stavanger'
WHERE LastName = 'Rasmussen'
Syntax
SELECT column_name(s)
FROM table_name
To select the columns named "LastName" and "FirstName",
use a SELECT statement like this:
SELECT LastName, FirstName FROM Persons
Persons
výsledok
LastName FirstName
Hansen Ola
Svendson Tove
Pettersen Kari
Select All Columns
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:
Order of clause
select--------from------------where-------order by
The LIKE Condition
The LIKE condition is used to specify a search for a pattern in
a column.
Syntax
SELECT column FROM table
WHERE column LIKE pattern
The following SQL statement will return persons with first names
that start with an 'O':
SELECT * FROM Persons
WHERE FirstName LIKE 'O%'
The following SQL statement will return persons with first names
that end with an 'a':
SELECT * FROM Persons
WHERE FirstName LIKE '%a'
Using LIKE
The following SQL statement will return persons with first names
that contain atleast 3 characters
SELECT * FROM Persons
WHERE FirstName LIKE ‘___%‘;
The following SQL statement will return persons with first names that contain
third character to be ‘s’
• Sum()
• Avg()
• Max()
• Min()
• Count()
Aggregate functions
• Select sum(sal) from emp;
1. COMMIT
• To permanently save
• COMMIT;
2. SAVEPOINT
• To make markers in a lengthy transaction
• SAVEPOINT <savepoint name>;
3. ROLLBACK
• To undo changes till last commit
• ROLLBACK;
• To undo changes till a marker
• ROLLBACK <savepoint name>;