SQL
SQL
SQL
SQL can be divided into two parts: The Data Manipulation Language (DML) and the Data
Definition Language (DDL).
RDBMS
table - relation
row - tuple
column - attribute
• 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.
• long - Character data up to a length of 2GB. Only one long column is allowed per table.
SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
UPDATE Persons
SET Address='Nissestien 67', City='Sandnes'
WHERE LastName='Tjessem' AND FirstName='Jakob'
NOTE-
Both the statements below are same
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 TOP 2 *
FROM person.contact
ALTERING TABLES
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table
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.
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-