2. What is SQL
Structured Query Language
Communicate with databases
Used to created and edit databases
Also used to create queries ,forms and reports
3. History of SQL
SQL: Structured Query Language
SQL is based on the relational tuple calculus
SEQUEL: Structured English Query Language; part of
SYSTEM R, 1974
SQL/86: ANSI & ISO standard
SQL/89: ANSI & ISO standard
SQL/92 or SQL2: ANSI & ISO standard
SQL3: in the works...
SQL2 supported by ORACLE, SYBASE, INFORMIX,
IBM DB2, SQL SERVER, OPENINGRES,...
4. SQL
SQL consists of the following parts:
Data Definition Language (DDL)
Interactive Data Manipulation Language (Interactive
DML)
Embedded Data Manipulation Language (Embedded
DML)
Views
Integrity
5. SQL Commands
The standard SQL COMMANDS are CREATE ,SELECT,
INSERT,UPDATE,DELETE and DROP.
DDL
Command Description
CREATE creates a new table or other object in database
ALTER Modifying an existing database object
DROP Deletes an entire table
6. DML (Data Manipulation Language)
Command Description
INSERT Creates a record
UPDATE Modifies records
DELETE Deletes records
8. SQL
Data Definition Language (DDL)
Create/alter/delete tables and their attributes
Following lectures...
Data Manipulation Language (DML)
Query one or more tables – discussed next !
Insert/delete/modify tuples in tables
9. PRIMARY Key:
A Primary key is a field in a table uniquely
Identifies each row/record in a database table. Primary
Keys must contains unique values.
A table can have only
one primary key, which may consist of single or multiple
fields. When multiple fields are used as a primary key,
they are called a ‘composite key’.
11. FOREIGN Key:
A foreign key is a key used to link two
tables together. This is sometimes called a
referencing key.
Foreign Key is a column or
a combination of columns whose values
match a Primary Key in a different table.
13. Data Integrity:
Entity Integrity : There are no duplicate rows in a
table.
Domain Integrity : Enforces valid entries for a
given column by restricting the type, the format,
or the range of values.
Referential Integrity : Rows cannot be deleted
which are used by other records.
User-Defined Integrity : Enforces some specific
business rules that do not fall into entity, domain,
or referential integrity.
15. Database Normalization
Database normalization is the process of
efficiently organizing data in a database. There are
two reasons of the normalization process:
Eliminating redundant data, for example, storing
the same data in more than one table.
Ensuring data dependencies make sense.
16. Types of Normalization
First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
17. First Normal Form:
Define the data items required, because
they become the columns in a table. Place
related data items in a table.
Ensure that there are no repeating groups
of data.
Ensure that there is a primary key.
18. Second Normal Form:
Second normal form states that it should meet
all the rules for 1NF and there must be no partial
dependences of any of the columns on the primary key.
Third Normal Form:
A table is in third normal form when the following
conditions are met:
It is in second normal form.
All nonprimary fields are dependent on the primary key.
19. SQL Data types
String types
CHAR(n) – fixed-length character data, n characters long
Maximum length = 2000 bytes
VARCHAR2(n) – variable length character data, maximum
4000 bytes
LONG – variable-length character data, up to 4GB. Maximum
1 per table
Numeric types
NUMBER(p,q) – general purpose numeric data type
INTEGER(p) – signed integer, p digits wide
FLOAT(p) – floating point in scientific notation with p binary
digits precision
Date/time type
DATE – fixed-length date/time in dd-mm-yy form
20. Available statements
SQL (1/2)
Giacomo.Govi@cern.ch 20
Statement Description
SELECT Data retrieval
INSERT
UPDATE
DELETE
Data Manipulation Language (DML)
CREATE
ALTER
DROP
RENAME
TRUNCATE
Data Definition Language (DDL)
COMMIT
ROLLBACK
SAVEPOINT
Transaction Control
GRANT
REVOKE
Data Control Language (DCL)
Rows
Tables/Objects
Manages
DML
21. SQL & Tools
SQL statements can be submitted via:
DB API’s for programming languages (C, C++, Java,
Python, PHP, …)
GUI applications (Excel, Access)
stored procedures (PL/SQL, Java)
Oracle tools (Reports, Forms, Designer…)
SQL*Plus (Oracle!) is the basic tool to submit
SQL commands (available on all CERN
platforms).
22. Summary
What is SQL
History of SQL
SQL Commands
PRIMARY Key
Data Integrity
Database Normalization
SQL Data types