Introduction To SQL Server and SQL
Introduction To SQL Server and SQL
Examples:
• Bank/Account systems
• Information in Web pages such as Facebook,
Wikipedia, YouTube, etc.
• Fronter, TimeEdit, etc.
• … lots of other examples!
3
Database Management Systems (DBMS)
• Microsoft SQL Server
– Enterprise, Developer versions, etc. (Professional use)
– Express version is free of charge
• Oracle
• MySQL (owned by Oracle, but previously owned by Sun
Microsystems) - MySQL can be used free of charge (open
source license), Web sites that use MySQL: YouTube,
Wikipedia, Facebook
• Microsoft Access
• IBM DB2
• Sybase
• etc.
We will use SQL server because it is very popular in the industry today, and we can use it for
free via the Microsoft DreamSpark Premium Subscription – which is available for the
students and staff at Telemark University College, or use the Express version which is available
for free for everybody. 4
Microsoft SQL Server
SQL Server consists of a Database Engine and a Management Studio. The Database Engine has no graphical interface - it
is just a service running in the background of your computer (preferable on the server). The Management Studio is
graphical tool for configuring and viewing the information in the database. It can be installed on the server or on the
client (or both).
5
Database Design
6
Database Design – ER Diagram
ER Diagram (Entity-Relationship Diagram)
• Used for Design and Modeling of Databases.
• Specify Tables and relationship between them (Primary Keys and
Foreign Keys) Table Name
Example:
Table Name
Column
Names
Primary Key
Primary Key
Foreign Key
Relational Database. In a relational database all the tables have one or more relation with each other using Primary Keys
7
(PK) and Foreign Keys (FK). Note! You can only have one PK in a table, but you may have several FK’s.
Table Name
Table Name
Column
Names
Primary Key
Primary Key
Foreign Key
8
Database - “Best Practice”
• Tables: Use upper case and singular form in table names – not
plural, e.g., “STUDENT” (not “students”)
• Columns: Use Pascal notation, e.g., “StudentId”
• Primary Key:
• If the table name is “COURSE”, name the Primary Key column
“CourseId”, etc.
• “Always” use Integer and Identity(1,1) for Primary Keys. Use
UNIQUE constraint for other columns that needs to be unique,
e.g. “RoomNumber”
• Specify Required Columns (NOT NULL) – i.e., which columns that
need to have data or not
• Standardize on few/these Data Types: int, float, varchar(x),
datetime, bit
• Use English for table and column names
• Avoid abbreviations! (Use “RoomNumber” – not “RoomNo”,
“RoomNr”, ...) 9
Database Design Exercise
12
Microsoft SQL Server
3
Your
Tables
13
Microsoft SQL Server
Do you get an error
when trying to
change your tables?
14
Create Tables using the Designer Tools
in SQL Server
Even if you can do “everything” using the SQL language, it is sometimes easier to do
something in the designer tools in the Management Studio in SQL Server.
Instead of creating a script you may as well easily use the designer for creating tables,
constraints, inserting data, etc.
1 2
Select “New Table …”: Next, the table designer pops up where you
can add columns, data types, etc.
16
SQL
Structured Query Language
18
SQL – Structured Query Language
Query Examples:
• insert into STUDENT (Name , Number, SchoolId)
values ('John Smith', '100005', 1)
DDL DML
Data Definition Language (DDL) Data Manipulation Language (DML)
CRUD
Create Drop
CREATE Tables DELETE Tables Create INSERT INTO
Delete DELETE
Create Tables using SQL
Example:
CREATE TABLE [SCHOOL]
(
SchoolId int IDENTITY(1, 1) NOT NULL PRIMARY
KEY,
SchoolName varchar(50) NOT NULL UNIQUE,
Description varchar(1000) NULL,
Address varchar50) NULL,
Phone varchar(50) NULL,
PostCode varchar(50) NULL,
PostAddress varchar(50) NULL,
)
GO
...
...
22
SQL Queries
Table Name: CUSTOMER
23
INSERT
Example:
INSERT INTO Customers (CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES ('Cardinal','Tom B. Erichsen','Skagen 21','Stavanger','4006','Norway');
24
SELECT
Students: Write and Execute the following Queries.
UPDATE CUSTOMER
SET ContactName='Alfred Schmidt', City='Hamburg'
WHERE CustomerName='Alfreds Futterkiste'
Update Warning!
Be careful when updating records. What happens if we had
omitted the WHERE clause, in the example above, like this:
UPDATE CUSTOMER
SET ContactName='Alfred Schmidt', City='Hamburg';
26
DELETE
Students: Write and Execute the following Queries.
27
SQL Queries
Students: Get all data from the BOOK table using SQL
29
Advanced SQL Features
• Views: Views are virtual table for easier access to
data stored in multiple tables.
• Stored Procedures: A Stored Procedure is a
precompiled collection of SQL statements. In a
stored procedure you can use if sentence, declare
variables, etc.
• Triggers: A database trigger is code that is
automatically executed in response to certain
events on a particular table in a database.
• Functions: With SQL and SQL Server you can use
lots of built-in functions or you may create your
own functions
30
Get Data from multiple tables in a
single Query using Joins
Example:
SELECT
SCHOOL.SchoolId, The Name of the View
SCHOOL.SchoolName,
COURSE.CourseId,
COURSE.CourseName, Inside the View you join the
COURSE.Description
different tables together using
FROM the JOIN operator
SCHOOL
INNER JOIN COURSE ON SCHOOL.SchoolId = COURSE.SchoolId
GO
32
Creating Views using the Editor
3
4
Add necessary tables
http://www.w3schools.com/quiztest/quiztest.asp?qtest=SQL
36
SQL Tutorial
37
Create Tables using SQL
if not exists (select * from dbo.sysobjects where id = object_id(N'[SCHOOL]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE [SCHOOL]
(
[SchoolId] [int] IDENTITY(1, 1) NOT NULL PRIMARY KEY,
[SchoolName] [varchar](50) NOT NULL UNIQUE,
[Description] [varchar](1000) NULL,
[Address] [varchar](50) NULL,
[Phone] [varchar](50) NULL,
[PostCode] [varchar](50) NULL,
[PostAddress] [varchar](50) NULL,
)
GO
...
...
Students: Create the necessary Tables in SQL Server (either with a SQL
38
Script or use the Designer Tools in SQL Server )
References
• H.-P. Halvorsen. (2014). Structured Query Language. Available:
http://home.hit.no/~hansha/?tutorial=sql
• NTNU. (2013). TDT4140 Systemutvikling. Available:
http://www.ntnu.no/studier/emner/TDT4140
• UiO. (2013). INF1050 - Systemutvikling. Available:
http://www.uio.no/studier/emner/matnat/ifi/INF1050/
• O. Widder. (2013). geek&poke. Available: http://geek-and-poke.com
• B. Lund. (2013). Lunch. Available: http://www.lunchstriper.no,
http://www.dagbladet.no/tegneserie/lunch/
• S. Adams. Dilbert. Available: http://dilbert.com
39
40
Hans-Petter Halvorsen, M.Sc.
E-mail: hans.p.halvorsen@hit.no
Blog: http://home.hit.no/~hansha/