microsoft sql serveraga-pics.com/wp-content/uploads/2018/10/sqlserver.pdf · institute of aga sql...

27
2018 Microsoft SQL Server LECTURER NIYAZ M. SALIH Institute of Aga

Upload: others

Post on 05-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

2018

Microsoft SQL Server

LECTURER

NIYAZ M. SALIH

Institute of Aga

Page 2: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Database:

A Database is a collection of related data organized in a way that data can be easily

accessed, managed and updated. Any piece of information can be a data, for

example name of your school. Database is actually a place where related piece of

information is stored and various operations can be performed on it.

Database Management System (DBMS):

A DBMS is software that allows creation, definition and manipulation of database.

DBMS is actually a tool used to perform any kind of operation on data in database.

DBMS also provides protection and security to database. It maintains data

consistency in case of multiple users. Here are some examples of popular DBMS,

MySQL, Oracle, Sybase, Microsoft Access and IBM DB2 etc.

Components of Database System:

The database system can be divided into four components.

Page 3: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Users: Users may be of various types such as DB administrator, System developer

and End users.

Database application: Database application may be Personal, Departmental,

Enterprise and Internal.

DBMS: Software that allow users to define, create and manages database access,

Ex: MySQL, Oracle etc.

Database: Collection of logical data.

Advantages of Database Management System (DBMS)

1. Minimize Data Redundancy

In File Processing System, duplicate data is created in many places because all the

programs have their own files.

2. Sharing of Data

In DBMS, Data can be shared in between authorized user of database. All the users

have their own right to access the database up to a level.

3. Data Consistency

DBMS controls data redundancy which in turn controls data consistency. Data

consistency means if you want to update data in any files then all the files should

not be updated again.

4. Data Integrity

Data integrity means unification of so many files into a single file. In DBMS data is

stored in different tables.

Page 4: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

5. Search Capability

Users of database may require fetching data from the database. There are

numerous queries users may ask about the data. Search speed of the database

must be fast to produce quick results.

6. Security

Data security means protecting your precious data from unauthorized access. Data

in database should be kept secure and safe to unauthorized modifications

7. Privacy

Privacy means up to what extent a user can access the data

8. Backup and Recovery

Data loss is a very big problem for all the organizations. In traditional file

processing system, a user needs to back up the database

9. Simplicity

DBMS is very simple for its users who use it. All the operations like insert, delete,

create and update are very easy to implement.

Data Abstraction:

Database systems are made-up of complex data structures. To ease the user

interaction with database, the developers hide internal irrelevant details from

users. This process of hiding irrelevant details from user is called data abstraction.

Page 5: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Levels of abstraction:

We have three levels of abstraction:

Physical level: This is the lowest level of data abstraction. It describes how data is

actually stored in database. You can get the complex data structure details at this

level.

Logical level: This is the middle level of 3-level data abstraction architecture. It

describes what data is stored in database.

View level: Highest level of data abstraction. This level describes the user

interaction with database system.

Page 6: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Database model:

A database model is a type of data model that determines the logical structure of

a database and fundamentally determines in which manner data can be stored,

organized and manipulated. The most popular example of a database model is

the relational model, which uses a table-based format.

Types of database models:

There are many kinds of data models. Some of the most common ones include:

Relational model

Hierarchical database model

Network model

Object-oriented database model

Entity-relationship model

Relational model:

A relational database (RDB) is a collective set of multiple data sets organized by

tables, records and columns. RDBs establish a well-defined relationship between

database tables. Tables communicate and share information, which facilitates data

search ability, organization and reporting.

Page 7: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Hierarchical model:

A hierarchical database model is a data model in which the data is organized into

a tree-like structure. The data is stored as records which are connected to one

another through links. A record is a collection of fields, with each field containing

only one value. The entity type of a record defines which fields the record

contains.

Network model:

The network model builds on the hierarchical model by allowing many-to-many

relationships between linked records, this model allows each child to have

multiple parents.

Object-oriented database model:

Page 8: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

This model defines a database as a collection of objects, or reusable software

elements, with associated features and methods.

Entity Relationship model(ER):

An entity relationship model, also called an entity-relationship (ER) diagram is a

way of graphically representing the logical relationships of entities (or objects) in

order to create a database.

Page 9: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Entity:

An entity in an ER Model is a real-world entity having properties called attributes.

Every attribute is defined by its set of values called domain. For example, in a

school database, a student is considered as an entity. Student has various

attributes like name, age, class, etc.

Relationship:

The logical association among entities is called relationship. Relationships are

mapped with entities in various ways. Mapping cardinalities define the number of

association between two entities.

Mapping cardinalities −

one to one

one to many

many to one

many to many

Page 10: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Database Administrator:

A Database Administrator is a person or a group of person who are responsible for

managing all the activities related to database system. This job requires a high

level of expertise by a person or group of person. There are very rare chances that

only a single person can manage all the database system activities so companies

always have a group of people who take care of database system.

Role and duties of Database Administrator:

Installing and Configuration of database:

DBA is responsible for installing the database software. He configures the software

of database and then upgrades it if needed.

Deciding the hardware device

Depending upon the cost, performance and efficiency of the hardware, it is DBA

who have the duty of deciding which hardware device will suit the company

requirement.

Managing Data Integrity

Data integrity should be managed accurately because it protects the data from

unauthorized use.

Database design

The logical design of the database is designed by the DBA.

Page 11: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Decides Data Recovery and Back up method

If any company is having a big database, then it is likely to happen that database

may fail at any instance. It is require that a DBA takes backups of entire database

in regular time span.

Tuning Database Performance

Database performance plays an important role for any business. If user is not able

to fetch data speedily then it may loss company business.

Capacity Issues

All the databases have their limits of storing data in it and the physical memory

also has some limitations.

Database accessibility

DBA writes subschema to decide the accessibility of database. He decides the

users of the database and also which data is to be used by which user.

Improve query processing performance

Queries made by the users should be performed speedily.

Decides validation checks on data

DBA has to decide which data should be used and what kind of data is accurate for

the company.

Page 12: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Monitoring performance

If database is working properly then it doesn’t mean that there is no task for the

DBA. Yes of course, he has to monitor the performance of the database. A DBA

monitors the CPU and memory usage.

Decides content of the database

A database system has many kind of content information in it. DBA decides fields,

types of fields, and range of values of the content in the database system.

Database implementation

Database has to be implemented before anyone can start using it. So DBA

implements the database system.

Structured Query Language (SQL):

Page 13: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

What is SQL?

SQL is Structured Query Language, which is a computer language for storing,

manipulating and retrieving data stored in a relational database.

SQL is the standard language for Relational Database System. All the Relational

Database Management Systems (RDBMS) like MySQL, MS Access, Oracle, Sybase,

Informix, Postgres and SQL Server use SQL as their standard database language.

Why SQL?

SQL is widely popular because it offers the following advantages:

Allows users to access data in the relational database management systems.

Allows users to describe the data.

Allows users to define the data in a database and manipulate that data.

Allows users to create and drop databases and tables.

Allows users to create view, stored procedure, functions in a database.

Allows users to set permissions on tables, procedures and views.

SQL Commands:

The standard SQL commands to interact with relational databases are CREATE,

SELECT, INSERT, UPDATE, DELETE and DROP. These commands can be classified

into the following groups based on their nature −

DDL - Data Definition Language:

Page 14: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Data Definition Language (DDL) is a standard for commands that define the

different structures in a database. DDL statements create, modify, and remove

database objects such as tables, indexes, and users.

DML - Data Manipulation Language:

A data manipulation language (DML) is a family of syntax elements similar to a

computer programming language used for selecting, inserting, deleting and

updating data in a database.

DCL - Data Control Language:

Page 15: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Data control language is a syntax similar to a computer programming language

used to control access to data stored in a database (Authorization).

Table:

The data in an RDBMS is stored in database objects which are called as tables. This

table is basically a collection of related data entries and it consists of numerous

columns and rows.

Field:

Page 16: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Every table is broken up into smaller entities called fields. The fields in the

CUSTOMERS table consist of ID, NAME, AGE, ADDRESS and SALARY.

A field is a column in a table that is designed to maintain specific information

about every record in the table.

Record:

A record is also called as a row of data is each individual entry that exists in a table.

For example, there are 7 records in the above CUSTOMERS table. Following is a

single row of data or record in the CUSTOMERS table:

Column:

A column is a vertical entity in a table that contains all information associated with

a specific field in a table.

For example, a column in the CUSTOMERS table is ADDRESS, which represents

location description and would be as shown in the CUSTOMERS table.

SQLServer SQL Commands:

Page 17: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT statement

The SELECT statement is used to select data from a database.

SELECT Syntax

SELECT department_id,department_name,manager_id,location_id FROM

hr.departments

SELECT * FROM hr.departments;

DISTINCT

The SELECT DISTINCT statement is used to return only distinct (different) values.

Syntax

SELECT DISTINCT department_id From hr.employees

Arithmetic Expression:

SELECT last_name,salary,salary+300 From hr.employees

Page 18: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT last_name,salary,12*salary+100 From hr.employees

SELECT last_name,salary,12*(salary+100) From hr.employees

NULL Value:

0 (zero) means value.

Space means character.

Null means unknown value or missing value.

Some fields may contains NULL value when there is no UNNULL constraint on it.

SELECT last_name,job_id,salary,commission_pct From hr.employees

SELECTlast_name,job_id,salary,12*salary*commission_pct From hr.employees

ALIASES:

Aliases are often used to make column names more readable.

Syntax

SELECT last_name as name,commission_pct comm From hr.employees

SELECT last_name "Name",salary * 12 "Annual Salary“ From hr.employees

Concatenation Operator(||):

SELECT last_name || job_id as "Employees“ From employees

Page 19: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT last_name || ' is a ' || job_id as "Employees“ From employees

SELECT last_name || ': 1 Month Salary=' || salary monthly From employees

DESCRIBE:

DESC employees

SELECT * FROM tab

WHERE clause:

The WHERE clause is used to filter records.

The WHERE clause is used to extract only those records that fulfill a specified

condition.

SELECT employee_id,last_name,job_id,department_id from employees WHERE

department_id=90

SELECT last_name,job_id,department_id from employees WHERE job_id='SA_REP‘

SELECT last_name,job_id,department_id from employees WHERE

last_name='WHALEN‘

SELECT employee_id,last_name,salary,manager_id FROM employees WHERE

manager_id IN (100,101,102)

SELECT employee_id,last_name,salary,manager_id FROM employees WHERE

manager_id =101 or manager_id =102 or manager_id =103

SELECT employee_id,last_name,salary,manager_id FROM employees WHERE

last_name IN ('Hartstein','Vargas')

Page 20: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT first_name FROM employees WHERE first_name like 'S%‘

SELECT last_name,hire_date FROM employees WHERE hire_date like '95%‘

SELECT last_name FROM employees WHERE last_name like '_o%'

LOGICAL PARAMETERS:

AND Syntax

SELECT employee_id,last_name,job_id,salary FROM employees

WHERE salary>=10000 AND job_id like '%MAN%'

OR Syntax

SELECT employee_id,last_name,job_id,salary FROM employees

WHERE salary>=10000 OR job_id like '%MAN%'

NOT Syntax

Page 21: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT last_name,job_id FROM employees

where job_id NOT IN ('IT_PROG‘)

ORDER BY:

The ORDER BY keyword is used to sort the result-set in ascending or descending

order.

Syntax

SELECT last_name,job_id,department_id,hire_date FROM employees ORDER BY

hire_date

SELECT last_name,job_id,department_id,hire_date FROM employees ORDER BY

hire_date DESC

SELECT last_name,salary FROM employees ORDER BY 2 DESC

SELECT last_name,department_id,salary FROM employees ORDER BY

department_id,salary

CASE function

The CASE function lets you evaluate conditions and return a value when the first

condition is met (like an IF-THEN-ELSE statement).

Syntax

Page 22: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT last_name,salary,job_id,

CASE job_id WHEN 'IT_PROG' THEN 1.10*salary

WHEN 'AD_VP' THEN 1.15*salary

WHEN 'AD_PRES' THEN 1.20*salary

ELSE salary

END "Revised salary"

FROM employees

DECODE function

The DECODE function has the functionality of an IF-THEN-ELSE statement.

Syntax

SELECT last_name,salary,job_id,

DECODE (job_id,'IT_PROG',1.10*salary,

'AD_VP' ,1.15*salary,

'AD_PRES',1.20*salary,salary)

Page 23: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

FROM employees

Aggregation

The following are the most commonly used SQL aggregate functions:

AVG – calculates the average of a set of values. COUNT – counts rows in a specified table or view. MIN – gets the minimum value in a set of values. MAX – gets the maximum value in a set of values. SUM – calculates the sum of values.

SELECT AVG(salary),MAX(salary),MIN(salary),SUM(salary) FROM employees WHERE job_id LIKE '%REP%'

SELECT MIN(hire_date),MAX(hire_date) FROM employees

SELECT MIN(last_name),MAX(last_name) FROM employees

SELECT COUNT(*) FROM employees WHERE department_id=50

SELECT COUNT(commission_pct) FROM employees WHERE department_id=80

SELECT COUNT(department_id) FROM employees

SELECT COUNT(DISTINCT department_id) FROM employees

SELECT AVG(commission_pct) FROM employees

Page 24: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT department_id,AVG(salary) FROM employees WHERE AVG(salary)>8000 GROUP BY department_id (Error)

SELECT department_id,AVG(salary) FROM employees HAVING AVG(salary)>8000 GROUP BY department_id

SELECT department_id,MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000

SELECT MAX(AVG(salary)) FROM employees GROUP BY department_id

SQL Joins

A JOIN clause is used to combine rows from two or more tables, based on a related

column between them.

Page 25: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

Self-JOIN is a regular join, but the table is joined with itself.

SELECT worker.last_name || ' works for ' || manager.last_name FROM employees

worker, employees manager

WHERE worker.manager_id= manager.employee_id

Cross join:

The SQL CROSS JOIN produces a result set which is the number of rows in the first

table multiplied by the number of rows in the second table

SELECT last_name,department_name FROM employees CROSS JOIN departments

(INNER) JOIN: Returns records that have matching values in both tables

Page 26: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

SELECT e.last_name, e.department_id,d.department_name FROM employees e INNER JOIN departments d ON (e.department_id=d.department_id)

LEFT (OUTER) JOIN: Return all records from the left table, and the matched records from the right table

SELECT e.last_name, e.department_id,d.department_name FROM employees e LEFT OUTER JOIN departments d ON (e.department_id=d.department_id)

RIGHT (OUTER) JOIN: Return all records from the right table, and the matched records from the left table

SELECT

e.last_na

me,

e.departm

ent_id,d.department_name FROM employees e RIGHT OUTER JOIN departments d

ON (e.department_id=d.department_id)

FULL (OUTER) JOIN: Return all records when there is a match in either left or right table

SELECT

e.last_na

me,

e.department_

id,d.departme

nt_name

FROM

Page 27: Microsoft SQL Serveraga-pics.com/wp-content/uploads/2018/10/SQLSERVER.pdf · Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih Users: Users may be of various types such as DB administrator,

Institute of Aga SQL SERVER Lecturer: Niyaz M. Salih

employees e FULL OUTER JOIN departments d ON

(e.department_id=d.department_id)