SQL, the Structured Query Language
SQL, the Structured Query Language
The ANSI standard language for the definition and manipulation of relational database.
Includes data definition language (DDL), statements that specify and modify database schemas.
Includes a data manipulation language (DML), statements that manipulate database content.
Structured Query Language (SQL)
Some Facts on SQL SQL data is case-sensitive, SQL commands are
not. First Version was developed at IBM by Donald D.
Chamberlin and Raymond F. Boyce. [SQL]
SQL query includes references to tuples variables and the attributes of those variables
Introduction to SQL Server System Every SQL Server relies on four
primary system databases, each of which must be present for the server to operate effectively
Master database
The master database stores basic configuration information for the server. This includes information about the file locations of the user databases, as well as logon accounts, server configuration settings, and a number of other items such as linked servers and startup stored procedures
Model Database
The model database is a template database that is copied into a new database whenever it is created on the instance.
Database options set in model will be applied to new databases created on the instance,
Msdb database
The msdb database is used to support a number of technologies within SQL Server, including the SQL Server Agent, SQL Server Management Studio, Database Mail, and Service Broker.
Msdb database
A great deal of history and metadata information is available in msdb, including the backup and restore history for the databases on the server
Tempdb
The tempdb system databases is a shared temporary storage resource used by a number of features of SQL Server, and made available to all users.
Tempdb is used for temporary objects, worktables, online index operations, cursors, table variables
Tempdb
It is recreated every time that the server is restarted, which means that no objects in tempdb are permanently stored.
SQL: DDL Commands CREATE TABLE: used to create a table. ALTER TABLE: modifies a table after it was
created.
DROP TABLE: removes a table from a database.
SQL: CREATE TABLE Statement Things to consider before you create your table
are:The type of datathe table namewhat column(s) will make up the primary keythe names of the columns
CREATE TABLE statement syntax:CREATE TABLE <table name>( field1 datatype ( NOT NULL ), field2 datatype ( NOT NULL ));
SQL: Attributes Types
SQL: ALTER TABLE Statement To add or drop columns on existing tables. ALTER TABLE statement syntax:
ALTER TABLE <table name>ADD attr datatype;orDROP COLUMN attr;
SQL: DROP TABLE StatementHas two options: CASCADE: Specifies that any foreign key
constraint violations that are caused by dropping the table will cause the corresponding rows of the related table to be deleted.
RESTRICT: blocks the deletion of the table of any foreign key constraint violations would be created.
DROP TABLE statement syntax:DROP TABLE <table name> [ RESTRICT|CASCADE ];
Example:CREATE TABLE FoodCart (date varchar(10),food varchar(20),profit float);
ALTER TABLE FoodCart (ADD sold int);
ALTER TABLE FoodCart(DROP COLUMN profit);
DROP TABLE FoodCart;
profitfooddate
soldprofitfooddate
soldfooddate
FoodCart
FoodCart
FoodCart
SQL: DML Commands INSERT: adds new rows to a table. UPDATE: modifies one or more attributes.
DELETE: deletes one or more rows from a table.
SQL: INSERT Statement To insert a row into a table, it is necessary to
have a value for each attribute, and order matters. INSERT statement syntax:
INSERT into <table name>VALUES ('value1', 'value2', NULL);Example: INSERT into FoodCart
VALUES (’02/26/08', ‘pizza', 70 );FoodCart
70pizza02/26/08500hotdog02/26/08350pizza02/25/08soldfooddate
500hotdog02/26/08350pizza02/25/08soldfooddate
SQL: UPDATE Statement To update the content of the table:
UPDATE statement syntax:UPDATE <table name> SET <attr> = <value>WHERE <selection condition>;Example: UPDATE FoodCart SET sold = 349
WHERE date = ’02/25/08’ AND food = ‘pizza’;FoodCart
70pizza02/26/08500hotdog02/26/08350pizza02/25/08soldfooddate
70pizza02/26/08500hotdog02/26/08349pizza02/25/08soldfooddate
SQL: DELETE Statement To delete rows from the table:
DELETE statement syntax:DELETE FROM <table name>WHERE <condition>;Example: DELETE FROM FoodCart
WHERE food = ‘hotdog’;FoodCart
Note: If the WHERE clause is omitted all rows of data are deleted from the table.70pizza02/26/08500hotdog02/26/08349pizza02/25/08soldfooddate
70pizza02/26/08349pizza02/25/08soldfooddate
SQL Constraints
Constraints
Database constraints are restrictions on the contents of the database or on database operations
Database constraints provide a way to guarantee that:
rows in a table have valid primary or unique key values
rows in a dependent table have valid foreign key values that reference rows in a parent table
individual column values are valid
Database Constraints SQL/400 constraints cover four specific integrity rules:
primary key constraint (to enforce existence integrity)
unique constraint (to enforce candidate key integrity)
foreign key constraint (to enforce foreign key, or referential integrity)
check constraint (to restrict a column's values; a partial enforcement of domain integrity)
You specify one or more of these constraints when you use the Create Table statement to create a base table.
You can also add or drop constraints with the Alter Table statement.
Create Table Sale ( OrderID Dec( 7, 0 ) Not Null Constraint SaleOrderIdChk Check( OrderID > 0 ), SaleDate Date Not Null, ShipDate Date Default Null, SaleTot Dec( 7, 2 ) Not Null, CrdAutNbr Int Default Null, CustID Dec( 7, 0 ) Not Null, Primary Key( OrderID ), Constraint SaleCrdAutNbrUK Unique ( CrdAutNbr ), Constraint SaleCustomerFK Foreign Key ( CustID ) References Customer ( CustID ) On Delete Cascade On Update Restrict, Constraint SaleShipDateChk Check( ShipDate Is Null
Or ShipDate >= SaleDate ) )
Primary Key Constraints A primary key serves as the unique identifier for rows in the table.
The syntax of the primary key constraint (following the Constraint keyword and the constraint name, if they're specified) is
Primary Key( column-name, ... )
Each primary key column's definition must include Not Null.
For a table with a primary key constraint, UDB/400 blocks any attempt to insert or update a row that would cause two rows in the same table to have identical value(s) for their primary key column(s).
A table definition can have no more than one primary key constraint.
Unique Constraints A unique constraint is similar to a primary key constraint
doesn't have to be defined with Not Null.
Recommend always specify a constraint name for a unique constraint: Constraint constraint-name Unique ( column-name, ... )
Note that a unique constraint does not use the Key keyword, as do primary key and foreign key constraints.
The SaleCrdAutNbrUK unique constraint specifies that any non-null value for the CrdAutNbr column must be unique.
Allowing the CrdAutNbr column to be null and specifying the SaleCrdAutNbrUK constraint together enforce a business rule that some orders (e.g., those paid by cash) may exist without a credit authorization number, but any order that does have a credit authorization number must have a unique value.
Foreign Key Constraints A foreign key constraint specifies how records in different
tables are related and how UDB/400 should handle row insert, delete, and update operations that might violate the relationship.
For example, sales rows are generally related to the customers who place the orders. Although it might be valid for a customer row to exist without any corresponding sale rows, it would normally be invalid for a sale row not to have a reference to a valid customer.
With a relational DBMS, the relationship between rows in two tables is expressed by a foreign key in the dependent table. A foreign key is one or more columns that contain a value identical to a primary key (or unique key) value in some row in the parent table (i.e., the referenced table).
Within SQL we might create the Customer and Sale tables so they have the following partial constraint definitions:
Customer table (parent)
Primary key column: CustID
Sale table (dependent)
Primary key column: OrderID
Foreign key column: CustID
For each row in the Sale table, the CustID column should contain the same value as the CustID column of some Customer row because this value tells which customer placed the order.
The purpose of specifying a foreign key constraint is to have UDB/400 ensure that the Sale table never has a row with a (non-null) value in the CustID column that has no matching Customer row.
Foreign Key Constraints cont.
The Sale table's foreign key constraint, which is
Constraint SaleCustomerFK Foreign Key ( CustID ) References Customer ( CustID ) On Delete Cascade On Update Restrict
Specifies that the CustID column in the Sale table is a foreign key that references the CustID primary key column in the Customer table.
Blocks any attempt to change the CustID column of a row in the Sale table to a value that doesn't exist in any row in the Customer table. [a new or updated Sale row must have a parent Customer row].
A foreign key constraint can specify the same table for the dependent and parent tables. Suppose you have an Employee table with an EmpID primary key column and a MgrEmpID column that holds the employee ID for the person's manager. :
Create Table Employee ( EmpID Dec ( 7, 0 ) Not Null, MgrEmpID Dec ( 7, 0 ) Not Null, other column definitions ... , Primary Key ( EmpID ), Constraint EmpMgrFK Foreign Key ( MgrEmpID ) References Employee ( EmpID ) On Update Restrict On Delete Restrict )
Check Constraints Used to enforce the validity of column values.
Constraint SaleOrderIdChk Check( OrderID > 0 ) [guarantees that the OrderID primary key column is always greater
than zero]
Constraint SaleShipDateChk Check( ShipDate Is Null Or ShipDate >= SaleDate )
[guarantees that either a row has no ship date (i.e., the ShipDate column is null, meaning "unknown") or the ship date is on or after the sale date].
A check constraint can compare a column to a constant (such as in the first example), to another column in the same table (such as in the second example), or to an expression (e.g., ColA + 3).
check constraints cont.
You can combine check constraints for more than one column into a single check constraint, as in the following example:
Constraint CustStatusNameChk Check ( ( Status = 'A' Or Status = 'I' ) And ( Name <> ' ' ) )
Add/Remove Constraints
After you create a table, you can use the Alter Table statement to add or remove a primary key, unique, foreign key, or check
constraint
To drop a table's primary key constraint, just specify the Primary Key keywords:
Alter Table Sale Drop Primary Key
Add/Remove Constraints To drop a unique, foreign key, or check constraint, you
must specify the constraint name:
Alter Table Sale Drop Constraint SaleCustomerFK To add a new constraint, use the same constraint
syntax as in a Create Table statement: Alter Table Sale Add Constraint SaleSaleTotChk
Check( SaleTot >= 0 )
SQL Statements, Operations, Clauses SQL Statements:
Select SQL Operations:
JoinLeft JoinRight JoinLike
SQL Clauses:Order ByGroup ByHaving
SQL: SELECT Statement A basic SELECT statement includes 3 clauses
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECTSpecifies the attributes that are part of the resulting relation
FROMSpecifies the tables that serve as the input to the statement
WHERESpecifies the selection condition, including the join condition.
Note: that you don't need to use WHERE
Using a “*” in a select statement indicates that every attribute of the input table is to be selected.
Example: SELECT * FROM … WHERE …;
To get unique rows, type the keyword DISTINCT after SELECT.
Example: SELECT DISTINCT * FROM … WHERE …;
SQL: SELECT Statement (cont.)
Example:Person
8034Peter5454Helena7029George6428Sally8034HarryWeightAgeName
8034Peter5454Helena8034HarryWeightAgeName
80
54
80
Weight
1) SELECT * FROM person WHERE age > 30;
2) SELECT weight FROM person WHERE age > 30;
3) SELECT distinct weight FROM person WHERE age > 30;
54
80Weight
SQL: Join operationA join can be specified in the FROM clause which list the two input relations and the WHERE clause which lists the join condition.Example:
Biotech1003Sales1002IT1001DivisionID
TN1002MA1001CA1000StateID
Emp Dept
SQL: Join operation (cont.)
Sales1002IT1001Dept.DivisionDept.ID
TN1002MA1001Emp.StateEmp.ID
inner join = joinSELECT *FROM emp join dept (or FROM emp, dept)on emp.id = dept.id;
SQL: Join operation (cont.)
IT1001Sales1002
nullnullDept.DivisionDept.ID
CA1000
TN1002MA1001
Emp.StateEmp.ID
left outer join = left joinSELECT *FROM emp left join depton emp.id = dept.id;
SQL: Join operation (cont.)
Sales1002Biotech1003
IT1001Dept.DivisionDept.ID
MA1001
nullnullTN1002
Emp.StateEmp.ID
right outer join = right joinSELECT *FROM emp right join depton emp.id = dept.id;
SQL: Like operationPattern matching selection
% (arbitrary string)SELECT *FROM emp WHERE ID like ‘%01’; finds ID that ends with 01, e.g. 1001, 2001, etc_ (a single character)SELECT *FROM emp WHERE ID like ‘_01_’; finds ID that has the second and third character as 01, e.g. 1010, 1011, 1012, 1013, etc
SQL: The ORDER BY ClauseOrdered result selection
desc (descending order)SELECT *FROM emp order by state desc puts state in descending order, e.g. TN, MA, CAasc (ascending order)SELECT *FROM emp order by id asc puts ID in ascending order, e.g. 1001, 1002, 1003
SQL: The GROUP BY ClauseThe function to divide the tuples into groups and
returns an aggregate for each group.Usually, it is an aggregate function’s companionSELECT food, sum(sold) as totalSoldFROM FoodCart group by food; FoodCart
419pizza500hotdogtotalSoldfood
70pizza02/26/08500hotdog02/26/08349pizza02/25/08soldfooddate
SQL: The HAVING ClauseThe substitute of WHERE for aggregate functionsUsually, it is an aggregate function’s companionSELECT food, sum(sold) as totalSoldFROM FoodCart group by food having sum(sold) > 450;FoodCart
500hotdogtotalSoldfood
70pizza02/26/08500hotdog02/26/08349pizza02/25/08soldfooddate
SQL: Aggregate FunctionsAre used to provide summarization information
for SQL statements, which return a single value.
COUNT(attr) SUM(attr) MAX(attr) MIN(attr) AVG(attr)
Note: when using aggregate functions, NULL values are not considered, except in COUNT(*) .
SQL: Aggregate Functions (cont.)
COUNT(attr) -> return # of rows that are not nullEx: COUNT(distinct food) from FoodCart; -> 2 SUM(attr) -> return the sum of values in the attr
Ex: SUM(sold) from FoodCart; -> 919 MAX(attr) -> return the highest value from the attr
Ex: MAX(sold) from FoodCart; -> 500
70pizza02/26/08500hotdog02/26/08349pizza02/25/08soldfooddate
FoodCart
SQL: Aggregate Functions (cont.)
MIN(attr) -> return the lowest value from the attrEx: MIN(sold) from FoodCart; -> 70 AVG(attr) -> return the average value from the attr
Ex: AVG(sold) from FoodCart; -> 306.33Note: value is rounded to the precision of the datatype
70pizza02/26/08500hotdog02/26/08349pizza02/25/08soldfooddate
FoodCart
Thanks