This action might not be possible to undo. Are you sure you want to continue?
The SQL is further divided into three sub-languages for various purposes: 1. DDL ( Data Definition Language) 2. DML ( Data Manipulation Language) 3. DCL ( Data Control Language) 1. Data Definition Language (DDL): - DDL provide following commands for various purposes: a. Create: - Create reserved keyword is provided by DDL to create tables, views, etc. b. Alter: - Alter reserved keyword is provided by DDL to modify existing structure of the table. c. Drop: - Drop reserved keyword is provided by DDL to drop the existing structure. 2. Data Manipulation Language (DML): - DML provides commands for various purposes as follows: a. Insert: - Insert reserved keyword is provided by DML to insert records in the existing tables. b. Select: - Select reserved keyword is provided by DML to select data from the data tables. c. Update: - Update reserved keyword is provided by DML to update, or modify existing data in the database. d. Delete: - Delete reserved keyword is provided by DML to delete existing record from the database. 3. Data Control Language (DCL): - DCL provide following command to control: a. Rollbacks. b. Commit. c. Save Point etc. Data Definition Language (DDL): 1. Create: - Data Definition Language provides reserved keyword create to create tables, views, database etc. Syntax: Create table <Table name> (
Column1 name data-type <constraint>, Column2 name data-type <constraint>, Column3 name data-type <constraint> ); Note: - Constraint is optional, and is restrictions. Data Types: 1. Char (Width): - This data type is used to accept characters. Note: • Maximum character allowed is 255. • By default if no width specified default one (1) is taken into consideration. • It is a fixed length data type. Eg. Create table employee ( Name char (30) ); 2. Varchar2 (Width): - It is a varying length data type. Note: - Maximum 2000 characters can be stored. Eg. Create table employee ( Name varchar2 (30) ); 3. Number (width, decimal or point): - It provide support to all types of numeric values. Note: • Maximum 40 digits allow. • By default if no width specified it is taken eight (8). • By default if no point specified zero (0) is taken into consideration. Eg. a. Number (7.2) Output: - 9999.99 b. Number (5.0) or Number (5) Output: - 99999
Q. Create a table name student having following column: Employee no. Name Salary Post Ans: Create table employee ( employee_no varchar2 (30), Name varchar2 (30), Salary number (5, 2), Post varchar2 (20) ); Q. Create a table name directors having following column: Director id Director first name Director last name Director city Ans: Create table directors ( Did varchar2 (10), Dfname varchar2 (20), Dlname varchar2 (15), Dcity varchar2 (30) );
Constraints: 1. NOT NULL. 2. Primary Key. 3. Check Constant. 4. Foreign Key. 5. Cascade Delete. 6. Cascade Update. 1. NOT NULL: - When not null restriction (Constraint) added to column it ensures that the value must passed for the column at the time of entering records. Syntax: Column-Name data-type NOT NULL, * Note: - If NOT NULL explicitly added with column name default null is taken into consideration.
Eg. Create table employee ( Employee-no number (4), Employee-name varchar2 (30) NOT NULL ); 2. Primary Key: - When primary key restriction (constraints) is added to column, it ensure that: * There is no duplicasy in this field values. * NULL should not exist. Syntax: 1. Column-name OR 2. Column-name primary key, data-type Primary key, data-type constraint <restriction-name>
Q. Create a table name student having following columns: Regno. Name Class Address Ans: By first method: Create table student ( Regno Name Class Address ); By Second Method: Create table student ( Regno Name Class Address );
number (15) primary key, varchar2 (30) NOT NULL, number (5) NOT NULL, varchar2 (30)
number (15) constraint regnopk primary key, varchar2 (30) NOT NULL, number (5) NOT NULL, varchar2 (30)
3. Check: - When check reserved keyword is added to column it ensures that only valid data should takes entry.
data-type check (column operator value) OR 2. Column-name data-type constraint <restriction-name> Check (column operator value) Operator used in SQL: 1. Relational Operator. 2. Logical Operator. 3. Special Operator. a. In and not in. b. Between and not between. c. Like and not like.
Relational operator or comparison operator: - This operator are used to compare two values following operator provided to compare values: A. > (Greater than). B. < (Less than). C. >= (Greater than or equal to). D. <= (Less than or equal to). E. !, (<> ) (Not equal to). F. = (Equal to) Restriction based on Relational operator: Q. To create a table name employee having following column and restrictions: Empno Primary key. Empname NOT NULL. Post NOT NULL. Salary Check. Ans: Create table employee ( Empno number (15) key, Empname varchar2 (30) Post varchar2 (10) Salary number (5) ); 2. Logical Operator: - these operators are used to combine more than one conditional expression. Following logical operators provided by SQL: a. AND: - It returns true if both the condition met true, otherwise false. It is Binary Operator. NOT NULL, NOT NULL, constrain salarychk check (salary>0) constraint empnopk primary
b. Or: - It returns true, if either one of the condition met true otherwise it returns false. It is also a binary operator. c. NOT: - It reserves the pulses that is true to false and vice-versa. It is also a binary operator. Check constraint base on Logical operator: Q. To create a table name employee having following column and restrictions: Empno primary key check must between 9000 to 9999 Empname NOT NULL Post programmer, executive, clerk Salary must be greater than 2000 and less than 45000. Ans: Create table employee ( Empno number (15) constraint empnopk primary key Check (Empno>=9000 and Empno<=45000), Empname varchar2 (30) NOT NULL, Post varchar2 (15) constraint postchk Check (post=’prog’ or post=’executive’ or post=’clerk’), Salary number (9) constraint salchk Check (salary>=2000 and salary<=45000) ); Q. Create a table movie as follows: Mid primary key mid must between M001 to M900 Mname NOT NULL Category Action, Thriller, Drama Cost must be either <=200000 or >=500000. Ans: Create table movies ( Mid varchar2 (10) constraint midpk Check (mid>=’M001’ and mid<=’M900’), Mname varchar2 (15) NOT NULL, Category varchar2 (25) constraint catchk Check (category=’Action’ or category=’Thriller’ or category=’Drama’), Cost number (15) constraint costchk Check (cost<=200000 or cost>=500000) ); 3. Special Operator: a. “IN” Operator: - This operator is used to restrict specified within parenthesis. Syntax: Column data-type constraint <restriction-name>
Check (Column-name in (Value1, Value2, Value3 …)) *Notes: a. If specified value are string type it must enclosed within single quote (‘‘). b. No quote (‘‘) require to enclosed number value. c. “IN” operator acts inside as or type. Check constraint based on in and not in operator: Q. Create a table name student having following column and restrictions: Regno primary key <8000 and >2000 Sname NOT NULL Class 1,2,3,4 Sex male, female Ans: Create table student ( Regno number (15) constraint regnopk primary key Check (regno>2000 and regno<8000), Sname varchar2 (15) NOT NULL, Class varchar2 (15) constraint classchk Check (class in (1, 2, 3, 4)), Sex varchar2 (15) constraint sexchk Check (sex in (‘mail’, ’female’)) ); b. ‘NOT IN’ operator: - This operator is used to restrict values based on values specified within parenthesis that is; it accepts value that not matched with the values specified. Syntax: Column data-type constraint <restriction-name> Check (column not in (value1, value2 …)) Note: 1. If specified values are of char/varchar2 type, that is must enclosed within the single quotes. 2. No quotes required for numeric values. 3. NOT IN operator internally works as AND operator. Check constraint based on IN and NOT IN operator: Q. Create a table name directors having following columns and restrictions: Did primary key did must be >=D001 and <=D999 Dfname NOT NULL Dlname must be Ghai, Verma, or Sharma Dcity not Patna, Pune, Bangalore
Ans: Create table directors ( Did Dfname Dlname Dcity );
varchar2 (15) constraint didpk primary key Check (Did>=’D001’ and Did<=’D999’), varchar2 (30) NOT NULL, varchar2 (20) constraint dlnamechk Check (Dlname in (‘Ghai’, ‘Verma’, ‘Sharma’)), varchar2 (30) constraint dcitychk Check (Dcity not in (‘Patna’, ‘Pune’, ‘Bangalore’))
c. BETWEEN and NOT BETWEEN operator: Between operators is used in SQL PLUS to select values based on ranges specified including initial and final limit. Syntax: Column data-type check (column-name between initial and final limit) NOT BETWEEN operators used in SQL PLUS to select values either less than initial values or, greater than final values. That is excluding initial and final limit. Syntax: Column data-type check (column-name not between initial limit and final limit) Check constraint based on BETWEEN and NOT BETWEEN operators Q. Create a table name result having following columns and restriction:Regno primary key check >=9000 and <=9999 SEM I, or II, or III Sub1 marks within (0-100) Sub2 marks must be >=0 and <=200 Total must be either >200 and <300 Ans: Create table result ( regno number (5) constraint regnopk primary key Check (regno between >=9000 and <=9999), SEM varchar2 (5) constraint semchk Check (SEM in (‘I’, ‘II’, ‘III’)), Sub1 number (3) constraint sub1chk Check (Sub1 between 0 and 100), Sub2 number (3) constraint sub2chk Check (Sub2 between 0 and 200), Total number (5) constraint totalchk Check (Total between 200 and 300)); d. LIKE operator: - This operator in SQL is used to restrict value based on Pattern matching. In order to generate pattern SQL provide two wild-card characters as follows. 1. %: - It matches zero, one, or more than one character.
Eg: D, Do, Dabc, are true. doo1, A001 are false. ‘E%K’ => EK, EooK, EoK are true. Ek, KEo1 are false 2. _(Underscore): - It matches exactly one character (required) Eg: ‘D_ _ _’ => D001, DABC are true. D0001, dEad are false. Syntax: Column data-type constraint <restriction-name> check (column like ‘pattern’) Check constraint based on pattern (like) matching Q. To create a table name directors having following columns and restrictions: Did primary key check director’s id must start with ‘D’ followed by any sequence of characters but must end with ‘k’. Dfname NOT NULL Dlname check Dlname must be either Kumar, Sharma, Singh, Verma. Dcity check city of directors must start with M, K, and C Ans: Create table directors ( Did number (8) constraint didpk primary key Check (Did like (‘D%k’)), Dfname varchar2 (30) NOT NULL, Dlname varchar2 (15) constraint dlnamechk Check (Dlname in (‘Kumar’, ‘Sharma’, ‘Singh’, ‘Verma’)), Dcity varchar2 (30) constraint dcitychk Check (Dcity ‘M%’ or, Dcity ‘K%’ or, Dcity ‘C %’) ); e. NOT LIKE operator: - This operator in SQL is used to accept all values other than the specified one. Syntax: Column data-type constraint <restriction-name> check (not like ‘pattern) Q. To create a table name movies having following columns and restrictions: Mid primary key check movie id must start with three characters formed by any characters but must end with S. Mname must be any value except movie name last characters must not be K, M, or N. ‘D%’ =>
Category Cost Ans: Create table movies ( Mid Mname Category Cost
must not be thriller and suspense. must lies within range 200000 to 350000.
varchar2 (10) constraint midpk primary key Check (Mid like ‘_ _ _ %S’), varchar2 (30) constraint mnamechk Check (Mname not like ‘%K’, ‘%M’, ‘%N’), varchar2 (15) constraint categorychk Check (Category in (‘Suspense’, ‘Thriller’)), number (10) constraint costchk Check (Cost between 200000 and 350000)
); Second method for creating table: Q. To create a table name movies having following column and restrictions: Mid primary key must be >=’M001’ and must be <=’M900’ Mname NOT NULL Category Action, Drama, Suspense Cost must lies within range 200000 and 500000 Ans: Create table movies ( Mid varchar2 (10), Mname varchar2 (30) NOT NULL, Category varchar2 (10), Cost number (6), Constraint midpk primary key (MID), Constraint midchk check (Mid >=’M001’ and Mid <=’M900’), Constraint catchk check (Category in (‘Action’, ’Drama’, ’Thriller’), Constraint costchk check (Cost between 200000 and 500000) ); Add more Questions Foreign Key or References: When foreign key restriction is added to column it ensured parent child relation that is there must be matching value available for each foreign key entity instance in its own master table. Syntax: 1. Column data-type constraint <Restriction-name> References <Master-Table> (Column-name) OR 2. Column data-type constraint <Restriction-name>
Foreign key <Column> References <Master-Table> (Column) Q. (By First method) Ans: Create table movies ( Mid varchar2 (10) Mname Category Did ); varchar2 (30) varchar2 (20) varchar2 (10)
constraint midpk primary key Check (mid like ‘M _ _ _ _‘), NOT NULL, constraint catchk Check (Category IN (‘Action’, ‘Drama’, ‘Suspense’)), constraint didfk References directors (Did)
By Second Method: Create table movies ( Mid Mname Category Did Constraint Constraint Constraint ‘Suspense’), Constraint );
varchar2 varchar2 varchar2 varchar2 Midpk Midchk Catchk didfk
(10), (30) NOT NULL, (20), (10), primary key (Mid), Check (Mid Like ‘M _ _ _ _’), Check (Category IN (‘Drama’,
Foreign Key (Did) References Directors (Did)
Q. Ans: Create Table directors ( Did Varchar2 Dfname Varchar2 Dlname Varchar2 Dcity Varchar2 Constraint Didpk Constraint Didchk Constraint Dcitychk );
(10), (20) NOT NULL, (10), (30), Primary key (Did), Check (Did Like ‘D%M’), Check (Dcity IN (‘Mumbai’, ‘Chennai’, ‘Kolkata’))
Create Table Movies ( Mid Varchar2 (10), Mname Varchar2 (30)
Category Cost Constraint Constraint Constraint Constraint Constraint );
Varchar2 (20), Number (10), Midpk Primary Key (Mid), Midchk Check (Mid ‘M _ _ _ _’), Catchk Check (Category NOT IN (‘Suspense’, ‘Thriller’)), Costchk Check (Cost Between 200000 and 500000), Didfk Foreign Key (Did) References Directors (Did) ON DELETE CASCADE
Alter Table: Alter Command: - SQL provide alter command to change or modify existing structure of the table. Syntax: Alter Table <Table Name>Add/Modify/Drop <Statement>; SQL provides three reserved key words that is add, drop, and modify to do the following jobs: 1. Add: - Add is used to add new columns in the existing table. 2. Modify: - Modify is used with a alter command to change the data-type of existing column. 3. Drop: - Drop is used with the alter command to drop the existing constraint. NOTE: - In ORACLE 8i Drop can also be used to drop the existing column. 1. ADD: - Add reserved key word used with alter table command to add new column to existing table. Syntax:a. To add single column: Alter Table <Table-name> Add Column-name <Restriction>; b. To add multiple columns: Alter Table <Table-name> Add (Column1 Data-type <Restriction>, Column2 _ _ _ _ _); Eg: Que: - To add column dfname in existing table directors. Ans: - Alter Table Directors Add dfname varchar2 (10) NOT NULL; Que: - To add two columns Dlname, Dcity in the existing table. Ans: Alter Table Directors Add ( data-type
Dlname varchar2 (20), Dcity varchar2 (10) constraint dcitychk Check (Dcity IN (‘Mumbai’, ‘Chennai’, ‘Kolkata’)) ); To add new restriction using Alter table command: Syntax: a. To add single constraint: Alter Table <Table-name> Add <Restriction>; b. To add multiple constraints on the same table at a time Alter Table <Table-name> Add (<Restriction1>, <Restriction2>, _ _ _ _ _); Note: 1. If table is empty means can not exist any, we can add any new column with restriction or, without restriction. 2. If table consists record, we can add new column without restriction. We can not add new column with restriction. Que: - To add new column Dfname in the existing table director. Ans: If table is empty: Alter Table Director Add Dfname varchar2 (20) NOT NULL;
If table consist of record then: Alter Table Directors Add Dfname varchar2 (20);
Que: - To add new column Dlname and Dcity in the existing table director. Ans: If table is empty then: Alter Table Director Add (Dlname varchar2 (20) NOT NULL, Dcity varchar2 (10) Constraint Dcitychk Check (Dcity IN (‘Mumbai’, ‘Chennai’))); If table consists of record then: Alter Table Director Add (Dlname varchar2 (20), Dcity varchar2 (10)); a. To add single constraint: -
Q. What are the essential components of DBMS? Ans: - The major components of a DBMS are as follows: 1. DML Pre-compiler: - It converts DML statements embedded in an application program to normal procedure calls, in the host language. 2. DDL Compiler: - The DDL compiler converts the Data Definition statements into a table. 3. Database Manager: - A database manager is a program or module which provide the following functions: a. The database manager is responsible for the storing, retrieving and updating of data in the database. b. The database manager is responsible fro security, backup and recovery of database in case of crash or failure and also for integrity enforcement. 4. Query Processor: - The query processor selects the best possible plan or strategy for evaluating and responding to a user query. 5. Database Administrator: - Database administrator is a person which is responsible for super-wising the creation, modification, permission and maintenance of database. 6. File Manager: - The file manager manages the allocation of space on disk storage and the data structure used to represent information stored on disk. Q. Write short notes on: A. Database Manager. B. Query Processor. Ans: a. Database Manager: - Database manager is implemented by a person or group of knowledgeable persons called administrator or DBA. DBA is responsible for superwising the creation, modification, and maintenance of database. DBA also maintains security that the database is not accessible to unauthorized users. The DBA is also responsible for granting permission to use the database to different users. b. Query Processor: - The database user retrieves data by formulating a query, in data manipulation language (DML) provided by the database. The query processor is used to interpret the on-line users query and converts it into effective or efficient series of operations in a form capable of being sent to the database manager.
The query processor uses the data dictionary to find the structure of the relevant portion of the database and uses this information to execute the query. Q. Explain the Independence? Ans: difference between the Physical and Logical Data
Q. Compare and contrast Traditional file Vs Database approach. Ans: In a traditional way, such as in COBOL, the information is used to keep in a file and to manipulate files, number of application programs developed and added. As file oriented approach has limitation regarding sharing of data, the only solution to this is to create new files which records the desired information which can be manipulated by the application developed by the programmers, which depends on the files, following are the drawbacks of the file oriented approach: a. Data Redundancy and Inconsistency: - Since, file are created to get desired information over a long period the various files are likely to have the same information may be duplicated in several files. Eg: - The address and Phone number of the student may appear in a Student File and Marks File. This redundancy leads to data inconsistency; if a particular student address is changed in Student File which not be reflected in Marks File due to limitation of file oriented approach. b. Program or Data Dependency: - In traditional file approach if a new data filed is to be added to a particular file, all such programs that access that particular file would have to be changed to allow for this new field, which would have been added to that particular file recently. This shows the limitation of file oriented approach, where file depends on program and program depends on file. These limitations motivate to use DBMS rather than traditional file approach.
OBJECT ORIENTED DATABASE MANAGEMENT SYSTEM; Common terms of an Object oriented programming language: 1. Object: - The term object means a combination of data or code and program or method that represents some real world entity. Eg: - Consider an employee named Vinay 23 years old and earn salary Rs. 8888/_ then Vinay may be represented in computer program as an object. The code or data part of this object would be Name: - Vinay, Age: - 28 and Salary: 8000. The program or method part of the object may be collections of programs or method i.e.; input, display, update salary, etc. In the Vinay object the name, age and salary are called attributes hold descriptive properties of the object Vinay. 2. Class: All object that have the same attributes for the data part and same program or method part are collectively called a class or type. The term class refers to the collections of all objects that have same attributes and methods. Eg: - Consider a class or type named Employee consist attributes or codes of data name, age, empno, and salary to hold data and methods or program i.e. input, change, display etc. Creating three objects of employee type named Vinay, Arti, and Rupam holds the same attributes and methods define the class or type employee. • Encapsulation: - It is the mechanism by which the data or code part of an object is so encapsulated that the user can’t see inside of object but can use the object by calling the program or method part of the object. • Inheritance: - The term inheritance is some time called re-uses i.e. codes ones defined is re-usable. Inheritance is a process by which a new class or type can be inherits from existing class. A class may inherit from one or more existing classes and the inheritance structure is called an inheritance hierarchy or class hierarchy. Q. Briefly discuss the alternative
This action might not be possible to undo. Are you sure you want to continue?