Vous êtes sur la page 1sur 11

SQL

SQL stands for Structured Query Language


SQL is a database language
SQL is a standard language for accessing and manipulating databases
SQL is an ANSI (American National Standards Institute) standard
SQL was initially called as SEQUEL (Structure English QUEry Language)
SQL was first designed and implemeted at IBM as an interface for an experimental
relational database system called SYSTEM R

SQL can execute queries against a database


SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and views

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

Using SQL in Your Web Site


To build a web site that shows some data from a database, you will need the following:
• An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
• A server-side scripting language, like PHP or ASP
• SQL
• HTML / CSS

RDBMS

RDBMS stands for Relational Database Management System.


RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM
DB2, Oracle, MySQL, and Microsoft Access.
The data in RDBMS is stored in database objects called tables.
An RDBMS is acessed using SQL
A table is a collections of related data entries and it consists of columns and rows.
SQL
SQL uses the terms

table - relation
row - tuple
column - attribute

A table can have up to 254 columns

• char(n) - Fixed-length character data (string), n characters long. The maximum size for
n is 255 bytes (2000 in Oracle8). Note that a string of type char is always padded on
right with blanks to full length of n.(☞ can be memory consuming).
Example: char(40)

• varchar2(n) - Variable-length character string. The maximum size for n is 2000. Only the bytes
used for a string require storage. Example: varchar2(80)

• number(o, d) - Numeric data type for integers and reals. o indicatesoverall number of digits, d
indicates number of digits after the decimal point.
Maximum values: o =38, d= −84 to +127. Examples: number(8), number(5,2)
Note that, e.g., number(5,2) cannot contain anything larger than 999.99 without result-
ing in an error. Data types derived from number are int[eger], dec[imal], smallint
and real.

• date - Date data type for storing date and time.


The default format for a date is: DD-MMM-YY. Examples: ’13-OCT-94’, ’07-JAN-98’

• long - Character data up to a length of 2GB. Only one long column is allowed per table.

Note: In Oracle-SQL there is no datatype boolean. It can, however, be simulated by using


either char(1) or number(1).
SQL
SELECT column_name(s)
FROM table_name

SELECT LastName,FirstName FROM Persons

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

SELECT DISTINCT column_name(s)


FROM table_name

SELECT DISTINCT City FROM Persons

SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC

SELECT * FROM Persons


ORDER BY LastName

SELECT * FROM Persons


ORDER BY LastName DESC

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

SELECT * FROM Persons


WHERE City='Sandnes'

SELECT * FROM Persons


WHERE FirstName='Tove'
AND LastName='Svendson'

SELECT * FROM Persons


WHERE FirstName='Tove'
OR FirstName='Ola'
(DOW)
SQL
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'

INSERT INTO table_name


VALUES (value1, value2, value3,...)

INSERT INTO Persons


VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger')

INSERT INTO table_name (column1, column2, column3,...)


VALUES (value1, value2, value3,... )

INSERT INTO Persons (P_Id, LastName, FirstName)


VALUES (5, 'Tjessem', 'Jakob'

DELETE FROM table_name


WHERE some_column=some_value

DELETE FROM Persons


WHERE LastName='Tjessem' AND FirstName='Jakob'

DELETE FROM table_name


or
DELETE * FROM table_name (UID)

NOTE-
Both the statements below are same

SELECT person.contact.lastname FROM person.contact


SELECT lastname FROM person.contact
SQL
Operator

> Greater than


SELECT firstname FROM person.contact WHERE firstname > 'C'

IN If you know the exact value you want to return for at least one of the columns

LIKE OPERATOR
SELECT column_name
FROM table_name
WHERE column_name LIKE 's%'

SELECT firstname
FROM person.contact
WHERE firstname LIKE 's%'
(Here we select only firstname that starts with s)

Substitues
% A substitute for zero or more characters (wild card)
_ A substitute for exactly one character(position marker)
[charlist] Any single character in charlist
[!charlist] Any single character in charlist here it doesn't start with

Now we want to select the persons with a last name that starts with "b" or "s" or "p" from the
"Person.contact" table.
SELECT * FROM Person.contact
WHERE LastName LIKE '[bsp]%'
Next, we want to select the persons with a last name that do not start with "b" or "s" or "p" from
the "Person.contact" table.
SELECT * FROM Person.contact
WHERE LastName LIKE '[!bsp]%'

BETWEEN OPERATOR

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2

SELECT *
FROM person.contact
WHERE contactid between 1 and 50

To display the persons outside the range in the previous example, use NOT BETWEEN:
NOT BETWEEN '400' AND '800'
SQL
IS NULL OPERATOR AND IS NOT NULL

It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.

SELECT Address FROM Persons.Contact


WHERE Address IS NUL
The above displays the column Address which have null value.

SELECT Address FROM Persons.Contact


WHERE Address IS NOT NULL
The above displays the column Address which doesn have null value.
SQL
SQL TOP

SELECT TOP number *


FROM table_name

SELECT TOP 2 *
FROM person.contact

(Here it can be noted that only first 2 is selected)

SELECT TOP 50 PERCENT *


FROM person.contact

(The above is to select 50 percent of the table)


SQL
SQL ALIAS
We can give a table or a column another name by using an alias.

Syntax for aliasing table


SELECT column_name(s)
FROM table_name AS alias_name

Syntax for aliasing Column


SELECT column_name AS alias_name
FROM table_name
SQL
CREATING TABLES
The SQL command for creating an empty table has the following form:

CREATE TABLE database_name.schema_name. table_name


(
column_name1 data_type NOT NULL PRIMARY KEY,
column_name2 data_type COLUMN CONSTAINT,
column_name3 data_type,
....
);

ALTERING TABLES
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table

ALTER TABLE database_name.schema_name. table_name


ADD/DROP CONSTRAINT

To add a column in a table,thesyntax is:


ALTER TABLE table_name
ADD column_name datatype

To delete a column in a table,the syntax is:


ALTER TABLE table_name
DROP COLUMN column_name

To change the data type of a column in a table,the syntax is:


ALTER TABLE table_name
ALTER COLUMN column_name datatype
SQL
Constraints
Constraints are keywords that defines the rules for columns
Constraints define rules regarding the values allowed in columns and are the standard mechanism
for enforcing integrity.
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).
Types of Constraints-
• NOT NULL
• UNIQUE
• PRIMARY KEY
• FOREIGN KEY
• CHECK
• DEFAULT

NOT NULL constraint specifies that no row of a column has NULL values
The NOT NULL constraint enforces a field to always contain a value. This means that you cannot
insert a new record, or update a record without adding a value to this field.

UNIQUE constraint specifies that no two rows of a column has same value.
We can use UNIQUE constraints to make sure that no duplicate values are entered in specific
columns.

PRIMARY KEY constraint uniquely identifies each record in a database table.


Primary keys must contain UNIQUE values.
A primary key column cannot contain NULL values.
Each table should have a PRIMARY KEY, and each table can have only one primary key.
PRIMARY KEY constraints relate one table to another

FOREIGN KEY

CHECK constraint allows a condition to be checked for when data in that column is updated or
inserted; for example, CHECK (PRICE > 0) causes the system to check that the Price column is
greater than zero before accepting the value...sometimes implemented as the CONSTRAINT
statement. CHECK(salary >= 15k AND salary <=19k)

DEFAULT constraint inserts the default value into the database if a row is inserted without that
column's data being inserted; for example, benefits integer DEFAULT = 10000

FOREIGN KEY REFERENCES Is a constraint that provides referential integrity for the data in
the column or columns. FOREIGN KEY constraints require that each value in the column exists in
the corresponding referenced column or columns in the referenced table. FOREIGN KEY
constraints can reference only columns that are PRIMARY KEY or UNIQUE constraints in the
referenced table or columns referenced in a UNIQUE INDEX on the referenced table. Foreign
keys on computed columns must also be marked PERS
SQL
Examples-

CREATE TABLE Persons


(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL UNIQUE ,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);

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)
);

Vous aimerez peut-être aussi