Database Lab Manual
Database Lab Manual
1. Developer – It has all feature which MS SQL server offers but we cannot
use it in production. From the learning perspective, is it an ideal candidate
to start.
2. Express: This is also a free SQL server download version but with the
limited set of features with no business intelligence applications.
We will select the Developer edition MS SQL server download for installation.
Click Install.
5. Once SSMS is successfully installed, click Restart.
1.5 Connect to SQL Server Database Engine
1. Click Start and search Microsoft SQL Server Management Studio
or SSMS.
2. From the Object Explorer panel on the left, click Connect.
3. In the Connect to Server dialog box, click Connect with the
default server values.
2.3 DBMDs
There are lots of different database systems, or DBMS – Database Management
Systems,
such as:
Microsoft SQL Server
Oracle
MySQL
Microsoft Access
IBM DB2
Sybase
Example:
To create database called ‘Testdb’, run the following query.
Example :
To remove database name ‘Testdb’, run the following query.
Syntax:
The data type specifies what type of data the column can hold.
You have special data types for numbers, text dates, etc.
Examples:
• Numbers: int, float
• Text/Stings: varchar(X) – where X is the length of the string
• Dates: datetime
• etc.
Example:
Next, the table designer pops up where you can add columns, data types, etc.
In this designer we may also specify Column Names, Data Types, etc. Finally,
Save the table by clicking the Save button.
4.2 ALTER TABLE
The ALTER TABLE statement is used to add, delete, or modify columns in an
existing table.
To add a column in a table, use the following syntax:
To change the data type of a column in a table, use the following syntax:
The primary Key column will then have a small key in front to illustrate that
this column is a Primary Key.
Example:
5.2 UPDATE
The UPDATE statement is used to update existing records in a table.
The syntax is as follows:
Note: Notice the WHERE clause in the UPDATE syntax. The WHERE clause
specifies which record or records that should be updated. If you omit the
WHERE clause, all records will be updated!
Example:
5.3 DELETE
The DELETE statement is used to delete rows in a table.
Syntax:
Note: Notice the WHERE clause in the DELETE syntax. The WHERE clause
specifies which record or records that should be deleted. If you omit the WHERE
clause, all records will be deleted!
Example:
Question 1:
Consider the following UNIVERSITY schema, where the primary keys are
underlined
a. create a database using SQL Server DBMS:
Solution:
create table classroom
b. Provide sample data for each of the relations defined in the previous section.
Chapter 6: SELECT Statement
Example 1:
selects the "CustomerName" and "City" columns from the "Customers" table
Example 2:
Selects all the columns from the "Customers" table
Syntax:
The following SQL statement selects all (including the duplicates) values from
the "Country" column in the "Customers" table.
The following SQL statement selects only the DISTINCT values from the
"Country" column in the "Customers" table:
The WHERE clause is used to filter records. It is used to extract only those records
that fulfill a specified condition.
Syntax:
Example:
Selects all the customers from the country "Mexico", in the "Customers" table
The WHERE clause can be combined with AND, OR, and NOT operators.
The AND and OR operators are used to filter records based on more than one
condition:
AND Syntax
OR Syntax
NOT Syntax
AND Example
selects all fields from "Customers" where country is "Germany" AND city is
"Berlin"
OR Example
selects all fields from "Customers" where city is "Berlin" OR "München"
Example
selects all fields from "Customers" where country is "Germany" OR "Spain"
NOT Example
selects all fields from "Customers" where country is NOT "Germany"
Example:
selects all fields from "Customers" where country is "Germany" AND city must
be "Berlin" OR "München"
Example:
selects all fields from "Customers" where country is NOT "Germany" and NOT
"USA"
6.5 SQL ORDER BY Keyword
Syntax:
Example:
selects all customers from the "Customers" table, sorted by the "Country" column
Example :
selects all customers from the "Customers" table, sorted DESCENDING by the
"Country" column
Example:
selects all customers from the "Customers" table, sorted by the "Country" and the
"CustomerName" column. This means that it orders by Country, but if some rows
have the same Country, it orders them by CustomerName.
Example:
selects all customers from the "Customers" table, sorted ascending by the
"Country" and descending by the "CustomerName" column
Chapter 7: SQL Joins
A JOIN clause is used to combine rows from two or more tables, based on a
related column between them. There are the different types of the JOINs in SQL
as followed:
INNER JOIN: returns records that have matching values in both tables.
LEFT OUTER JOIN: returns all records from the left table, and the
matched records from the right table.
RIGHT OUTER JOIN: returns all records from the right table, and the
matched records from the left table.
FULL OUTER JOIN: returns all records when there is a match in either left
or right table.
The INNER JOIN selects records that have matching values in both tables.
Syntax:
Example:
selects all orders with customer information
Example:
The LEFT JOIN returns all records from the left table (table1), and the matching
records from the right table (table2). The result is all records from the left table
and 0 records from the right side, if there is no match.
Syntax:
Example:
The RIGHT JOIN returns all records from the right table (table2), and the
matching records from the left table (table1). The result is all records from the
right table and 0 records from the left side, if there is no match.
Syntax:
Example:
return all employees, and any orders they might have placed.
The FULL OUTER JOIN returns all records when there is a match in left (table1) or right
(table2) table records. Note that FULL OUTER JOIN and FULL JOIN are the same.
Syntax:
Example:
A self Join is a regular join, but the table is joined with itself.
Syntax:
Where T1 and T2 are different table aliases for the same table.
Example:
Syntax:
Example2: create a view that selects every product in the "Products" table with
a price higher than the average price:
Syntax:
Syntax: