0% found this document useful (0 votes)
28 views24 pages

First Lab Class Handouts - To Upload

Uploaded by

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

First Lab Class Handouts - To Upload

Uploaded by

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

LAB updated contents

DBMS
Tools
1. Microsoft SQL Server
2. Oracle
3. MySQL

Versions:
SQL Server 2019 Windows 8
Windows 10
Latest version available.

64 Bit os
SQL Server 2016 Windows 7 (2016)
SQL Server 2014
SQL Server 2012 Obsolete version:
About these versions, Microsoft
has stopped supporting these.

*Out of Support
SQL Server Downloads | Microsoft

https://microsoft.com › en-sa › sql-server-downloads

SSMS

https://docs.microsoft.com/en-us/sql/ssms/download-sql-server-management-studio-ssms?
redirectedfrom=MSDN&view=sql-server-ver15

Free specialised edition


Express
ideal for development and production for desktop, web and
small server applications.

1.Developer

Express Developer
 Entry Level  For people who build but also
test applications
 Ideal for learning and building  Enterprise Level
small scaled applications
(desktop and web)

For Khalid:

You must have to have your own computer / Laptop.

You should install Win8 or win10 to use its latest


version of SQL server.
Instance Name: By defaut
It is labelled as

MSSQLSERVER

Local Server or

127.0.0.1/Irfan343016056997

You may have one


or many instances.

Step By Step Installation of SQL Server

1. Express or Developer Edition


2. SSMS (SQL Server Management Studio

Difference Between MS SQL Server and SSMS (SQL


Server management Studio)
SQL Server SSMS
 Interface
 Is a client / server
architecture.  To Connect and work
with SQL Server.
 Process clients requests,
replies to that requests.
Server Type:
Database Engine is by default option.

Server Name:

MSSQLSERVER is default server name.

/localhost/SQLEXPRESS

*localhost is my computer name because the server has


already been installed on my computer.

*SQLEXPRESS is an instance. As I told you that there


might be more than one instances.
Authentication

*Windows authentication is set as by default. Here you


don’t need a username and a password.

But For
*SQL Server Authentication, you need a username and
password.

MS SQL Server is RDBMS.

RDBMS: Relational database management system.

DBMS: Database management system

Difference between …
RDBMS DBMS
 Store data in a tabular  Holds or store data as file
form.
 Here there is a  No relationship between
relationship between data these files.
stored in tables.

What is system databases?

System databases created automatically when you install


SQL server. Example… master, model, msdb, tempdb.
What is user Databases?

User Databases are created by the database users. That are


granted access. Example BikeStores.

Data:
Its about facts to any object like your name, your age,
your height, your weight etc

Database:
Is a systematic collection of data. In this way, we can
make data management easy.

Facebook:
 Store, manipulate data

1. Your friends information


2. Friends activities
3. Friends messages
4. Advertisements

Telephonic directory:
1. People
2. Phone numbers
3. Other contact details
14-02-2021

We will learn how to simply load a user database using


SQL management studio.

SQL: Structured Query Language


 BikeStores Sample Database - create objects

This File is for Creating objects including schemas and tables.

Schema: SQL schema is a logical collection of database objects. TABLES,


TRIGGERS, VIEWS, PROCEDURES ETC
 BikeStores Sample Database - load data
This file is to load data into the tables.

 BikeStores Sample Database - drop all objects


This file is for removing tables and their schemas from the sample
database. Refresh your sample database.
SQL Commands
DDL DML DCL TCL

Data Definition Data manipulation Data Control Transaction Control


Language Language Language Language

Which deals with It deals with data Include It deals with a


database schemas manipulation. commands mostly transaction within a
and descriptions. used for granting database.
rights,
permissions and
other controls of
the database
system.

CREATE SELECT GRANT COMMIT


ALTER INSERT REVOKE ROLLBACK
DROP UPDATE SAVEPOINT
SET
TRUNCATE DELETE TRANSACTION
COMMENT MERGE
RENAME LOCK TABLE
CALL

Standard format of SELECT COMMAND


SELECT
SELECT_LIST (names of Fields)
FROM
SCHEMA_NAME.TABLE_NAME;

1. First, specify a list of comma-separated


columns from which you want to query
data in the SELECT clause.
2. Second, specify the SELECT statement,
SQL server processes the FROM clause
first and then the SELECT clause even
though the SELECT command appears
first in the query.

WHERE For applying condition


clause

Operators:
 AND
 OR
 IN
 BETWEEN
 LIKE
 NULL

SELECT
product_id,
product_name,
category_id,
model_year,
list_price
from production.products
where category_id = 6
order by
list_price desc;

SELECT
product_id,product_name,
category_id,model_year,list_price
from production.products

where category_id = 6 AND model_year =


2018
order by
list_price desc;

SELECT
product_id,product_name,
category_id,model_year,list_price
from production.products
where list_price > 350 AND model_year =
2018
order by
list_price desc;

SELECT
product_id,product_name,
category_id,model_year,list_price
from production.products

where list_price BETWEEN 350 AND 1000


order by
list_price desc;

SELECT
product_id,product_name,
category_id,model_year,list_price
from production.products

where list_price IN (299, 369, 469.99)


order by
list_price desc;

SELECT
product_id,product_name,
category_id,model_year,list_price
from production.products

where product_name LIKE '%Electra%'


order by
list_price desc;

select
first_name,
last_name, phone
from sales.customers
where phone IS NULL
order by
first_name,
last_name;

select
first_name,
last_name, phone
from sales.customers
where phone IS NOT NULL
order by
first_name,
last_name;

select *
from production.products
where
(brand_id = 1 OR brand_id = 2)
AND list_price > 1000
order by list_price desc;

select *
from production.products
where
brand_id = 1
OR brand_id = 2
OR brand_id = 4
order by brand_id desc;

select product_name, brand_id


from production.products
where
brand_id IN (1, 2, 3)

order by
brand_id desc;

select product_name, brand_id,


list_price
from production.products
where
brand_id = 1
OR brand_id = 2
AND list_price > 500
order by
brand_id desc;

select product_name, list_price


from
production.products
where
list_price NOT IN (89.99,109.99)
order by
list_price;

ORDER BY To sort out the result set


Clause By default, it’s in ASC (ascending
order)

DESC
Descending order

OFFSET -
This Clause is used to limit the
FETCH Clause number of rows.

NOTE: The OFFSET and FETCH


clauses are the options of the ORDER
BY Clause. You must use the
OFFSET and FETCH clauses with
ORDER BY Otherwise you will get
an ERROR.
OFFSET CLAUSE

The OFFSET clause specifies the


number of rows to skip.

FTECH Clause

It specifies the number of row to


return after OFFSET.

NOTE: The OFFSET Clause is


mandatory while FETCH Clause is
Optional.
select
product_name,
list_price
from
production.products
order by
list_price,
product_name;

select
product_name,
list_price
from
production.products
order by
list_price,
product_name
Offset 10 rows
fetch next 5 rows only;
SELECT TOP
statement
It allows you to limit the number of
rows or percentage of rows returned.

select top 10
product_name,
list_price
from
production.products
order by
list_price DESC;

select top 5 percent


product_name,
list_price
from
production.products
order by
list_price DESC;
SELECT TOP with
TIES It include the rows that match the
statement
values in the last row.

select top 3 with ties


product_name,
list_price
from
production.products
order by
list_price DESC;

In this case, it returned three more


products whose list prices are the
same as the third one.
Select DISTINCT This clause to retrieve only distinct
Clause values in a specified list of columns.
select
city
from sales.customers
order by
city;

select distinct
city
from sales.customers
order by
city;

select distinct
city, state
from sales.customers
Select DISTINCT order by
city,
Clause state;

 With multiple
columns

GROUP BY To group rows into groups.


select city, count (*)

Clause from sales.customers


where state = 'CA'
group by city
order by city;

HAVING To filter groups based on one or more


conditions.
clause select city, count(*)
from sales.customers
where state = 'ca'
group by city
having count (*) > 10
order by city desc;

ASC/DESC
The ASC sorts the result from the
lowest value to the highest value.

The DESC sorts the result set from


the highest to the lowest one.
NOTE:

Each column called Field

Each row called Record.


NOTE:
You should avoid or not use the SELECT *

1. Because it retrieves more data than


your application needs to function. It
causes un-necessary data to transfer.

CREATE DATABASE
How to create a new database
>CREATE DATABASE TestDb;

DROP DATABASE
How to remove a database
>DROP DATABASE IF EXISTS TestDb;

BACKUP DATABASE
Create a full backup of an existing Database
>BACKUP DATABASE TestDb1
TO DISK = ‘Filepath’
WITH DIFFERENTIAL;
>BACKUP DATABASE TestDb1 TO DISK = 'C:\
BackupDB\TestDb1.bak';

NOTE:
WITH DIFFERENTIAL: It reduces the backup time because it
copies only the changes.
BACKUP DATABASE TestDb1
TO DISK = 'C:\BackupDB\TestDb1.bak'
WITH DIFFERENTIAL;

CREATE TABLE
Tables are used to store data in the database and
each table has its unique name.

Student Table:
Name ID Address Mobile
Mosa 123 Buraydah 123
Abdullah 456 Alras 456

CREATE TABLE Student


(
Name varchar(255),
ID int,
Address varchar(255),
Mobile int
);
DROP TABLE
Used to drop / remove an existing table in a database

NOTE:
Be careful before dropping or deleting a table, why?
Because if the table contains information, the
complete information will result in loss.

>DROP TABLE Student;

TRUNCATE TABLE

Used to delete data inside a table, but not the table


itself.
>TRUNCATE TABLE Student;

INSERT INTO
insert into Student (Name, ID, Address, Mobile)
values ('Abdullah', '123', 'Buraydah', '123');

DATA TYPES
In SQL Server, a column, variable and parameter
holds a value that associated with a type. A data
type is an attribute that specifies the type of data.

numeric(p,s)
p= for precision
s= scale numbers

p is a total number that can be stored both to the left


and to the right of the decimal point. P must be from
1 to 38 but default is 18.

s indicates the maximum number of digits stored to


the right of the decimal point.

date
store a date only. (3 bytes)

int
4 bytes

varchar (size)
A variable length string (contain letters, numbers,
and special characters). The size in characters is
from 0-65535.

varchar2 (size)
varchar and varchar2 are same string type datatypes
but varchar2 considers NULL and EMPTY string
same.

char(size)
0-255 characters
CREATE TABLE dept
(
deptno int,
dname varchar(14),
loc varchar(13)
);

You might also like