Basic SQL Server 2000 DDL and DML
Basic SQL Server 2000 DDL and DML
1
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
Here the following characters are not allowed to be used for object naming.
(+,-,/,>,<,\,!,*,%,$,#,(,) , .,? white space etc)
Simple syntax
2
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
The above table (Department) will consider DID as a primary key and Name can
not take null and Loc has no constraints.
The above table (Loan) will consider MID, CID and BDate together as a primary
key.
3
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
4
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
or
After this instruction is executed, the employee table will have the
following schema.
EID FName LName Salary DoB
In the next table, the primary key are (MID,CID,BDate) with constraint name
Pk_Loan. Then, if we want to delete the MID attribute, we can not simply
drop it before lifting the constraint on it. Thus first the Pk_Loan should be
deleted and then MID can be deleted.
6
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
For multiple tuple insertion, one has to repeatedly execute the above code
with different values.
11. Add Constraint to an existing table
After a table is created one can add constraints or delete constraints to the
existing table. Constraints might be: Primary Key, Foreign Key, Null, Check,
etc.
The general syntax would be:
ALTER TABLE <table name> ADD CONSTRAINT <constraint name> <constraint>
7
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
8
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
In the select statement inbuilt functions like count (COUNT), maximum (MAX),
minimum (MIN), average (AVG), total (SUM), etc can be used, where in such
cases the group by phrase will be used.
To displays the maximum salary from each department
SELECT DID, MAX(salary) FROM Employee
GROUP BY EDID
We can also sort the result using one of the attributes in the result using the
ORDER BY clause.
SELECT * FROM Employee
WHERE Salary>1000
ORDER BY FName
If we use the delete statement without the where clause (without the
condition), all tuples of the table will be deleted and we will have an empty
table.
DELETE employee
The above command will delete all the tuples in the employee table but NOT
the table.
To increase the salary of all employees earning a slary less than 1000 by 25%
UPDATE Employee
SET Salary=Salary*1.25
WHERE salary<1000
General Syntax
CREATE VIEW <View_Name> AS
SELECT <attribute List>
FROM <Table_Name>
WHERE <Condition>
Note: The database does not store the view data! The database engine
recreates the data, using the view's SELECT statement, every time a user
queries a view.
To create a virtual table or a view which will display the list of employees
whose salary is greater than 100, the view will be:
CREATE VIEW Emp_Sal_1000 AS
SELECT *
FROM Employee
After this query is executed, we can run the following query, as it acts just
like a normal table in the database.
SELECT * FROM Emp_Sal_1000
Generally, any select statement can be used to from a view, where then after
the view can be used as a normal table. A view can also be used in other
queries as a normal table.
11
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
12
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
3. Right Join
Joins the tables using equality between the attributes of primary key
and foreign key columns and also includes tuples from the table in
the right side in the result even if they don’t participate in the
relationship. For attributes from the table in the left side the value
‘null’ will be field for those tuples from the right side not
participating in the relationship.
SELECT <attribute List>
FROM <table1> RIGHT JOIN <table2>
ON <Join condition>
WHERE <additional Condition>
4. Full Join
Joins the tables using equality between the attributes of primary key
and foreign key columns and also includes tuples from both tables
in the result even if they don’t participate in the relationship. For
attributes from the table in the left side the value ‘null’ will be field
for those tuples from the table in the right side not participating in
the relationship. And For attributes from the table in the right side
the value ‘null’ will be field for those tuples from the table in the left
side not participating in the relationship.
13
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
DEPARTMENT
DID DName DLoc
D01 Finanace B05
D02 Personnel B04
D03 sales B03
D04 Project B07
D05 Mang B01
¾ Inner Join
SELECT * FROM
employee INNER JOIN department
ON employee.edid=department.did
Or using the alias method
SELECT * FROM
employee E INNER JOIN department D
ON E.edid=D.did
The above two SQL queries will produce the following table
14
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
SELECT * FROM
employee LEFT OUTER JOIN department
ON employee.edid=department.did
SELECT * FROM
employee E LEFT JOIN department D
ON E.edid=D.did
SELECT * FROM
employee E LEFT OUTER JOIN department D
ON E.edid=D.did
The above four SQL queries will produce the following table
15
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
SELECT * FROM
employee RIGHT OUTER JOIN department
ON employee.edid=department.did
SELECT * FROM
employee E RIGHT JOIN department D
ON E.edid=D.did
SELECT * FROM
employee E RIGHT OUTER JOIN department D
ON E.edid=D.did
The above four SQL queries will produce the following table
EID FName LName Salary EDID DID DName DLoc
e01 Abebe Taye 1500.0000 d01 d01 Finanace B05
e04 hirut seleshi 350.0000 d01 d01 Finanace B05
e03 Almaz mitiku 700.0000 d02 d02 Personnel B04
e02 bekele mamo 230.0000 d03 d03 sales B03
e05 mammo mesfin 875.0000 d03 d03 sales B03
NULL NULL NULL NULL NULL d04 Project B07
NULL NULL NULL NULL NULL d05 Mang B01
16
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])
SQL Server 2000 Lab Note
SELECT * FROM
employee FULL OUTER JOIN department
ON employee.edid=department.did
SELECT * FROM
employee E FULL JOIN department D
ON E.edid=D.did
SELECT * FROM
employee E FULL OUTER JOIN department D
ON E.edid=D.did
The above four SQL queries will produce the following table
EID FName LName Salary EDID DID DName DLoc
e06 mahlet takele 1250.0000 NULL NULL NULL NULL
e07 belay Kebede 1100.0000 NULL NULL NULL NULL
e01 Abebe Taye 1500.0000 d01 d01 Finanace B05
e04 hirut seleshi 350.0000 d01 d01 Finanace B05
e03 Almaz mitiku 700.0000 d02 d02 Personnel B04
e02 bekele mamo 230.0000 d03 d03 sales B03
e05 mammo mesfin 875.0000 d03 d03 sales B03
NULL NULL NULL NULL NULL d04 Project B07
NULL NULL NULL NULL NULL d05 Mang B01
17
Prepared By: Wondwossen Mulugeta, Faculty of Informatics, AAU ([email protected])