You are on page 1of 55

Adopt by Tsegay M.

 Create the database and relation structures;


 Perform basic data management tasks, such as:
◦ Insertion
◦ Modification
◦ Deletion of data
 Perform both simple and complex queries

Adopt by Tsegay M.
 Transform-oriented language
◦ A language to use relations to transform
input into required output
 Non-procedural language
◦ Specify what you require than how to get it
 Free
format
 Command consists English word, such as:
◦ CREATE, INSERT, SELECT, etc

Adopt by Tsegay M.
 Execute queries against a database
 Retrieve data from a database

 Insert records into a database

 Update records in a database


 Delete records from a database
 Create new databases
 Create new tables in a database
 Set permissions

Adopt by Tsegay M.
 DDL (Data Definition
Language):
 DML (Data Manipulation
Language):
 DCL (Data Control Language):

Adopt by Tsegay M.
 DDL and DCL statements are commonly used
by a database designer and database
administrator for establishing the database
structures.
 The DDL part permits database tables to be
created or deleted.
 It also defines indexes (keys), specify links
between tables, and impose constraints
between tables.
Adopt by Tsegay M.
Most important DDL statements :
 CREATE DATABASE - creates a new database
 ALTER DATABASE - modifies a database
 CREATE TABLE - creates a new table
 ALTER TABLE - modifies a table
◦ Adds or removes a column from a table.
◦ Used in connection with ADD, MODIFY and DROP.
 DROP TABLE - deletes a table
◦ Deletes all rows & removes the table from the database.
 CREATE INDEX - creates an index (search key)
 DROP INDEX - deletes an index

Adopt by Tsegay M.
 SELECT - extracts data from a database
◦ - retrieves rows from a table.
◦ Specifies which columns to include in the result set.
 UPDATE - updates data in a database
◦ Modifies existing rows in a table.
 DELETE - deletes data from a database
◦ Removes a specified row or a set of rows from a table.
 INSERT INTO/ insert - inserts new data into
a database
◦ Adds rows to a table.

Adopt by Tsegay M.
 GRANT: to allow specified users to perform
specified tasks

 REVOKE: to cancel previously granted or


denied permissions
 DENAY: restricts users not to perform tasks

Adopt by Tsegay M.
Adopt by Tsegay M.
 Toidentify database object on
which the statement should act
◦Table name
◦Column name

Adopt by Tsegay M.
 Character strings:
Data type Description
Fixed-length character string. Maximum 8,000 characters
char(n) or
character(n)

varchar(n) Variable-length character string. Maximum 8,000 characters


varchar(max) Variable-length character string. Maximum 1,073,741,824
characters

text Variable-length character string. Maximum 2GB of text data

Adopt by Tsegay M.
Data type Description Storage
Tiny Int Allows whole numbers from 0 to 255 1 byte
Small Int Allows whole numbers between -32,768 and 32,767 2 bytes
Int Allows whole numbers between -2,147,483,648 and 4 bytes
2,147,483,647
Big Int Allows whole numbers between -9,223,372,036,854,775,808 and 8 bytes
9,223,372,036,854,775,807
decimal(p,s) Fixed precision and scale numbers. 5-17
Allows numbers from -10^38 +1 to 10^38 –1. bytes
numeric(p,s) Fixed precision and scale numbers. 5-17
Allows numbers from -10^38 +1 to 10^38 –1. bytes
Small money Monetary data from -214,748.3648 to 214,748.3647 4 bytes
money Monetary data from -922,337,203,685,477.5808 to 8 bytes
922,337,203,685,477.5807
float(n) Floating precision number data from -1.79E + 308 to 1.79E + 4 or 8
308. bytes
real Floating precision number data from -3.40E + 38 to 3.40E + 38 4 bytes

Adopt by Tsegay M.
Data type Description Storage
Date time From January 1, 1753 to December 31, 9999 with an accuracy 8 bytes
of 3.33 milliseconds
Date time2 From January 1, 0001 and December 31, 9999 with an accuracy 6-8 bytes
of 100 nanoseconds
Small date From January 1, 1900 to June 6, 2079 with an accuracy of 1 4 bytes
time minute
date Store a date only. From January 1, 0001 to December 31, 9999 3 bytes

time Store a time only to an accuracy of 100 nanoseconds 3-5 bytes

Date time The same as datetime2 with the addition of a time zone offset 8-10 bytes
offset
Time stamp Stores a unique number that gets updated every time a row gets
created or modified. The timestamp value is based upon an
internal clock and does not correspond to real time. Each table
may have only one timestamp variable

Adopt by Tsegay M.
 Identify the hardware
&software requirements
before installation.

Adopt by Tsegay M.
Adopt by Tsegay M.
The Create
command:-
Create SQL DB
Create table

Adopt by Tsegay M.
 Syntax
CREATE DATABASE database name

 Example: Now we want to create a database called


"my_db".

CREATE DATABASE my_db

Adopt by Tsegay M.
 Database Tables
◦ A database contains one/more tables.
Each table is identified by a name (e.g.
"Customers" or "Orders").
◦ Tables contain records/rows with data.
 Syntax CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
)
Adopt by Tsegay M.
 Create table by the name Student with columns: Id,
Name, Sex, section.
CREATE TABLE Student
(
Id int,
Name varchar(25),
Sex char(1),
Section varchar(25)
)
Id Name Sex Section

Adopt by Tsegay M.
 Create database by the name SQL Student
 Create the following table using access procedures.
Student Detail IDNo Fname Lname Age Sex

001 Derartu Gudeta 19 F


002 Kebron Solomo 21 M
n
003 Alemu Ebisa 20 M

 Create the following table using SQL statement Course


Detail CseCode CseName Crdthrs Instractor
IT010 Database 80 Selam
C202 SPSS 90 Hlina
M220 Maintenan 90 Teshome
ce
Adopt by Tsegay M.
 Used to limit the type of data that can go into a table.
 Can be specified when a table is created (with the
CREATE TABLE statement) or after the table is
created (with the ALTER TABLE statement).
 Major constraints
◦ NOT NULL
◦ UNIQUE
◦ PRIMARY KEY
◦ FOREIGN KEY
◦ CHECK
◦ DEFAULT

Adopt by Tsegay M.
 Enforces a column to NOT accept NULL values.
 Enforces a field to always contain a value.

CREATE TABLE Employee


(Id int NOT NULL,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Address varchar(10),
City varchar(8))

Adopt by Tsegay M.
 Primary keys must contain unique
values.
 Can’t contain NULL values.
 Each table should have a primary key.

Adopt by Tsegay M.
CREATE TABLE Employee
(Id int NOT NULL PRIMARY KEY,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Address varchar(10),
City varchar(8))

CREATE TABLE Employee


(Id int NOT NULL,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Address varchar(10),
City varchar(8),
CONSTRAINT pk_ID PRIMARY KEY (Id,LastName))
Adopt by Tsegay M.
ALTER TABLE Student detail ADD PRIMARY KEY (Id)

ALTER TABLE Persons ADD CONSTRAINT pk_ID


PRIMARY KEY (Id, LastName)

Adopt by Tsegay M.
ALTER TABLE Employee DROP
CONSTRAINT pk_ID

Adopt by Tsegay M.
CREATE TABLE employee
(
Empno char(5),
fname varchar(15),
lname varchar(15),
salary numeric (8,2),
pno char(2), primary key(Empno, pno)
)

Adopt by Tsegay M.
A FOREIGN KEY in one table points
to a PRIMARY KEY in another table.
 FK is used to prevent actions that
would destroy link between tables.

Adopt by Tsegay M.
"Persons" table:
P_Id LastName FirstName Address City

1 Hansen Ola Timoteivn 10 Sandnes

2 Svendson Tove Borgvn 23 Sandnes

3 Pettersen Kari Storgt 20 Stavanger

"Orders" table: O_Id OrderNo P_Id


1 77895 3
2 44678 3
3 22456 2
4 24562 1
Adopt by Tsegay M.
 On CREATE table
CREATE TABLE Orders
(O_Id int PRIMARY KEY, OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id))

 On ALTER table
ALTER TABLE Orders
ADD FOREIGN KEY (P_Id) REFERENCES Persons (P_Id)
Or
ALTER TABLE Orders
ADD CONSTRAINT fk_PerOrders FOREIGN KEY
(P_Id)REFERENCES Persons(P_Id)

 To DROP FK
ALTER TABLE Orders
DROP CONSTRAINT fk_PerOrders

Adopt by Tsegay M.
 on CREATE TABLE
CREATE TABLE Persons
(P_Id int NOT NULL, LastName varchar(255) NOT NULL,
FirstName varchar(255), Address varchar(255), City
varchar(255) DEFAULT 'Sandnes')

 on ALTER TABLE
ALTER TABLE Persons
ALTER COLUMN City SET DEFAULT 'SANDNES'

Adopt by Tsegay M.
ID FName Sex

Edu01 Weinshet F
Edu02 Tadesse M
Edu03 Beti F
Edu04 Truwork F

Adopt by Tsegay M.
On create table
CREATE TABLE employee3
(
Emp no char(5) primary key,
fname varchar(15),
lname varchar(15),
salary numeric(8,2)
constraint sal_chk check(salary between 200 and 5000)
)

On alter table
ALTER TABLE employee3 ADD CONSTRAINT sal_chk CHECK (salary between 200 and
5000)

Adopt by Tsegay M.
INSERT INTO Stud Info (ID,
Fname)
VALUES (‘IT005’, ‘Habtamu’)

Adopt by Tsegay M.
Alter table customer Alter column sex drop default

Alter table customer Alter column MobileNo drop


Unique

Alter table Orders Alter column OrdDate drop Check

Adopt by Tsegay M.
 Used to select data from a database

Syntax

SELECT column_name(s)FROM table_name

SELECT * FROM table_name

Adopt by Tsegay M.
SELECT DISTINCT column_name(s)FROM
table_name

Example: student, course table

Adopt by Tsegay M.
 Used to filter records or to extract only
those records that fulfill a specified
criterion.

Syntax

SELECT column_name(s)
FROM table_name
WHERE column_name operator value

Adopt by Tsegay M.
 Select * FROM StudInfo WHERE
Sex=‘F’
 SELECT * FROM StudInfo WHERE
Fname ‘T%’
 SELECT EmpID, EmpName WHERE
Salary > 4000

Adopt by Tsegay M.
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to return for at least
one of the columns

Adopt by Tsegay M.
 SELECT * FROM Persons WHERE FirstName='Tove‘
AND LastName='Svendson‘

Example
SELECT *
FROM StudInfo
WHERE ID ‘E%’
AND Sex=‘F’

Adopt by Tsegay M.
Example
 SELECT * FROM Persons
WHERE FirstName='Tove‘ OR
FirstName='Ola‘
 SELECT * FROM StudInfo
WHERE Fname=‘B%’
OR Sex=‘M

Adopt by Tsegay M.
 SELECT * FROM StudInfo
WHERE Sex=‘F’
AND (Fname=‘T%’ OR Fname=‘W%’

Adopt by Tsegay M.
 Used to sort the result-set by a
specified column
 Sort the records in ascending order
by default.
 If you want to sort the records in a
descending order, you can use the
DESC keyword.
Adopt by Tsegay M.
 Syntax
◦ SELECT column_name(s)
FROM table_name
ORDER BY column_name(s) ASC|DESC
 Example
◦ SELECT Fname, Sex
FROM StudInfo
ORDER BY Fname ASC/DESC

Adopt by Tsegay M.
 Used to update existing records in a table
Syntax
 UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Adopt by Tsegay M.
 Example
◦UPDATE StudInfo
SET Fname=‘Asnake’, Sex=‘M’
WHER ID= ‘Edu04’

Adopt by Tsegay M.
 Used to delete records/ rows in a table.
 Syntax
◦ DELETE FROM table_name
WHERE some_column=some_value
 Example
◦ DELETE FROM Project
WHERE PjtNo=N08

Adopt by Tsegay M.
 Delete All Rows
◦DELETE FROM table_name
or
◦DELETE * FROM table_name

Adopt by Tsegay M.
 The ALTER TABLE statement is used to add, delete, or
modify columns in an existing table.

Adopt by Tsegay M.
SQL ALTER TABLE Syntax
To add a column in a table, use the following syntax:
ALTER TABLE table_name ADD column_name datatype

Example
alter table student add section char (8)

Adopt by Tsegay M.
 To change the data type of a column in a table, use the
following syntax:

ALTER TABLE table_name ALTER COLUMN column_name


datatype

 Example
◦ Alter table student alter column age int

Adopt by Tsegay M.
To delete a column in a table, use the following syntax
(notice that some database systems don't allow deleting a column):

ALTER TABLE table_name DROP COLUMN column_name

Example:
ALTER TABLE student drop COLUMN sex

Adopt by Tsegay M.
 Auto-increment allows a unique number to be
generated when a new record is inserted into a table.

CREATE TABLE Stud info (P_Id int PRIMARY KEY


IDENTITY,
LastName varchar(25) NOT NULL,
FirstName varchar(25),
Levels char(15))

Adopt by Tsegay M.

You might also like