Presentation is loading. Please wait.

Presentation is loading. Please wait.

INTRODUCTION. Relational Database Concept – Dr. E. F. Codd proposed the relational model for database systems in 1970. – It is the basis for the relational.

Similar presentations


Presentation on theme: "INTRODUCTION. Relational Database Concept – Dr. E. F. Codd proposed the relational model for database systems in 1970. – It is the basis for the relational."— Presentation transcript:

1 INTRODUCTION

2 Relational Database Concept – Dr. E. F. Codd proposed the relational model for database systems in 1970. – It is the basis for the relational database management system (RDBMS). – The relational model consists of the following: Collection of objects or relations Set of operators to act on the relations Data integrity for accuracy and consistency RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.

3 Definition of a Relational Database A relational database is a collection of relations or two-dimensional tables. With the Oracle server, you can store and manage information by using the SQL language and PL/SQL engine. Oracle server Table name: EMPLOYEES Table name: DEPARTMENTS ……

4 Relation or Table The Entity information in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of columns and rows. Remember, a table is the most common and simplest form of data storage in a relational database. Example:

5 Components of table Attribute/Column/Field: The characteristics of entity are called attribute. The values of these attributes are called Attribute values. Tuple/Record/Row: The value of multiple fields placed in the horizontal plane.

6 DATA MODEL Model of system in client’s mind Entity model of client’s model Tables on disk Oracle server Table model of entity model

7 Entity-Relationship Model – Create an entity relationship diagram from business specifications or narratives: EMPLOYEE #* number *name ojob title DEPARTMENT #* number *name olocation assigned to composed of

8 ENTITY

9 Relating Multiple Tables – Each row of data in a table is uniquely identified by a primary key (PK). – You can logically relate data from multiple tables using foreign keys (FK). Primary key Foreign key Table name: EMPLOYEES Table name: DEPARTMENTS …

10 Relational Database Properties A relational database: – Can be accessed and modified by executing structured query language (SQL) statements – Contains a collection of tables with no physical pointers – Uses a set of operators

11 Communicating with an RDBMS Using SQL SQL statement is entered.Statement is sent to Oracle server. Oracle server SELECT department_name FROM departments;

12 Oracle’s Relational Database Management System User tables Data dictionary Oracle server

13 SQL SQL (Structured Query Language) is the most common language used to communicate with relational database management system. It was first originated at the IBM Research Laboratory in San Jose, California. The project name was SYSTEM R whose purpose was to validate the feasibility of the implementation of relational model in database management system. The name of the language used in System R was ‘sequel’ which was later replaced to SQL. After the popularity of system R project, a number of commercial RDBMS products were released, like SQL/DS of IBM. Other products included ORACLE from ORACLE Corp, INGRES from Relational Technology (1981), SYBASE from SYBASE Inc. (1986) and SQL Server from MICROSOFT.

14 SQL BASICS SQL is used to communicate with any Relational Database. Its primary function is to support the definition, manipulation and management of data in relational database. It provides the syntax and semantics of data definition and manipulation of data. It also provides an interface for portability of database definition and application programs.

15 Advantages (a)Non-procedural in nature:- SQL is a non procedural or declarative language. A programmer just has to specify WHAT is needed. (b) Interactive and embedded SQL :- Allows the user to issue commands directly to the database management system and result is produced immediately. SQL commands can be embedded inside a program written in high level languages like C, C++, Java etc. The embedded SQL provides the additional programming features, which are not there in SQL language.

16 Advantages (c ) Portability (d) Cross Platform Communication (e) Easy to learn:- SQL is termed as English like language, which is very easy to learn and adopt. (f) SQL operations work for a set of rows rather than record-by-record. It makes SQL more powerful than the other languages like COBOL.

17

18 Data Definition Language Data Definition Language (DDL) statements are used to define the database structure or schema. Includes the commands used to define or modify the database objects like tables, views, index etc. Sets up, changes, and removes data structures from table.

19 Data Manipulation Language Data Manipulation Language (DML) statements are used for managing data within schema objects. Includes statements that allow the processing or manipulation of database objects. Retrieves data from the database, enters new rows, changes existing rows, and removes unwanted rows from tables in the database.

20 Other languages Data Control Language which is having DBA related commands. It gives or removes access rights to both the oracle database and structures within it. Transactional control languages: Manages the changes made by DML statements. Changes to data can be grouped together into logical transactions.

21 CREATING TABLE Creating Table:- A table can be created in the Database by using CREATE TABLE command. Structure : CREATE TABLE ( Column1datatype[constraint], Column2datatype[constraint],……….. Column ndatatype[constraint], ); Where, tablename is the name of the table, to be created, which can be from 1 to 30 characters long. It must begin with a alphabet, rest of the characters can be any combination of letters, digits or the underscore ( _ ). It must not be a SQL reserved word. For example, employees is a valid name whereas, 3student is invalid as its first character is a digit. Column1, Column2 …. Column n are the attributes of the table. Same rules as of table also apply for column names. The name of the columns should be chosen so that one item of data is stored for each record.

22 DATATYPES DATATYP E DESCRIPTION CHAR (size) To store alphanumeric type of data of fixed length. Maximum value is 2000 characters. Default value of size is 1. Value will be padded with spaces on the right until it reaches the size of characters in length. VARCHAR 2 (size) Similar to CHAR but can store variable number of characters. Maximum length is 4000 characters. No default value is allowed. VARCHAR (size) Synonymous with VARCHAR2 however likely to change in future versions of Oracle. It is suggested to use VARCHAR2. NUMBER (size) Stores fixed and floating point number with maximum precision in 38 digits. NUMBER (P,S) Precision(P) determines the maximum length of data, whereas, SCALE (S) denotes the number of places to the right of decimal. If scale is omitted then default is zero. DATEStores date in a table. The default format is DD-MON-YYYY.

23 DATATYPES DATATYPE DESCRIPTION LONGCan store up to 2 GB of characters. Only one long column per table is allowed. RAWUsed to store binary data such as graphics, sound, animation etc. Maximum permissible data is of 2000 bytes. LONG RAWContains raw binary data otherwise the same as LONG column. The values entered must be in hex notation. Maximum limit is 2 GB. BFILEExternal Binary File. Maximum Limit is 4 GB.

24 Constraint Constraint are the conditions which can be attached to a column(s). It works like a flag. Whenever user attempts to load a value in that column, this value is checked against the data constraint. These constraints can either be placed at the column level or at the table level. Column level constraints can be applied to any one column at a time. They are local to that a specific column. The table level constraints are applied when the constraint definition contains generally more than one column. These constraints are stored as a part of the global table definition. The general syntax for table level constraint is Syntax: CONSTRAINT (column) Where, constraint_name is the name of the constraint. type_constraint is any constraint listed below and column is the name of the attribute on which constraint is applied.

25 CONSTRAINTS NULL: If a row lacks a data value for a particular column, that value is said to be NULL. Column of any data type may contain NULL unless the column was defined as NOT NULL, when table was created. In Oracle, all columns are by default NULL. NOT NULL: If a column is defined as NOT NULL, then it becomes a mandatory column i.e. the user has to enter the data into it. It can take duplicate values if it is not a unique column but cannot take Nulls.

26 CONSTRAINTS UNIQUE: It prevents duplicate values being entered in the column. A table may have more than one unique keys. An example of this key is …., phone NUMBER (7) UNIQUE,…. The syntax for having this constraint at table level will be …, phone NUMBER (7), CONSTRAINT uq_phone UNIQUE(phone)…

27 CONSTRAINTS CHECK: This constraint is used to enforce some rules that can be evaluated based on a logical expression. … roll_no NUMBER(4) CHECK (roll_no > 35) … roll_no VARCHAR2(4), CONSTRAINT chk_roll CHECK (roll_no LIKE ‘a%’)

28 CHECK Define a check column constraint: CREATE TABLE distributors ( did NUMBER (5) CHECK (did > 100), name varchar(40) ); Define a check table constraint: CREATE TABLE distributors ( did NUMBER (5), name varchar(40), CONSTRAINT con1 CHECK (did > 100 )

29 CONSTRAINTS PRIMARY KEY: A primary key is a one or more column used to uniquely identify each row in the table. These values should never be changed and should never be NULL. An example of primary key syntax is given here … empno NUMBER (4) PRIMARY KEY,… This constraint can also be applied at table level as follows. …empno NUMBER(4), CONSTRAINT pk_empno PRIMARY KEY (empno),

30 PRIMARY KEY CREATE TABLE films ( code char(5), title varchar(40), date_prod date kind varchar(10), CONSTRAINT code_title PRIMARY KEY(code,title) ); CREATE TABLE distributors ( did NUMBER PRIMARY KEY, name varchar(40) );

31 CONSTRAINTS(FOREIGN KEY) Foreign key represents relationship between tables. A foreign key is a column in one base table whose values are required to match some specific primary key value in some other base table. A relation or table in which foreign key is defined is called foreign table or referencing relation. A relation or table in which primary key is defined and is referenced by foreign key is called primary table or referenced relation. A foreign key value can be left null. It rejects an INSERT or UPDATE of a value in foreign table, if a corresponding value does not currently exist in the primary key table. It also rejects DELETE from the primary table if corresponding records exists in foreign key table.

32 FOREIGN KEY An example of creating foreign key constraint is given here … deptno NUMBER(2) REFERENCES dept(deptno)…. The syntax at table level: … deptno NUMBER(2), CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES dept (deptno)…., Where fk_deptno is the name of the foreign key constraint.

33 CONSTRAINTS DEFAULT: We can assign a default value to the columns. Now when a row is inserted in the table then all the corresponding columns will take their values as specified. If some column is not having a value then in place of NULL it will store the default value. CREATE TABLE Persons ( P_Id NUMBER NOT NULL, LastName varchar2(255) NOT NULL, FirstName varchar2(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' );

34 CREATE TABLE EXAMPLE An example of creating table is given below CREATE TABLE emp ( empno NUMBER (4) PRIMARY KEY, ename VARCHAR2(20), job VARCHAR(20), sal NUMBER(4), comm NUMBER(4), hiredate DATE, deptno NUMBER(2), CONSTRAINT chk_ename CHECK (ename=UPPER (ename)), CONSTRAINT chk_sal CHECK (sal>comm), CONSTRAINT fk_deptno FOREIGN KEY (deptno) REFERENCES DEPARTMENT(deptno) );

35 CONSTRAINTS Create Table Department( Dname VARCHAR2(10) NOT NULL, DEPTNO NUMBER NOT NULL, MGRENO CHAR(9) NOT NULL, MGRSTARTDATE DATE, PRIMARY KEY (DNUMBER), UNIQUE (DNAME) );


Download ppt "INTRODUCTION. Relational Database Concept – Dr. E. F. Codd proposed the relational model for database systems in 1970. – It is the basis for the relational."

Similar presentations


Ads by Google