Professional Documents
Culture Documents
• Terminology:
– Table = Relation
– Row = Tuple
– Column = Attribute
• CREATE statement
– Main SQL command for data definition
The CREATE TABLE Command in SQL
Syntax Example
CREATE TABLE table_name CREATE TABLE Persons
( (
column_name1 data_type(size), PersonID int,
column_name2 data_type(size), LastName varchar(255),
column_name3 data_type(size), FirstName varchar(255),
.... Address varchar(255),
); City varchar(255)
);
Attribute Data Types and Domains in SQL
• Basic data types
– Numeric data types
• Integer numbers: INTEGER, INT, and SMALLINT
• Floating-point (real) numbers: FLOAT or REAL, and DOUBLE
PRECISION
– Character-string data types
• Fixed length: CHAR(n), CHARACTER(n)
• Varying length: VARCHAR(n), CHAR VARYING(n), CHARACTER
VARYING(n)
– Bit-string data types
• Fixed length: BIT(n)
• Varying length: BIT VARYING(n)
– Boolean data type
• Values of TRUE or FALSE or NULL
– DATE data type
• Ten positions
• Components are YEAR, MONTH, and DAY in the form YYYY-MM-DD
Attribute Data Types and Domains in SQL
• NOT NULL
– NULL is not permitted for a particular attribute
– CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
Specifying Constraints in SQL
• Default value
– DEFAULT <value>
– CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)
Add new column alter table Command
• UNIQUE clause
– Specifies alternate (secondary) keys
SELECT column_name,column_name
FROM table_name;
SELECT * FROM table_name;
The SELECT-FROM-WHERE Structure of Basic
SQL Queries
Query 1:
INSERT a record of a person in PERSON table
INSERT INTO
Customers (P_Id, FirstName, LastName, Address City)
VALUES (‘1”, ‘Zahra’,‘Zahid’,‘Sialkot’, ‘Sialkot’);
INSERT INTO
Orders (O_Id, OrderNo, P_Id)
VALUES (1, 1000, 1);
Basic Retrieval Queries in SQL
• SELECT statement
– One basic statement for retrieving information
from a database.
SELECT column_name,column_name
FROM table_name;
SELECT * FROM table_name;
The SELECT-FROM-WHERE Structure of Basic
SQL Queries (cont’d.)
Query 2:
Retrieve the person id, First name and City of Person(s)
whose Firs name is ‘Ahmed’
SELECT P_Id, FirstName, City
FROM PERSON
WHERE FirstName = ‘Ahmed’
Query 3:
Retrieve the person id, First name and City of Person(s)
whose id = ‘2’ and First name is ‘Ahmed’
SELECT P_Id, FirstName, City
FROM PERSON
WHERE P_Id = 2 AND FirstName = ‘Ahmed’
The UPDATE-WHERE Structure of Basic SQL Queries
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
Query 1:
Update First name and City of Person(s) where id is 3.
UPDATE PERSON
SET FirstName=‘Sabir', City=‘Lahore'
WHERE P_Id=‘3';
The DELETE Structure of Basic SQL Queries
DELETE FROM table_name
WHERE some_column=some_value;
Query 1:
Delete person whose id is 4
DELETE FROM PERSON
WHERE P_Id = 4
Comparisons Involving NULL
and Three-Valued Logic
• SUM
• AVG
• MIN
• MAX
• COUNT
Input to Aggregate Function
• SUM and AVG :
Operates only on collections of numbers .
Query:
Result:
sum_salary
54000.00
Revised Query for AVG()
Query:
Result :
avg_salary
11000.00
MIN() and MAX()
Returns: MIN() returns the smallest value of a column.
MAX() returns the largest value of a column.
Query:
Query:
Result:
sno_count
2
Use of COUNT() and SUM()
Example: Find the total number of Managers and the sum
of there salary.
Query:
Query:
Usage of Aggregation Functions
• Use of GROUP BY
• Use of HAVING
Use of GROUP BY
The GROUP BY statement is used in conjunction with
the aggregate functions to group the result-set by one or
more columns.
Result:
Example: For each branch office with more than one member
of staff, find the number of staff working in each
branch and the sum of their salaries.
Query: