SQL Introduction
SQL Introduction
SQL Introduction
Query Language
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.
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;
FoodCart
FoodCart
date
food
sold
02/25/08 pizza 350
02/26/08 hotdog 500
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
350
500
70
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
350
500
70
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
349
500
70
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
349
500
70
date
food
02/25/08 pizza
02/26/08 pizza
sold
349
70
Note: If the WHERE clause is omitted all rows of data are deleted from the table.
SQL Constraints
Constraints
Database Constraints
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.
Dec( 7, 0 )
Not Null
Date
Not Null,
ShipDate
Date
Default Null,
SaleTot
Dec( 7, 2 )
Not Null,
CrdAutNbr
Int
CustID
Dec( 7, 0 )
Default Null,
Not Null,
Unique ( CrdAutNbr ),
Constraint SaleCustomerFK
References Customer
( CustID )
On Delete Cascade
On Update Restrict,
Constraint SaleShipDateChk Check( ShipDate Is Null
Or ShipDate >= SaleDate ) )
A primary key serves as the unique identifier for rows in the table.
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
Note that a unique constraint does not use the Key keyword, as do
primary key and foreign key constraints.
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.
Within SQL we might create the Customer and Sale tables so they have
the following partial constraint definitions:
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.
SaleCustomerFK
References Customer
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
MgrEmpID
Check Constraints
[guarantees that the OrderID primary key column is always greater than
zero]
[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].
You can combine check constraints for more than one column into a single
check constraint, as in the following example:
Constraint CustStatusNameChk
Check ( (
And
))
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
Add/Remove Constraints
SQL: SELECT
Statement
A basic SELECT statement includes 3 clauses
SELECT <attribute name> FROM <tables> WHERE <condition>
SELECT
FROM
WHERE
Specifies the
attributes that are
part of the
resulting relation
Specifies the
tables that serve
as the input to the
statement
Specifies the
selection condition,
including the join
condition.
Example:
Person
1) SELECT *
FROM person
WHERE age > 30;
Name
Age
Weight
Harry
34
80
Name
Age
Weight
Sally
28
64
Harry
34
80
George
29
70
Helena
54
54
Helena
54
54
Peter
34
80
Peter
34
80
2) SELECT weight
FROM person
WHERE age > 30;
Weight
Weight
80
80
54
54
80
ID
1000
1001
1002
Dept
State
CA
MA
TN
ID
1001
1002
1003
Division
IT
Sales
Biotech
Emp.ID
1000
1001
1002
Emp.State
CA
MA
TN
Dept.ID
null
1001
1002
Dept.Division
null
IT
Sales
Emp.ID
1001
1002
null
Emp.State
MA
TN
null
Dept.ID
1001
1002
1003
Dept.Division
IT
Sales
Biotech
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
349
500
70
food
totalSold
hotdog 500
pizza 419
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
349
500
70
food
totalSold
hotdog 500
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
349
500
70
date
02/25/08
02/26/08
02/26/08
food
pizza
hotdog
pizza
sold
349
500
70
Thanks