You are on page 1of 17

Lecture 06:

QUERY-BY-EXAMPLE

1
Objectives
The main features of Query-By-Example (QBE).
• The types of query.
• How to use QBE to build queries to select fields and
records.
• How to use QBE to target single or multiple tables.
• How to perform calculations using QBE.
• How to use advanced QBE facilities including parameter,
find matched, find unmatched, crosstab, and auto-
lookup queries.
• How to use QBE action queries to change the content of
tables.

2
SQL server
• Windows 10
– To open SQL Server Configuration Manager.
• On the Start Page > type “SQLServerManager13.msc”
(for SQL Server 2016)
• Or - Open > Computer Manager
> Services and Applications
> SQL Server Configuration.

3
Microsoft SQL Server Management Studio (SSMS)

SSMS is a client tool and not the server by itself

Developer Developer
Machine 1 Machine 1

Database Server

Developer Developer
Machine 1 Machine 1

Developer machines (clients) connects to SQL Server Using SSMS


4
Microsoft SQL Server Management Studio
– To open Microsoft SQL Server Management Studio.
• On the Start Page > type “Microsoft SQL Server Management Studio”.
• Start > Microsoft SQL Server 2016
> Microsoft SQL Server Management Studio

• Server Type > Database Engine


• Server name > (local) or . Or 172.0.0.1
• Authentication > Windows /SQL server
− If SQL Server Authentication
> type (User name & Password)

5
Object Explorer window

Type queries

Expand Object Click on New Query  Open the SQL window

Select database

A SQL Server database can be created, alter, and cropped


1. Graphically using SQL Server Management Studio (SSMS)
2. Using a Query
Structured Query Language
To create the database using a query
Create Database databaseName
Whether, you create a database graphically using the designer, or using a
query, the following two files gets generated
.MDF file-Data File (Contains actual data)
.LDF file-Transaction Log file (Used to recover the database)
To alter a database, once it’s created
Alter Database databaseName Modify Name = newDatabaseName

To delete or drop a database


Drop Database databaseThatYouWantToDrop
– Dropping a database, deletes the .LDF and .MDF files.
– You cannot drop a database, if it is currently in use. You get an error
stating cannot drop database “databaseThatYouWantToDrop”
because it is currently in use.
8
Structured Query Language
• Generally, the Entity Relational Diagram and input data
is created by following steps
Step 1: Create Table with primary key with integrity
constraint.
Step 2: Create foreign key with other integrity constraint.
Step 3: Input data.

9
Structured Query Language
• The aim is to create tblPerson and tblGender tables and establish
primary key and foreign key constraints using a query.
Create table tblPerson
(
PersonID int NOT NULL Primary Key,
[Name] nvarchar(50),
Email nvarchar(50), tblPerson tblGender
PersonID GenderID
GenderID int
Name Gender
)
Email
GenderID
Create table tblGender
(
GenderID int NOT NULL Primary Key,
Gender nvarchar(50) NOT NULL
)

Alter Table tblPerson Add Constraint tblPerson_GenderID_FK


Foreign Key (GenderID) References tblGender(genderID)
10
Structured Query Language
Can you explain the constraint of foreign key? and why do
we use foreign key constraint?
Foreign keys are used to enforce database integrity.
A foreign key in one table points to a primary key in
another table. The foreign key constraint prevents invalid
data form being inserted into the foreign key column. The
values that you enter into the foreign key column, has to
be one of the values contained in the table it points to.

11
Structured Query Language
Insert Into tblGender Values (1,'Male'),
(2, 'Female'),
(3, 'Unknown')

Insert Into tblPerson Values (1, 'John', 'j@yahoo.com', 1),


(2, 'Mary', 'm@gmail.com', 2),
(3, 'Smon', 'smon@sam.com', 1),
(4, 'Kenny', 'kenny@ny.com',3),
(5, 'Sam', 'sam@mail.com', NULL)

Insert Into tblPerson (PersonID, [Name]) values (6, 'Richard')

12
How to input the value in table
department
departID
departName
departPhone
headOfDepart
dateOperate lecturer
lecID
lecName
Salary
lecturer * gender
lecID DoB
lecName lecAddress
Salary leader
gender DepID
DoB
lecAddress
leader
departID

13
14
15
16
Thank you for your listen
End of lecture 06

17

You might also like