0% found this document useful (0 votes)
118 views12 pages

Lab 1 RDBMS

The document provides instructions for exploring MS SQL Express edition and retrieving data from a database using SELECT statements. It describes how to create a database and table using SQL Server Management Studio. It also explains how to add data to the table manually or using INSERT statements. Finally, it covers the basics of retrieving data using SELECT statements, including selecting all columns, specific columns, using column aliases, joining columns, computing values, and using the WHERE clause.

Uploaded by

abiha naqvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views12 pages

Lab 1 RDBMS

The document provides instructions for exploring MS SQL Express edition and retrieving data from a database using SELECT statements. It describes how to create a database and table using SQL Server Management Studio. It also explains how to add data to the table manually or using INSERT statements. Finally, it covers the basics of retrieving data using SELECT statements, including selecting all columns, specific columns, using column aliases, joining columns, computing values, and using the WHERE clause.

Uploaded by

abiha naqvi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209

LAB # 1

EXPLORING MS SQL EXPRESS


EDITION, RETRIEVING DATA FROM
DATABASE

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

Creating Database by SQL SERVER EXPRESS

Select Start-->All Programs-->Microsoft SQL Server Express -->SQL SERVER


ManagementStudio.

CREATE A DATABASE USING SSMS

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...:

Name your database (I called mine TaskTracker) and click OK:

2
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209

Your New Database


Your new database will appear under the Databases section of the Object Explorer
(just under the System Databases folder). Here's mine:

3
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209

CREATE A TABLE USING SSMS

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:

Figure 1-1 Creating a new table

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.

Figure 1-2 Column names and identifying informatio

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.

Figure 1-3 Setting the Table Designer properties

Step 4. Save your new table by selecting File > Save Table_1, as shown in Figure 1-4.

Figure 1-4 Saving the new table

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.

Figure 1-5 Naming the table

Your new table will appear under the Tables section, as depicted in Figure 1-6.

Figure 1-6 The newly created table

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 :

You can now start entering the

data directly into your table

7
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209

RETRIEVING DATA FROM


DATABASE

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

Using SELECT Statement:


The SELECT statement retrieves rows and columns from one or more tables and
presents data in the result set i.e. output of SELECT statement in a grid.
1. Choose All Columns or Fields
The asterisk (*) sign is used for retrieving all columns from a table.

SYNTAX
SELECT * FROM table_name
Examples:
select * from bonus
select * from employee
select * from department
select * from salary

2. Choose Selected Columns or Fields


For selecting specific columns, we mention the columns by listing their names.
SYNTAX:
SELECT column1, column2, … FROM table_name
Examples:

1. SELECT ename, job, sal FROM Employee


2. SELECT dno, dname from department

3. Using the Column Alias with the AS Clause

SELECT DName AS 'Department Name' FROM Department

We have use AS clause to as the heading of ‘Department Name’ instead of Dname


from the Department table as shown below.

4. Joining Constant Characters in the SQL Statement

Join column Emp_Name and Emp_Job column of Employee table having the
same data type by this query:

SELECT EName + ' does the work of ' + Job


AS 'Employee Work' FROM Employee

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

5. Computing Values in SELECT Statements

To obtain an increase of 25 percent in the salary of employees in the employees


table can be calculated like this:

SELECT ENo, EName, Job, Sal AS 'Previous Salary', Sal * 0.25 AS increment,
Sal + Sal * 0.25 AS 'Current Salary' FROM Employee

Single quote ‘ ‘ is used in the second AS-Clause because it contains space


between two words, otherwise it is not necessary.

6. Using WHERE-Clause in SELECT statement

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

7. Limiting Records By WHERE Clause

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:

select * from employee where hiredate='1991-09-06'


or
select * from employee where hiredate='9/6/1991'

8. Using DISTINCT keyword

Using DISTINCT keyword with our query, repeating records can be eliminated.

Type in the following query in the Editor Pane.

SELECT Head FROM Employee

Head column from the Employee table will be retrieved and as used here without
DISTINCT keyword, all records will be displayed.

Now edit the same query with the DISTINCT keyword.

SELECT distinct Head FROM Employee

Repeating records will be eliminated.

9. Limiting Records by Using ‘TOP’ and ‘PERCENT’ In


SELECTStatement
The TOP-Clause limits the rows retrieved from the query result.

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:

SELECT TOP 5 * FROM Employee


Asterisk (*) sign again provides all the columns of the table, and column names
can be provided to restrict the number of columns retrieved as:

SELECT TOP 5 eno,ename FROM Employee

Similarly a percent of records can be retrieved from a table. For example

SELECT TOP 60 PERCENT * FROM Salary

11
Lab # 1: Exploring MS SQL Express edition, retrieving data from database SWE–209

LAB TASKS:

1. Create below tables in SQL Server Express

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)

Write SQL queries to

1. List all the details of employees who are female


2. List names and addresses of all employees who work for the IT department.

3. Create Hotel Database, insert 25 record for every field:


 Hotel (hotelNo, hotelName, city)
 Room (roomNo, hotelNo, type, price)
o (Hint:Type can “S” for Single , “D” for double , “F” Family)
 Booking (HotelNo, guestNo, dateFrom, dateTo, roomNo)
 Guest (guestNo, guestName, guestAddress)

Write SQL queries

a) List all the columns/attributes of the tables.


b) List the name and addresses of top five guests living in the “Karachi” city .
c) List Room type with a price less than Rs.4000 per night.
d) List Room type with a price not equal to Rs.7000 per night
e) List the bookings for which no date to have been specified.
f) List the top 60% of hotel which are booked after Jan 2020.

12

You might also like