1 Introduction
1 Introduction
1
Contents
What is SQL? Instance of SQL Server
When do you need a Advantages of Instances
Database? SQL Server – Editions
Uses of Databases Create databases
Ways to Use SQL Create table
Some Relational Database
Concepts
Finding Your Way Around
the Server
What is SQL Server?
Usage of SQL Server
Versions of SQL Server
SQL Server Components
2
Software environment (1)
3
Software environment (2)
4
What is SQL?
5
SQL is a Standard - BUT....
6
When do you need a Database?
Traditional Uses:
Live Queries
Report Generation
Normalization, foreign keys, joins, etc.
Newer uses:
Storage - data is extracted and analyzed in
another application
Backends to web sites
Traditional rules may not be as important
8
Ways to Use SQL
Console command
GUI interfaces are often available
Interfaces to many programming
languages: R, python, perl, PHP, etc.
SQLite - use SQL without a database
server
PROC SQL in SAS
9
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a database
SQL can create new databases
SQL can create new tables in a database
SQL can create stored procedures in a database
SQL can create views in a database
SQL can set permissions on tables, procedures, and
views
10
Using SQL in Your Web Site
11
Some Relational Database Concepts
12
RDBMS
14
Show some information with SQL Server
SELECT name, database_id, create_date
FROM sys.databases;
16
What is SQL Server?
17
Usage of SQL Server
To create databases.
To maintain databases.
To analyze the data through SQL Server
Analysis Services (SSAS).
To generate reports through SQL Server
Reporting Services (SSRS).
To carry out ETL operations through SQL
Server Integration Services (SSIS)
18
Versions of SQL Server
19
SQL Server Components
SQL Server works in client-server architecture, hence it
supports two types of components: (a) Workstation and
(b) Server.
Workstation components are installed in every
device/SQL Server operator’s machine. These are just
interfaces to interact with Server components. Example:
SSMS, SSCM, Profiler, BIDS, SQLEM etc.
Server components are installed in centralized server.
These are services. Example: SQL Server, SQL Server
Agent, SSIS, SSAS, SSRS, SQL browser, SQL Server full
text search etc.
20
Instance of SQL Server
An instance is an installation of SQL Server.
An instance is an exact copy of the same software.
If we install 'n' times, then 'n' instances will be created.
There are two types of instances in SQL Server a)
Default b) Named.
Only one default instance will be supported in one
Server.
Multiple named instances will be supported in one
Server.
Default instance will take the server name as Instance
name.
Default instance service name is MSSQLSERVER.
16 instances will be supported in 2000 version.
50 instances will supported in 2005 and later versions.
21
Advantages of Instances
22
SQL Server – Editions
SQL Server is available in various editions. This chapter
lists the multiple editions with its features.
Enterprise: This is the top-end edition with a full feature
set.
Standard: This has less features than Enterprise, when
there is no requirement of advanced features.
Workgroup: This is suitable for remote offices of a larger
company.
Web: This is designed for web applications.
Developer: This is similar to Enterprise, but licensed to
only one user for development, testing and demo. It can
be easily upgraded to Enterprise without reinstallation.
23
SQL Server – Editions (cont'd)
24
SQL Server – Editions (cont'd)
Business Intelligence: Business Intelligence Edition is a
new introduction in SQL Server 2012. This edition
includes all the features in the Standard edition and
support for advanced BI features such as Power View
and PowerPivot, but it lacks support for advanced
availability features like AlwaysOn Availability Groups
and other online operations.
Enterprise Evaluation: The SQL Server Evaluation Edition
is a great way to get a fully functional and free instance
of SQL Server for learning and developing solutions. This
edition has a built-in expiry of 6 months from the time
that you install it.
25
SQL Server – Editions (cont'd)
2005 2008 2008 R2 2012 2014
Enterprise
Standard
Developer
Workgroup
Win Compact Edition
- Mobile
Enterprise Evaluation
Express
Web
Datacenter
Business Intelligence
26
Data Types
Character
CHARACTER(L) - a fixed-length character of length L
CHARACTER VARYING(L) or VARCHAR(L) – supports
maximum length of L
Binary
BIT(L), BIT VARYING(L) - like corresponding
characters
BINARY LARGE OBJECT(L) or BLOB(L)
Temporal
DATE
TIME
TIMESTAMP
28
CREATE DATABASE
Two ways:
Use SQL Server Management Studio
Use Transact-SQL
29
Use SQL Server Management Studio
To create a database
In Object Explorer, connect to an instance of the SQL
Server Database Engine and then expand that
instance.
Right-click Databases, and then select New Database.
In New Database, enter a database name.
To create the database by accepting all default
values, select OK;
See for more information:
https://docs.microsoft.com/en-us/sql/relational-
databases/databases/create-a-database?view=sql-
server-ver15
30
Use Transact-SQL
To create a database
Connect to the Database Engine.
From the Standard bar, select New Query.
Copy and paste the following example into the query
window and select Execute.
This example creates the database QLKH. Because the
keyword PRIMARY is not used, the first file (QLKH_dat)
becomes the primary file. Because neither MB nor KB is
specified in the SIZE parameter for the QLKH _dat file, it
uses MB and is allocated in megabytes. The QLKH_log
file is allocated in megabytes because the MB suffix is
explicitly stated in the SIZE parameter.
31
Use Transact-SQL (cont'd)
USE master;
GO
CREATE DATABASE QLKH
ON
( NAME = QLKH_dat,
FILENAME = 'C:\Program Files\Microsoft SQL Server\
MSSQL13.MSSQLSERVER\MSSQL\DATA\QLKHdat.mdf',
SIZE = 10,
MAXSIZE = 50,
FILEGROWTH = 5 )
LOG ON
( NAME = QLKH_log,
FILENAME = 'C:\Program Files\Microsoft SQL Server\
MSSQL13.MSSQLSERVER\MSSQL\DATA\QLKHlog.ldf',
SIZE = 5MB,
MAXSIZE = 25MB,
FILEGROWTH = 5MB );
GO
32
CREATE TABLE statement
33
Now, create three tables
34
CREATE TABLE Lecturers(
LID char(4) NOT NULL,
FullName nchar(30) NOT NULL,
Address nvarchar(50) NOT NULL,
DOB date NOT NULL,
CONSTRAINT pkLecturers PRIMARY KEY (LID)
)
35
CREATE TABLE Participation(
LID char(4) NOT NULL,
PID char(4) NOT NULL,
Duration smallint,
CONSTRAINT pkParticipation PRIMARY KEY (LID, PID),
CONSTRAINT fk1 FOREIGN KEY (LID) REFERENCES Lecturers (LID),
CONSTRAINT fk2 FOREIGN KEY (PID) REFERENCES Projects (PID)
)
36
INSERT INTO Lecturers VALUES('GV01',N'Vũ Tuyết Trinh',N'Hoàng Mai, Hà Nội',
'1975/10/10'),
('GV02',N'Nguyễn Nhật Quang',N'Hai Bà Trưng, Hà Nội','1976/11/03'),
('GV03',N'Trần Đức Khánh',N'Đống Đa, Hà Nội','1977/06/04'),
('GV04',N'Nguyễn Hồng Phương',N'Tây Hồ, Hà Nội','1983/12/10'),
('GV05',N'Lê Thanh Hương',N'Hai Bà Trưng, Hà Nội','1976/10/10')