Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fundamentals of DBMS Notes-1.

Similar presentations


Presentation on theme: "Fundamentals of DBMS Notes-1."— Presentation transcript:

1 Fundamentals of DBMS Notes-1

2 Database vs DBMS Database is a collection of related data stored in several tables, and linked through foreign keys DBMS is a collection of computer programs that is dedicated for the management (i.e. organization, storage and retrieval) of all databases that are installed in a system (i.e. hard drive or network).

3 Three Level Architecture of DBMS
1. Physical Level / Internal Level 2. Conceptual Level / Logical Level 3. External Level / View Level

4 The above diagram, shows the architecture of DBMS.
Mapping is the process of transforming request response between various database levels of architecture. Mapping is not good for small database, because it takes more time. In External / Conceptual mapping, DBMS transforms a request on an external schema against the conceptual schema. In Conceptual / Internal mapping, it is necessary to transform the request from the conceptual to internal levels. 1. Physical Level - Physical level describes the physical storage structure of data in database. It is also known as Internal Level. This level is very close to physical storage of data. At lowest level, it is stored in the form of bits with the physical addresses on the secondary storage device. At highest level, it can be viewed in the form of files. The internal schema defines the various stored data types. It uses a physical data model.

5 2. Conceptual Level - Conceptual level describes the structure of the whole database for a group of users. It is also called as the data model. Conceptual schema is a representation of the entire content of the database. These schema contains all the information to build relevant external records. It hides the internal details of physical storage. 3. External Level- External level is related to the data which is viewed by individual end users. This level includes a no. of user views or external schemas. This level is closest to the user. External view describes the segment of the database that is required for a particular user group and hides the rest of the database from that user group.

6 Data Definition Language (DDL)
Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples: CREATE - to create objects in the database create table emp3 (name varchar(10),salary int); ALTER - alters the structure of the database alter table student add column rno int; //add) ALTER TABLE s1 ALTER COLUMN rno TYPE varchar(40); //modify type and size update s1 set rno='A01' ; alter table s1 alter column mark type numeric(4,2); ALTER TABLE distributors RENAME COLUMN address TO city; ALTER TABLE distributors RENAME TO suppliers; DROP - delete objects from the database drop table emp;

7 Data Manipulation Language (DML)
Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples: SELECT - retrieve data from the a database select * from emp; INSERT - insert data into a table insert into s1 (name,doj,rno) values('kiran',' ',89),('kiran',' ',89); UPDATE - updates existing data within a table update stud set rno='A01' ; DELETE - deletes all records from a table, the space for the records remain delete from s1 where name='kiran';

8 SQL Constraints SQL constraints are used to specify rules for the data in a table.  If there is any violation between the constraint and the data action, the action is aborted by the constraint. Constraints can be specified when the table is created (inside the CREATE TABLE statement) or after the table is created (inside the ALTER TABLE statement).

9 In SQL, we have the following constraints:
NOT NULL - Indicates that a column cannot store NULL value UNIQUE - Ensures that each row for a column must have a unique value PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table CHECK - Ensures that the value in a column meets a specific condition DEFAULT - Specifies a default value for a column

10 NOT NULL The NOT NULL constraint enforces a column to NOT accept NULL values. The NOT NULL constraint enforces a field to always contain a value. This means that you cannot insert a new record, or update a record without adding a value to this field. CREATE TABLE PersonsNotNull ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) ALTER TABLE distributors ALTER COLUMN street SET NOT NULL; To remove a not-null constraint from a column:   ALTER TABLE distributors ALTER COLUMN street DROP NOT NULL;

11 UNIQUE The UNIQUE constraint uniquely identifies each record in a database table. The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. CREATE TABLE Persons ( P_Id int NOT NULL UNIQUE, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

12 To create a UNIQUE constraint on the "P_Id" column when the table is already created, use the following SQL: ALTER TABLE Persons ADD UNIQUE (P_Id) To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

13 PRIMARY KEY The PRIMARY KEY constraint uniquely identifies each record in a database table. Primary keys must contain UNIQUE values. A primary key column cannot contain NULL values. Most tables should have a primary key, and each table can have only ONE primary key. Primary key on single column CREATE TABLE Persons ( P_Id int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) )

14 To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) ) OR CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255), PRIMARY KEY (P_Id,LastName) )

15 PRIMARY KEY Constraint on ALTER TABLE
ALTER TABLE Persons ADD PRIMARY KEY (P_Id); To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax: ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName); To DROP a PRIMARY KEY Constraint ALTER TABLE Persons DROP CONSTRAINT pk_PersonID;

16 FOREIGN KEY (Reference Constraint)
A foreign key is a key used to link two tables together. This is sometimes called a referencing key. Foreign Key is a column or a combination of columns whose values match a Primary Key in a different table. The relationship between 2 tables matches the Primary Key in one of the tables with a Foreign Key in the second table.We say this maintains the referential integrity between two related tables.

17 A foreign key on single column

18 A foreign key on group of columns

19 SQL FOREIGN KEY Constraint on ALTER TABLE
alter table emp2 add constraint f_key foreign key(eno,sno) references emp1(eno,sno); Drop foreign key constraint alter table emp1 drop constrant f_key;

20 CHECK Constraint (Business Rule constraint)
The CHECK constraint is used to limit the value range that can be placed in a column. If you define a CHECK constraint on a single column it allows only certain values for this column. If you define a CHECK constraint on a table it can limit the values in certain columns based on values in other columns in the row.

21 create table emp3 (name varchar(10),sal int check(sal>5000);
create table emp3 (name varchar(10),sal int check(sal>5000 and name like ‘a%’); To add a check constraint to a table and all its children: ALTER TABLE distributors ADD CONSTRAINT zipchk CHECK (sal>5000); To remove a check constraint from a table and all its children: ALTER TABLE distributors DROP CONSTRAINT zipchk;

22 DEFAULT The DEFAULT constraint is used to insert a default value into a column. The default value will be added to all new records, if no other value is specified. CREATE TABLE Persons ( P_Id int NOT NULL, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) DEFAULT 'Sandnes' )

23 Difference between TRUNCATE, DROP and DELETE
The TRUNCATE command is used to delete all the rows from the table and free the space containing the table. Truncate table emp; Removes All rows from a table. Does not require a WHERE clause, so you can not filter rows while Truncating. Faster than delete DELETE Removes Some or All rows from a table. A WHERE clause can be used to remove some rows. If no WHERE condition is specified, all rows will be removed. It removes rows row-by-row one at a time and records an entry in the Transaction logs, thus is slower than TRUNCATE. The DROP command is used to remove an object from the database. If you drop a table, all the rows in the table is deleted and the table structure is removed from the database. Once a table is dropped we cannot get it back.

24 LIKE Predicate The LIKE predicate compares a column of type CHAR or VARCHAR (string) with a pattern. This pattern is also a string, but it may contain two characters with a special meaning. The '_' (underscore) represents exactly one character and '%' (percent) represents a string of zero, one or more characters.

25 ORDER BY The ORDER BY keyword is used to sort the result-set by one or more columns. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in a descending order, you can use the DESC keyword. SELECT * FROM Customers ORDER BY Country; SELECT * FROM Customers ORDER BY Country DESC;


Download ppt "Fundamentals of DBMS Notes-1."

Similar presentations


Ads by Google