Lab 1 RDBMS
Lab 1 RDBMS
LAB # 1
OBJECTIVE
Overview of the features of MS SQL Server Edition and Retrieve data from your database
using SELECT Statement.
THEORY
A database (db) is an organized collection of data, typically stored in electronic format.
It allows you to input, organize, and retrieve data quickly. Traditional databases are
organized by fields, records, and files. To better understand what a database is, consider
the telephone book as a simple example.
If you had the telephone book stored on disk, the book would be the file. Within the
telephone book, you would have a list of records each of which contains a name,
address, and telephone number. These single pieces of information (name, address,
phone number) would each constitute a separate field.
Because a database can store thousands of records, it would be a chore if you had to
open a table and go through each record one at a time until you found the record you
needed. Of course, the process would be even more difficult if you had to retrieve
multiple records.
Thankfully, you don’t have to go through database records in this way. Rather, to
retrieve data within a database, you run a database query, which is an inquiry into the
database that returns information back from the database. In other words, a query is
used to ask for information from a database.
Databases are often found on database servers so that they can be accessed by multiple
users and provide a high level of performance. One popular database server is
Microsoft SQL Server. Database servers like SQL Server do not actually house
graphical programs, word-processing applications, or any other type of applications.
Instead, these servers are entirely optimized to serve only the purposes of the database
itself, usually using advanced hardware that can handle the high processing needs of
the database. It is also important to note that these servers do not act as workstations;
they generally are mounted on racks located in a central data center and can be accessed
only through an administrator’s desktop system.
1
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
The following steps demonstrate how to create a database in SQL Server 2019 using
SQL Server Management Studio.
1. From the Object Explorer, right click on the Databases folder/icon and
select New database...:
2
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
3
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
Before you begin, be sure to launch SQL Server Management Studio. Make sure
you’ve expanded the particular database in which you wish to create the new table,
then follow these steps:
Step1 . Right-click the Table folder and select New Table, as shown in Figure 1-1:
Step 2. Use the information shown in Figure 1-2 to complete the details for Column
Name, Data Type, and Length, as specified in the parentheses and Allow Nulls
columns.
4
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
Step 3. Set the Default Value of the DateCreated column to (getdate()); this will
insert the current date within each new record for that specific field. See Figure 1-3.
Step 4. Save your new table by selecting File > Save Table_1, as shown in Figure 1-4.
5
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
Step 5. Type the new name of the table you are saving, as shown in Figure 1-5.
Your new table will appear under the Tables section, as depicted in Figure 1-6.
ADDING DATA
We can use the Edit Top 200 Rows option to manually type data directly into the table
rows.
Manually entering data is OK if you only have a little bit of data to enter. But it's a bit
clunky and can impractical if you have a lot of data. Plus it doesn't really suit most
business needs, where non-technical users need to be able to update the database.
In any case, here's how to manually enter data directly into the table:
6
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
1. In the Object Explorer, right click on the table you wish to open, and
select :
7
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
THEORY
Introduction to SQL
SQL (Structured Query Language) is the standard language used for creating, updating
and querying relational databases. It is supported by all modern database management
systems (e.g. Oracle, IBM DB2, Microsoft Access, Microsoft SQL Server,
PostgreeSQL, MySQL, etc.).
SQL is a declarative language (as opposed to a procedural language like C, Perl, etc.).
This means that the language is used by the programmer to directly describe what end
result is required (e.g. what data is required from a query operation), as opposed to
explicitly describing all the steps required to retrieve the required data from storage).
Thus, SQL is high-level, quite easy to learn, allowing complex tasks on a database to
be specified simply.
SQL has a defined syntax that specifies how standard keywords can be combined to
form valid SQL statements. SQL statements can be divided into three categories,
according to their function:
1. Data Manipulation: Statements that retrieve, insert, update, edit or delete data
stored in database tables. These statements begin with the keywords: SELECT,
INSERT, UPDATE or DELETE.
2. Data Definition: Statements that create, modify, or destroy database objects (such
as tables). These statements begin with the keywords: CREATE, ALTER or DROP.
3. Data Control: Statements that authorise certain users to view, change or delete data.
These statements begin with the keywords: GRANT or REVOKE. We do not consider
data control in the lab exercises.
8
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
SYNTAX
SELECT * FROM table_name
Examples:
select * from bonus
select * from employee
select * from department
select * from salary
Join column Emp_Name and Emp_Job column of Employee table having the
same data type by this query:
This will work only for the columns of the same data type; otherwise SQL Server
will generate an error as shown below:
9
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
SELECT ENo + ' is the employee number of ' + EName AS EMP FROM
Employee
SELECT ENo, EName, Job, Sal AS 'Previous Salary', Sal * 0.25 AS increment,
Sal + Sal * 0.25 AS 'Current Salary' FROM Employee
You can restrict the rows returned from the query by using the WHERE clause. A
WHERE-clause has a condition that must be true and should directly follow from the
FROM clause.
SYNTAX:
SELECT column_name FROM table_name WHERE condition(s)
Comparison operators are used in conditions that compare one expression to another.
Following is a list of comparison operators:
OPERAT MEANING
OR
= Equal to
> Greater than
>= Greater than or equal
To
< Less than
<= Less than or equal to
<> Not equal to
! Not
Following point must be kept in mind when using the WHERE clause:
o Character strings and dates are enclosed in single
quotationmarks.
o Character values are not case sensitive and date values
areformat sensitive.
o The default date format is MM/DD/YYYY
10
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
Using the WHERE clause, following query will retrieve details of all those employees
whose Emp_head number is 125:
SELECT * FROM Employee WHERE Head = 125
Try these as well:
Using DISTINCT keyword with our query, repeating records can be eliminated.
Head column from the Employee table will be retrieved and as used here without
DISTINCT keyword, all records will be displayed.
SYNTAX:
SELECT TOP (n) column_name_list FROM table_name
The following query will retrieve first five rows from the employee table using
the TOP-clause:
11
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209
LAB TASKS:
2. Update the above schema with the following given details and insert 25 records
Employee (empNo, fname, lname, address, DOB, gender, position, deptNo)
Department (deptNo, deptName, mgrEmpNo)
Project (projNo, projName, deptNo)
WorksOn (empNo, projNo, dateWorked, hoursWorked)
12