1 RDBMS Concepts

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 45

RDBMS Concepts

Database

Database is an organized collection of data. Collection of records stored in a systematic way. Organized for Better Retrieval and Sorting.

Types of Database Models

Hierarchical model - Data is organized into a tree-like structure Network model - Uses records and sets. Records contain fields, sets define man-many relationships. Relational model Geared for OLTP Dimensional Model Geared for Datawarehousing Object Relational Model Object Oriented Features

Basic Constructs

Tables Columns Constraints Indexes Views Procedures SQL

Table & Columns

In a relational database and SQL databases a table is a set of data elements (values) that is organized using a model of horizontal rows and vertical columns. Columns form the attributes of a table. A table consists of one or more columns. Every RDBMS has a restriction on the max columns. Eg: Oracle allows 1000 columns per table

Primary Key

Primary key is a value that can be used to identify a unique row in a table. Primary key is a candidate key chosen as the main method of uniquely identifying a tuple in a relation. Natural Key Key as understood by Business Eg: Associate Id Surrogate Key Non Business Key Eg : Sequence Number

Primary Key..

Should be immutable, its value should not be changed during the course of normal operations of the database. Candidate keys uniquely identifying a row but are likely not to be immutable. Primary key should generally be short to minimize data stored by relations. Can be a compound consisting on multiple columns.

Foreign Keys

A field or group of fields that point to a key field or group of fields usually in a different table. In scenarios where a foreign key refers to the same table itself it is a self key. Ensures Data Integrity Eg : Employee Belonging to a Department

Foreign Keys Example


Employee
ASSOCIATE ID FIRST NAME LAST NAME DEPT NO

Department
DEPT NO DEPT NAME

Not Nulls

10

Constraint - Not Null

Enforces the need for data to be mandatory. Applied at column level. Record cannot be created if value is not supplied. Eg: First Name is mandatory.

11

Constraint - Check

Enforces the values entered are from a fixed List of Values. Valid only when a fixed list is present and action is more for validation. For variable list a Master Detail with Foreign Keys must be used. Eg: A flag indicating the gender M or F.

12

Defaults

Provides for a default value when none is provided. Useful when user wants to avoid specifying a value every time. Eg: Default the date to todays date.

13

Defaults

14

Indexes

Feature in a database that allows quick access to the rows in a table. Smaller than the original table. Optimized for quick searching.
Eg : Search on the name of an associate

15

Index Types..

Primary Key Indexes Unique Key Indexes Non-unique Indexes Composite Indexes Foreign Key Indexes Bitmap Indexes Function Based Indexes

16

SQL Select

Structured Query Language Way to access data. Uses simplified English SQL is a database access, nonprocedural language. Users describe in SQL what they want done, and the SQL language compiler automatically generates a procedure to navigate the database and perform the desired task.
Eg: Get all the employess Select * from employee


17

SQL Select..

Get all employees whose salary > 5000 Select * from employee where salary > 5000 Get associate id and first name Select associate_id,first_name from employee

18

Samples of Select

19

SQL Standards

ANSI SQL The American National Standards Institute (ANSI) has established a set of industry standards for SQL. Set of standards which SQL language must be complainant with. Database vendors introduce non ANSI complaint features. Reduction in portability due to noncompliance.

20

SQL Joins

Combines records from two or more tables in a relational database.

21

Cross Join

A cross join returns the cartesian product of the sets of rows from the joined tables. While not used commonly, a cross join is the foundation upon which inner joins are built.
SELECT * FROM employee CROSS JOIN department; SELECT * FROM employee ,department;


22

Inner Join

Intersection between the two tables

SELECT * FROM employee ,department WHERE employee.DepartmentID = department.DepartmentID

23

Left outer join

Instead of limiting results to those in both tables, it limits results to those in the "left" table SELECT * FROM employee LEFT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID

24

Right outer join

Much like a left outer join, except that the tables are reversed SELECT * FROM employee RIGHT OUTER JOIN department ON employee.DepartmentID = department.DepartmentID

25

Full outer join

Combines the results of both left and right outer joins. SELECT * FROM employee FULL OUTER JOIN department ON employee.DepartmentID = department.DepartmentID

26

SQL Inserts

Adds a record to a table in a relational database. INSERT INTO table (column1, [column2, ... ]) VALUES (value1, [value2, ...])
Eg: INSERT INTO phone_book (name, number) VALUES ('John Doe', '555-1212');

27

SQL Updates

Updates an existing record from a table in a relational database. Eg: Update phone_book set name = John Davis where name ='John Doe';

28

SQL Delete

Deletes an existing record from a table in a relational database. Eg: Delete phone_book where name = John Davis

29

Commit/Rollback

Signifies the end of a transaction unit. Commit Means finalize and write all these changes to the database. Rollback Revert back all changes made to the database. Valid from the point when the last commit/rollback was issued.

30

Transaction

31

SQL Types

DML Data Manipulation Language Changes to Data Eg: Select, Insert, Update, Delete
DDL Data Definition Language Changes to structure Eg: Create Table

32

Sample Create Table

CREATE TABLE EMP ( EMPNO NUMBER(9), ENAME VARCHAR2(10 BYTE), JOB VARCHAR2(9 BYTE), MGR NUMBER(4), HIREDATE DATE, SAL NUMBER(7,2), COMM NUMBER(7,2), DEPTNO NUMBER(2) );
CREATE UNIQUE INDEX PK_EMP ON EMP (EMPNO); ALTER TABLE EMP ADD ( CONSTRAINT PK_EMP PRIMARY KEY (EMPNO)); ALTER TABLE EMP ADD ( CONSTRAINT FK_DEPTNO FOREIGN KEY (DEPTNO) REFERENCES DEPT (DEPTNO));

33

Triggers

Applicable to a table. Way to enforce complex integrity rules which cannot be implemented by constraints. Consists of a SQL statements along with procedural constructs. Get fired when the relevant condition is met, irrespective of the source of the transaction.

34

Stored Procedures

A procedure or function is a object that consists of a set of SQL statements, grouped together, stored in the database Contains business logic and/or complex logic which cannot be done by simple SQL Procedures and functions let you combine the ease and flexibility of SQL with the procedural functionality of a structured programming language.

35

Query Optimizer

The query optimizer determines the most efficient way to execute a SQL statement. Considers many factors related to the objects referenced and the conditions specified in the query. Tries to in an automated way find the most efficient way to execute a query.

36

Views

Tailored presentation of the data contained in one or more tables or other views. Can be thought of as a stored query or a virtual table. Eg: The employees table has several columns and numerous rows of information. If you want users to see only five of these columns or only specific rows, then you can create a view of that table.

37

Sample View

38

Security

Schema Level Security Login Object Level Security Tables, Views etc

39

ACID

Refers to Atomicity, Consistency, Isolation and Durability. Key properties of any DBMS without which integrity of the database cannot be guaranteed. Terms relevant wrt to a Transaction Transaction is a single logical operation Eg : Transfer of Funds (consists of multiple steps)

40

Atomicity

Guarantee that either all of the tasks of a transaction are performed or none of them are. Transfer of funds can be completed or it can fail for a multitude of reasons, but atomicity guarantees that one account won't be debited if the other is not credited as well.

41

Consistency

Database maintains it state when the transaction begins and when it ends. Transaction cannot break the rules, or integrity constraints, of the database. If an integrity constraint states that all accounts must have a positive balance, then any transaction violating this rule will be aborted.

42

Isolation

Ability to make operations in a transaction appear isolated from all other operations. No operation outside the transaction can ever see the data in an intermediate state. Transferred funds can be seen on one account or the other, but never on both even if the query runs while the transfer was still being processed.

43

Durability

Guarantee that once the user has been notified of success, the transaction will persist, and not be undone. It will survive system failure, the database has checked the integrity constraints and won't need to abort the transaction.

44

Common Databases in Market

Oracle SQL Server DB2 Teradata Sybase My SQL

45

You might also like