Sei sulla pagina 1di 31

Amity Business School, Noida

IT FOR MANAGERS
Data Models and SQL

- Rakshanda Bhardwaj
Database Models

A database model determines the logical


structure of a database.

To be precise, it fundamentally determines that


in which manner the data can be stored,
organized, and manipulated.
There are five models of DBMS, which are distinguished based
on how they represent the data contained:

The Hierarchical Model


The Network Model
The Relational Model
The Deductive Model
The Object Oriented Model
Deductive Model
A deductive model stores as little data as possible but
compensates by maintaining rules that allow new data
combinations to be created when needed.
Additional facts are deduced in a form of table, but are
manipulated using predicate calculus.
A deductive database system is a database system which can
make deductions based on rules and facts stored in the
database.
Datalog is the language typically used to specify facts, rules
and queries in deductive databases.
Object Oriented Data Model
In the object oriented data model(OODM), both data and
their relationships are contained in a single structure known
as an object.
An object includes information about relationship between
the facts within the object, as well as information about its
relationship with other objects.
The attributes/facts describe the property of an object.
It is the abstraction of the real- word entity.
Introduction to Structured Query
Language (SQL)
SQL is the standard language for accessing the databases in DBMS.
It is a non procedural language but is sequential in nature.
SQL functions fit into two broad categories:
Data Definition Language (DDL): defines the schema of the database
Includes commands to:
Create database objects, such as tables, indexes, and views
Define access rights to those database objects
Data Manipulation Language (DML): provides commands to
manipulate the database
Includes commands to insert, update and delete the data within
database tables
Basic DDL Commands in SQL
CREATE: to define new tables (to define relation schemas)
DROP: to delete table definitions (to delete relation schemas)
ALTER: to change the definitions of existing tables (to change
relation schema)
Other features of DDL:
Specify referential integrity constraints (FKs)
Specify user-defined attributes constraints
Basic DML Commands in SQL
SELECT: a query command that uses relation algebra like
expressions
INSERT: to add new rows to table
UPDATE: to change the state (the value) of rows.
DELETE: to remove rows
Data Types

Data type selection is usually dictated by nature of


data and by intended use
We need to pay close attention to expected use of
attributes for sorting and data retrieval purposes.
Syntax of SQL query
CREATE TABLE table-name
(
column_name1 data type(size),
column_name2 data type(size),
column_name3 data type(size),
....
);

Example: Create a table named Persons

CREATE TABLE Persons


(
PersonID int, PersonID LastName FirstName Address City
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
Example: For a relation/table Customer,

SELECT *
FROM Customer

Result:
SELECT FirstName, LastName, City
FROM Customer

Result:
SELECT Id, FirstName, LastName, City, Country, Phone
FROM Customer
WHERE Country = 'Sweden

Result:
INSERT INTO table-name (column-names)
VALUES (values)

Example:
INSERT INTO Customer (FirstName, LastName, City,
Country, Phone)
VALUES ('Craig', 'Smith', 'New York', 'USA', 1-01-993
2800)

Result: 1 new record inserted.


SELECT column-names
FROM table-name
WHERE condition
ORDER BY column-names

Example: For a table named Supplier,

SELECT CompanyName, ContactName, City, Country


FROM Supplier
ORDER BY CompanyName
The default sort order is ascending, that is, low-high or
a-z.

Result:
UPDATE table-name
SET column-name = value, column-name = value, ...
WHERE condition

Example: For a table named Product,

UPDATE Product
SET IsDiscontinued = 1
WHERE UnitPrice > 50

Result: 7 records updated.


DELETE table-name
WHERE condition

Example: For a relation named Products, delete products which


cost over $50.

DELETE Product
WHERE UnitPrice > 50

Results:
7 records deleted.
SELECT DISTINCT column-name
FROM table-name
Can be used with COUNT and other aggregates:

SELECT COUNT (DISTINCT column-name)


FROM table-name

Example: For a relation named Supplier,

SELECT DISTINCT Country


FROM Supplier
ORDER BY Country
SELECT COUNT (DISTINCT Country)
FROM Supplier

Count

16
AGGREGATE FUNCTIONS : LOGICAL OPERATORS :

SELECT MIN(column-name) SELECT column-names


FROM table-name FROM table-name
WHERE condition1 AND condition2
Example: For a relation Order,
SELECT SUM(TotalAmount) SELECT column-names
FROM Order FROM table-name
WHERE OrderDate= 2013 WHERE column-name BETWEEN
value1 AND value2
SQL Joins
Ability to combine (join) tables on common attributes
is most important distinction between relational
database and other databases
Join is performed when data are retrieved from more
than one table at a time
SELECT column-names
FROM table-name1 JOIN table-name2
ON column-name1 = column-name2
WHERE condition

Example: For Joining two tables Order and Customer,

SELECT OrderNumber, TotalAmount, FirstName, LastName, City,


Country
FROM Customer C JOIN Order O
ON O.CustomerID = C.Id
ORDER BY TotalAmount
CUSTOMER ORDER
Id Id
FirstName OrderDate
LastName
OrderNumber
City
CustomerId
Country
TotalAmount
Phone
THANK YOU.

Potrebbero piacerti anche