0% found this document useful (0 votes)
18 views

Schema Diagram

Uploaded by

dhivya2310943
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Schema Diagram

Uploaded by

dhivya2310943
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Database Design

Relational Model
• In this model, data is organised in two-dimensional tables and the
relationship is maintained by storing a common field.
• This model was introduced by E.F Codd in 1970, and since then it
has been the most widely used database model, in fact, we can say
the only database model used around the world.
• The basic structure of data in the relational model is tables. All the
information related to a particular type is stored in rows of that
table.
• Hence, tables are also known as relations in relational model.
• In the coming tutorials we will learn how to design tables, normalize
them to reduce data redundancy and how to use Structured Query
language to access data from tables.
Relational Model
Overview of Database Design Process
Steps - Database Design Process
1. Collecting and analyzing all the requirements
◼ Requirements collection and analysis
 Database designers interview prospective database
users to understand and document their data
requirements
◼ Functional requirements
 User defined operations that will be applied to the
database (retrievals and updates)
Steps - Database Design Process
2. Conceptual design
◼ Create a conceptual schema for the database using a high-level
conceptual data model
◼ Includes detailed descriptions of the entity types,
relationships and constraints
◼ Used as reference to ensure that all users’ data
requirements are met and that the requirements do not
conflict
◼ Modifications to the conceptual schema can be introduced if
some functional requirements cannot be specified using the
initial schema
Steps - Database Design Process
3. Logical design or data model mapping
◼ Actual implementation of the database using a commercial
DBMS
4. Physical design
◼ Specifying internal storage structures, indexes, access paths
and file organizations for the database files
◼ Application programs are designed and implemented
Instances and Schemas
• Instance – the actual content of the database at a particular point in
time
• Schema
• overall design of DB, schema changes are infrequent
• The Skeleton of the database is created by the attributes and this
skeleton is named as Schema.
• Schema is mentioning the logical constraints like table, primary key
etc.
• Schema does not represent the data type of the attributes.
Database Schema
• A database schema is a logical representation of data that shows how the
data in a database should be stored logically. It shows how the data is
organized and the relationship between the tables.
• Database schema contains table, field, views and relation between
different keys like primary key, foreign key.
• Data are stored in the form of files which is unstructured in nature which
makes accessing the data difficult. Thus to resolve the issue the data are
organized in structured way with the help of database schema.
• Database schema provides the organization of data and the relationship
between the stored data.
• Database schema defines a set of guidelines that control the database
along with that it provides information about the way of accessing and
modifying the data.
Entities and Attributes
• A real-world thing either living or non-living that is easily recognizable
and no recognizable. It is anything in the enterprise that is to be
represented in our database. It may be a physical thing or simply a
fact about the enterprise or an event that happens in the real world.
• An entity can be place, person, object, event or a concept, which
stores data in the database.
• Attributes are properties used to describe an entity.
• For example: If we consider a car entity, it can have its attributes as a
car's registration number, car's model, car's name, car's color, number
of seats that are there inside the car, etc. Below is the tabular
representation of the car entities.
Entities and Attributes
Entities and Attributes
• A specific entity will have a value for each of its attributes.
• For example a specific employee entity may have Name='John Smith',
SSN='123456789', Address ='731, Fondren, Houston, TX’,
Gender='M', BirthDate='09- JAN- 5 ‘
• Each attribute has a value set (data type) associated with it – e.g.
integer, string, subrange, enumerated type, …
Types of Attributes
• Simple (Atomic) versus Composite
• Single-valued versus Multi valued
• Stored versus Derived
• Null values
Composite vs. Simple Attributes
• Simple
• Each entity has a single atomic value for the
attribute. For example, SSN or Sex.
• Composite
• The attribute may be composed of several
components. For example:
• Address(Apt#, House#, Street, City, State, ZipCode,
Country)
• Name(FirstName, MiddleName, LastName).
• Composition may form a hierarchy where some
components are themselves composite.
Example - composite attribute
Single-valued vs. Multivalued Attributes
• Single-valued
• Attributes have a single value for a particular
entity
• Ex: Age
• Multi-valued
• An entity may have multiple values for that
attribute. For example, Color of a CAR or
PreviousDegrees of a STUDENT.
• Denoted as {Color} or {PreviousDegrees}.
Stored versus Derived Attributes
• Stored versus Derived attribute
• For a person entity the value of age can be
determined from the value of that person’s
Birth_date.
• The Age attribute is called a derived attribute
and is said to be derivable from the Birth_date
attribute which is called as stored attribute.
NULL values
• Null values – in some cases, a particular entity may not have an
applicable value for an attribute
• Ex: Apartment_number, College_degree
• A special value called NULL is created
• NULL can be used
• When not applicable
• Do not the value (unknown)
• Missing – it is known that attribute value exists but is missing
• Not known – it is not known whether the attribute value exists
Complex Attributes
• In general, composite and multi-valued attributes may be
nested arbitrarily to any number of levels, although this is
rare.
• For example, PreviousDegrees of a STUDENT is a composite
multi-valued attribute denoted by {PreviousDegrees (College,
Year, Degree, Field)}
• Multiple PreviousDegrees values can exist
• Each has four subcomponent attributes:
• College, Year, Degree, Field
SCHEMA DIAGRAM
• COMPANY DATABASE:
• EMPLOYEE (SSN, Name, Address, Gender, Salary, SuperSSN, DNo)
• DEPARTMENT (DNo, DName, MgrSSN, MgrStartDate)
• DLOCATION (DNo,DLoc)
• PROJECT (PNo, PName, PLocation, DNo)
• WORKS_ON (SSN, PNo, Hours)
SCHEMA DIAGRAM
SQL

• SQL is a standard database language used to access and manipulate


data in databases.
• SQL stands for Structured Query Language. SQL was developed by
IBM Computer Scientists in the 1970s.
• By executing queries SQL can create, update, delete, and retrieve data
in databases like MySQL, Oracle, PostgreSQL, etc.
• Overall SQL is a query language that communicates with databases.
DDL – Data Definition Language
• DDL used to define the database structure or schema.
• CREATE - to create objects in the database
• ALTER - alters the structure of the database
• DROP - delete objects from the database
• TRUNCATE - remove all records from a table, including all spaces
allocated for the records are removed
• COMMENT - add comments to the data dictionary
• RENAME - rename an object
DML– Data Manipulation Language
• statements are used for managing data within schema objects.
• SELECT - retrieve data from the a database
• INSERT - insert data into a table
• UPDATE - updates existing data within a table
• DELETE - deletes all records from a table, the space for the records
remain
DDL
CREATE TABLE customers ( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL, city varchar2(50) );
CREATE TABLE Example with primary key
CREATE TABLE customers
( customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id)
);
DDL
• ALTER TABLE Statement
Syntax: ALTER TABLE table_name ADD column_name column-
definition;
Example
ALTER TABLE customers ADD customer_age varchar2(50);
• How to add multiple columns in the existing table
• ALTER TABLE table_name
• ADD (column_1 column-definition,
• column_2 column-definition,
• ...
• column_n column_definition);
DDL
• How to modify column of a table
• ALTER TABLE customers MODIFY customer_name varchar2(100) not
null;
• How to drop column of a table
ALTER TABLE customers DROP COLUMN customer_name
• How to rename column of a table
ALTER TABLE table_name RENAME COLUMN old_name to new_name;
DML
• SELECT Statement
Syntax
SELECT expressions FROM tables WHERE conditions;
Example
SELECT * FROM customers;
SELECT age, address, salary FROM customers WHERE age < 25
Insert Statement
INSERT INTO table (column1, column2, ... column_n ) VALUES
(expression1, expression2, ... expression_n );
Example
INSERT INTO suppliers (supplier_id, supplier_name) VALUES (50, 'Flipkart’);
INSERT ALL statement
INSERT ALL
INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, ex
pr_n)
INTO table_name(column1, column2, column_n) VALUES (expr1, expr2, exp
r_n)
INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, ex
pr_n)
UPDATE Statement
• UPDATE table SET column1 = expression1, column2 = expression2,
column_n = expression_n WHERE conditions;
• UPDATE suppliers SET supplier_name = 'Kingfisher' WHERE
supplier_id = 2;
• UPDATE suppliers SET supplier_address = 'Agra', supplier_name =
'Bata shoes' WHERE supplier_id = 1;
DELETE Statement
• Syntax
• DELETE FROM table_name WHERE conditions;
• DELETE FROM customers WHERE name = 'Sohan’;
TRUNCATE TABLE
• TRUNCATE TABLE customers;
• DELETE TABLE customers;
Foreign Keys
• A foreign key is a way to enforce referential integrity within your
Oracle database. A foreign key means that values in one table must
also appear in another table.
• The referenced table is called the parent table while the table with
the foreign key is called the child table. The foreign key in the child
table will generally reference a primary key in the parent table.
• A foreign key can be defined in either a CREATE TABLE statement or
an ALTER TABLE statement.
Foreign Keys
CREATE TABLE supplier
( supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

CREATE TABLE products


( product_id numeric(10) not null,
supplier_id numeric(10) not null,
CONSTRAINT fk_supplier
FOREIGN KEY (supplier_id)
REFERENCES supplier(supplier_id)
);
Foreign Keys

• ALTER TABLE statement


ALTER TABLE products ADD CONSTRAINT fk_supplier FOREIGN KEY
(supplier_id) REFERENCES supplier(supplier_id);

• Primary key alter table statement


• ALTER TABLE supplier ADD CONSTRAINT supplier_pk PRIMARY KEY
(supplier_id);
• ALTER TABLE STUDENT ADD PRIMARY KEY (student_id);
NOT NULL
CREATE TABLE STUDENT (student_id int NOT NULL,FirstName
varchar(255) NOT NULL,LastName varchar(255) NOT NULL,Age int);
Alter table
ALTER TABLE STUDENT MODIFY AGE int NOT NULL;
UNIQUE
CREATE TABLE student (
student_id int NOT NULL UNIQUE,
FirstName varchar(25) NOT NULL,
LastName varchar(25),
Age int
);
Alter table
ALTER TABLE STUDENT ADD UNIQUE(age);
ALTER TABLE Student
ADD CONSTRAINT UC_Person UNIQUE (age);
CHECK Constraint
CREATE TABLE student (student_id int NOT NULL,FirstName varchar(25)
NOT NULL,LastName varchar(25),Age int CHECK (Age>=18))

CREATE TABLE pets( ID INT NOT NULL,Name VARCHAR(30) NOT NULL,


check(GENDER in ('Male', 'Female', 'Unknown')));

Alter table
• ALTER TABLE student ADD CHECK (Age>=19);
• ALTER TABLE Persons ADD CONSTRAINT CHK_PersonAge CHECK
(Age>=18 AND City='Sandnes’);

You might also like