Escolar Documentos
Profissional Documentos
Cultura Documentos
Introduction to Database:
1.0 Introduction :
Database is a collection of related data. Database management system is software
designed to assist the maintenance and utilization of large scale collection of data. DBMS
came into existence in 1960 by Charles. Integrated data store which is also called as the
first general purpose DBMS. Again in 1960 IBM brought IMS-Information management
system. In 1970 Edgor Codd at IBM came with new database called RDBMS. In 1980
then came SQL Architecture- Structure Query Language. In 1980 to 1990 there were
advances in DBMS e.g. DB2, ORACLE.
Data
• Data is raw fact or figures or entity.
• When activities in the organization takes place, the effect of these activities need
to be recorded which is known as Data.
Information
• Processed data is called information
• The purpose of data processing is to generate the information required for
carrying out the business activities.
In general data management consists of following tasks
• Data capture: Which is the task associated with gathering the data as and when
they originate.
• Data classification: Captured data has to be classified based on the nature and intended
usage.
• Data storage: The segregated data has to be stored properly.
• Data arranging: It is very important to arrange the data properly
• Data retrieval: Data will be required frequently for further processing,
Hence it is very important to create some indexes so that data can be retrieved
easily.
• Data maintenance: Maintenance is the task concerned with keeping the data upto-
date.
• Data Verification: Before storing the data it must be verified for any error.
• Data Coding: Data will be coded for easy reference.
• Data Editing: Editing means re-arranging the data or modifying the data for presentation.
• Data transcription: This is the activity where the data is converted from one form into
another.
• Data transmission: This is a function where data is forwarded to the place where it would
be used further.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
Metadata (meta data, or sometimes meta information) is "data about data", of any sort
in any media. An item of metadata may describe a collection of data including multiple
content items and hierarchical levels, for example a database schema. In data processing,
metadata is definitional data that provides information about or documentation of other
data managed within an application or environment. The term should be used with
caution as all data is about something, and is therefore metadata.
Database
• Database may be defined in simple terms as a collection of data
• A database is a collection of related data.
• The database can be of any size and of varying complexity.
• A database may be generated and maintained manually or it may be computerized.
DBMS Utilities
• A data loading utility:
Which allows easy loading of data from the external format without writing
programs.
• A backup utility:
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
DBMS
1. DBMS is a collection of data and user is not required to write the procedures for
managing the database.
2. DBMS provides an abstract view of data that hides the details.
3. DBMS is efficient to use since there are wide varieties of sophisticated techniques to
store and retrieve the data.
4. DBMS takes care of Concurrent access using some form of locking.
5. DBMS has crash recovery mechanism, DBMS protects user from the effects of system
failures.
6. DBMS has a good protection mechanism.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
• Data Recovery and Concurrency: Recovery of the data after system failure and
concurrent access of records by multiple users is also handled by DBMS.
• Data Dictionary Maintenance: Maintaining the data dictionary which contains the
data definition of the application is also one of the functions of DBMS.
• Performance: Optimizing the performance of the queries is one of the important
functions of DBMS.
1. The END User who uses the application. Ultimately he is the one who actually
puts the data into the system into use in business. This user need not know
anything about the organization of data in the physical level.
2. The Application Programmer who develops the application programs. He/She has
more knowledge about the data and its structure. He/she can manipulate the data
using his/her programs. He/she also need not have access and knowledge of the
complete data in the system.
3. The Data base Administrator (DBA) who is like the super-user of the system.
The role of DBA is very important and is defined by the following functions.
• Defining the schema: The DBA defines the schema which contains the
structure of the data in the application. The DBA determines what data
needs to be present in the system and how this data has to be presented
and organized.
• Liaising with users: The DBA needs to interact continuously with the
users to understand the data in the system and its use.
• Defining Security & Integrity checks: The DBA finds about the access
restrictions to be defined and defines security checks accordingly. Data
Integrity checks are defined by the DBA.
• Defining Backup/Recovery Procedures: The DBA also defines procedures
for backup and recovery. Defining backup procedure includes specifying
what data is to be backed up, the periodicity of taking backups and also
the medium and storage place to backup data.
• Monitoring performance: The DBA has to continuously monitor the
performance of the queries and take the measures to optimize all the
queries in the application.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
Aim
To execute a set of SQL statements using arithmetic, logical and comparison operators.
TABLE EMPLOYEES:
create table employees(eid varchar(4),ename varchar(5),edept varchar(15),esalary
number(6));
select * from employees;
1. ARITHMETIC OPERATORS ( +, -, *, / )
Query: 1. select eid, ename, esalary*2 from employees;
2. update employees set esalary=esalary*2;
select * from employees;
2. LOGICAL OPERATORS
1.AND:
Write a query to display the details of employee whose salary is between 20000 and 50000.
Syntax: select (column_name 1,...,column_name n) from <table_name> where
search_condition1 and
search_condition2;
Query: select * from employees where esalary>20000 and esalary<50000;
2. OR:
Write a query to display the details of employee whose id is A101 or B102.
Syntax:
select (column_name 1,...,column_name n)
from <table_name> where search_condition1 or
search_condition2;
Query: select * from employees where eid=‘A101’ or eid=‘B102’;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
3. NOT
Write a query to display the details of employee whose id is not A102.
Syntax: select (column_name 1,...,column_name n) from <table_name> where not
search_condition;
Query: select * from employees where not eid=‘A102’;
3. COMPARISON OPERATORS:
1.NOT BETWEEN:
Write a query to display the names of employee whose salary is not between 20000 and
60000.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
not
between value1 and value2;
Query: select ename from employees where esalary not between 20000 and 60000;
2.BETWEEN:
Write a query to display the names of employee whose salary is between 30000 and 60000.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
between value1 and value2;
Query: select ename from employees where esalary between 30000 and 60000;
a.Write queries to display the details of employee whose name begins with character ‘a’, ends
with character ‘z’.
Queries: select * from employees where ename like ‘a%’;
select * from employees where ename like ‘%z’;
b. Write a query to display the names of employee whose name has a length of four
characters
Query: select ename from employees where ename like ‘____’;
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
4. NOT LIKE:
Write a query to display the details of employee whose name does not begin with character
‘a’
Query: select * from employees where ename not like ‘a%’;
5. IN:
Write a query to display the details of employee whose name is abc , xyz or def.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
in
(<value1>,<value 2>,...,<value n>);
Query: select * from employees where ename in ( ‘abc’,’xyz’,’def’);
6. NOT IN:
Write a query to display the details of employee whose name is not abc , xyz or def.
Syntax: select (column_name 1,...,column_name n) from <table_name> where column_name
not in(<value1>,<value 2>,...,<value n>);
Query: select * from employees where ename not in ( ‘abc’,’xyz’,’def’);
4. MISCELLANEOUS FUNCTIONS
2. GROUP BY : Groups the rows based on distinct values that exist for specified column.
Write a query to display the individual total salary amount of all the departments.
Syntax: select column_name,aggregate_function(column_name) from <table_name> group
by column_name;
Query: select edept,sum(esalary) from employees group by edept;
3. HAVING : Output the rows of the query having the specified values.
Write a query to display the individual total salary amount of departments having the total
amount greater than100000.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
TABLE DUAL:
•The dual table is a temporary table which contains exactly one column whose name is
dummy and a single row. The value of that record is X. The owner of dual is SYS (SYS is the
owner of the database and the owner of the data dictionary) but dual can be accessed by every
user.
I AGGREGATE FUNCTIONS
1. COUNT:
It is used to count the no of rows or records in a table.
a.
Syntax: select count(*) from <table_name> ;
Ex: select count(*) from employees;
b. Syntax: select count(<column_name>) from <table_name> ;
Ex: select count(emp_name) from employees;
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
2. SUM
It is used to compute the sum of a particular field in a table.
Syntax: select sum(<column_name>) from <table_name> ;
Ex: select sum(salary) from employees;
3. AVERAGE
It is used to compute the average of a particular field in a table.
Syntax: select avg(<column_name>) from <table_name> ;
Ex: select avg(salary) from employees;
4. MINIMUM
It is used to minimum value of a particular field in a table.
Syntax: select min(<column_name>) from <table_name> ;
Ex: select min(salary) from employees;
5. MAXIMUM
It is used to maximun value of a particular field in a table.
Syntax: select max(<column_name>) from <table_name> ;
Ex: select max(salary) from employees;
II CHARACTER FUNCTIONS
1. UPPER
Returns the given string with all the characters in uppercase.
Syntax: select upper(<string>) from <table_name> ;
Ex: select upper(emp_name) from employees;
select upper(‘hello’) from dual;
2. LOWER
Returns the given string with all the characters in lowercase.
Syntax: select lower(<string>) from <table_name> ;
Ex: select lower(emp_name) from employees;
select lower(‘hello’) from dual;
3. SUBSTR
Returns the substring selected from the given string. The substring starts from nth
character of the given string and m is the length of the substring.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
4. CONCAT
Concatenates the two given strings.
Syntax: select concat(<string1>,<string2>) from <table_name> ;
Ex: select concat(emp_id,emp_dept) from employees;
select concat(‘hai’,’hello’) from dual;
5. TRANSLATE
Returns the given string after replacing all the occurrence of each character in str1 with the
characters from str2.
Syntax: select translate(<string>,<str1>,<str2>) from <table_name> ;
Ex: select translate(‘rack’,’r’,’j’) from dual;
6. ASCII
Returns the decimal equivalent of a single ASCII character.
Syntax: select ascii(<character>) from <table_name> ;
Ex: select ascii(‘b’) from dual;
7. CHR
Returns the ASCII character represented by the given decimal number.
Syntax: select chr(<n>) from <table_name> ;
Ex: select chr(‘97’) from dual;
8. LENGTH
Returns the length of the given string.
Syntax: select length(<string>) from <table_name> ;
Ex: select length(‘database’) from dual;
9. LPAD
Performs left padding of the given string up to length specified the user with the specified
character.
Syntax: select lpad(<string>,<n-length of the resultant string>,<character>) from
<table_name> ;
Ex: select lpad(‘database’,10,’*’) from dual;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
10. RPAD
Performs right padding of the given string up to length specified the user with the specified
character.
Syntax: select rpad(<string>,<n-length of the resultant string>,<character>) from
<table_name> ;
Ex: select rpad(‘database’,10,’*’) from dual;
11. INITCAP
Capitalizes starting letter of each word in a given string.
Syntax: select initcap(<string>) from <table_name> ;
Ex: select initcap(emp_name) from employees;
select initcap(‘database’) from dual;
2. CEIL
Returns the greatest integer value equal to or greater than the given number.
Syntax: select ceil(<n>) from <table_name> ;
Ex: select ceil(23.4) from dual;
3. FLOOR
Returns the smallest integer value equal to or smaller than the given number.
Syntax: select floor(<n>) from <table_name> ;
Ex: select floor(23.4), floor(34.7), floor(39.2) from dual ;
4. MOD
Returns the value of ( n modulus r).
Syntax: select mod(<remainder>,<n>) from <table_name> ;
Ex: select mod(20,3) from dual;
5. POWER
Returns the value of (mn ).
Syntax: select power(<m>,<n>) from <table_name> ;
Ex: select power(20,3) from dual;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
6. ROUND
Returns the value of n rounded to m places right of decimal point. If value of m is not
mentioned, n is rounded to 0 places.
Syntax: select round(<n>,<m>) from <table_name> ;
Ex: select round(35.3333,1) from dual;
7. SIGN
Used to check the sign of a number. [ -1 if n<0, 0 if n=0, 1 if n>0 ]
Syntax: select sign(<n>) from <table_name> ;
Ex: select sign(3) from dual;
8. SQRT
Returns the squareroot of the given number.
Syntax: select sqrt(<n>) from <table_name> ;
Ex: select sqrt(9) from dual;
9. TRUNC
Value of n will be truncated to m decimal places. If value of m is not mentioned, n will be
truncated to 0 places.
Syntax: select trunc(<n>,<m>) from <table_name> ;
Ex: select trunc(333.123,1) from dual;
10. COS
Returns the cosine value of any number passed as an angle in radians to the function.
Syntax: select cos(<n>) from <table_name> ;
Ex: select cos(0) from dual;
11. SIN
Returns the sine value of any number passed as an angle in radians to the function.
Syntax: select sin(<n>) from <table_name> ;
Ex: select sin(0) from dual;
12. TAN
Returns the tangent value of any number passed as an angle in radians to the function.
Syntax: select tan(<n>) from <table_name> ;
Ex: select tan(0) from dual;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
13. EXP
Returns the value of (e ) , where e=2.71828183...
Syntax: select exp(<n>) from <table_name> ;
Ex: select exp(0) from dual;
14. LOG
nReturns logarithm value of n with base b.
Syntax: select log(<b>,<n>) from <table_name> ;
Ex: select log(2,2) from dual;
15. LN
Returns natural logarithm value of n, where n>0.
Syntax: select ln(<n>) from <table_name> ;
Ex: select ln(1) from dual;
16. LEAST
Returns the least value among the given numbers.
Syntax: select least(<n1>,<n2>,<n3>,...) from <table_name> ;
Ex: select least(56,99,34,1,23) from dual;
17. GREATEST
Returns the greatest value among the given numbers.
Syntax: select greatest(<n1>,<n2>,<n3>,...) from <table_name> ;
Ex: select greatest(56,99,34,1,23) from dual;
1. SYSDATE
Returns the current date and time in the Oracle server.
Syntax: select sysdate from <table_name> ;
Ex: select sysdate from dual;
2. ADD_MONTHS
Adds or subtracts months from a date.
Syntax: select add_months(<date>,<no of months>) from <table_name> ;
Ex: select add_months(’20-jan-13’,2) from dual;
3. LAST_DAY
Provides the last day of the month specified in the given date.
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
4. TO_CHAR
Converts date into charater format.
Syntax:
select to_char(<date>,’mm’) from <table_name> ;
select to_char(<date>,’dd’) from <table_name> ;
select to_char(<date>,’yyyy’) from <table_name> ;
Ex: select to_char(sysdate,’mm’) from dual ;
select to_char(sysdate,’dd’) from dual ;
select to_char(sysdate,’yyyy’) from dual ;
5. TO_DATE
Converts string into date format
Syntax: select to_date('<string>’, 'Month dd, YYYY’) from <table_name> ;
Ex: select to_date('January 26 2013', 'Month dd, YYYY’) from dual;
Result
Thus a set of SQL statements using aggregate, character, numerical and date functions were
implemented successfully.
To execute a set of SQL statements implementing DML, DQL and TCL commands
DML – Data Manipulation Language:
The Data Manipulation Language (DML) is used to access or manipulate the data organized
by an appropriate data model.
DQL – Data Query Language:
The Data Query Language (DQL) enables the users to retrieve information from the
database.
It is used to display the data stored in a table.
TCL – Transaction Control Language:
The Transaction Control language (TCL) enables the users to control the changes made
by atransaction in a database.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
Syntax: SQL> insert into <table name> values(val1, val2,…….. val n.);
Query: SQL> insert into employee values(1001,’abcd’,’programmer’,25000);
b) Write a query to insert the record (1002,’def’,’analyst’,30000) into employee table using
subtitution method
Syntax: SQL > insert into <table name> values(& column name1, &column name2,…..);
Query: SQL> insert into employee values(&empno,&ename,&desgn,&salary);
4. TRUNCATE : It is used to delete all the rows from the table and free the space containing
the table.
a) Write queries to insert and delete the contents of table emp1.
Syntax: SQL>truncate table <table name>;
Query: SQL>insert into emp1 values( 1, ‘abc’);
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
Syntax: SQL > select <column name1,column name2...column name n> from <table name>
;
Query: SQL> select empno,ename from employee;
d) Write a query to display the names of the employees who are programmers.
Syntax: SQL > select <column name1,column name2...column name n> from <table name>
where <condition>;
Query: SQL> select empno,ename from employee where desgn =’programmer’;
e) Write a query to display the different designations available.
Syntax: SQL > select distinct column name from <table name> ;
Query: SQL> select distinct desgn from employee;
1. SAVEPOINT: It is used to save the changes made until the particular timepoint.
Syntax: SQL > savepoint <savepoint name> ;
Query: SQL>savepoint svp;
2. ROLLBACK: It is used to undo the changes done by the current transaction until a
particular
timepoint.
Syntax: SQL > rollback <savepoint name>;
Query: SQL>rollback svp;
3. COMMIT: It is used to end the current transaction and to save the changes made by the
transaction.
Syntax: SQL > commit ;
Query: SQL>commit;
RESULT
Thus a set of SQL statements implementing DML, DQL and TCL commands were
executed
successfully.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
IMPLEMENTATION OF DDL :
AIM
To execute a set of SQL statements implementing DDL commands and Constraints
Syntax : SQL> create table <target table name> as select (column name.1, column name.2
,…………column name n ) from <soursce table name>;
Query: SQL> create table emp1 as select ( empno, ename) from employee;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
5.TRUNCATE : It is used to delete all the rows from the table and free the space containing
the table.
a) Write queries to insert and delete the contents of table emp1.
Syntax: SQL>truncate table <table name>;
Query: SQL>insert into emp1 values( 1, ‘abc’);
SQL> select * from emp1;
SQL>truncate table emp1;
SQL> select * from emp1;
1.2 CONSTRAINTS
Constraints are part of the table definition that limits and restricts the value entered into
its column.
Constraints can be created in three ways: Operations on Constraints:
Column level constraints Enable
Table level constraints Disable
Using DDL command - alter Drop
www.ethnus.com Schedule a Mock Interview at www.eguru.ooo facebook.com/ethnus
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
TYPES OF CONSTRAINTS:
1. NOT NULL : This constraint enforces a column to not accept NULL values.The NOT
NULL constraint enforces a field to always contain a value.
Column level constraint: Write a query to create not null constraint at column level.
Syntax: SQL> create table <table.name> (column name1 <datatype> (size) <constraint
type>, columnname.2<datatype> (size));
Query: SQL> create table student (rollno number(5) NOT NULL , dept char(5), name
char(15));
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
SQL>alter table <table name> add constraint <constraint name> UNIQUE (column name)
Query: SQL> create table student (rollno number(5), dept char(5), name char(15));
SQL> alter table student add constraint consrt UNIQUE(rollno);
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
Parent table: SQL> create table <table.name1> (column name11 <datatype> (size),
columnname.12<datatype> (size),…. ,PRIMARY KEY (columnname11));
Child table: SQL> create table <table.name2> (column name.1<datatype> (size) , column
name.2 <datatype> (size), constraint <constraint name> FOREIGN KEY(column name2)
references <table name1> ( column name11));
Query:
Parent table: SQL> create table student (rollno number(5) , dept char(5), name
char(15), PRIMARY KEY(rollno));
Child table: SQL> create table project (pno char(20) , studno number(5), constraint consrt
FOREIGN KEY (studno)references student (rollno));
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
OPERATIONS ON CONSTRAINT:
1.Write a query to disable a constraint.
Syntax: SQL>alter table <Table name> DISABLE CONSTRAINT <constraint name>;
Query: SQL>alter table student DISABLE CONSTRAINT consrt;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
1. Find all customers who have both an account and a loan at the bank.
select distinct customer-name from borrower
where customer-name in (select customer-name from depositor)
2. Find all customers who have a loan at the bank but do not have
an account at the bank
select distinct customer-name from borrower
where customer-name not in (select customer-name from depositor)
3. Find the names of all branches that have greater assets than all branches located
in Brooklyn.
select branch-name from branch where assets > all
(select assets from branch where branch-city = ‘Brooklyn’)
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
Result:
Thus a set of SQL nested subqueries for manipulating banking database and employee
database were executed successfully.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
JOIN QUERIES
Aim
To execute a set of SQL queries implementing joins.
SQL Joins:
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables.
1. INNER JOIN: Return rows when there is at least one match in both tables
Write a query to display the books details with their purchase details with no null values.
Syntax: SQL> SELECT column_name(s) FROM table_name1 INNER JOIN table_name2
ON table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books INNER JOIN purchase on
books.book_id=purchase.book_id order by books.title;
2. LEFT JOIN: Return all rows from the left table, even if there are no matches in the
right table.
Write a query to display all the available book details with or without their purchase details.
Syntax: SQL> SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON
table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books LEFT JOIN purchase on
books.book_id=purchase.book_id order by books.title;
3. RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
Write a query to display all the available purchase details with or without their book details.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
4. FULL JOIN: Return rows when there is a match in one of the tables
Write a query to display all the available books details with all the available purchase details.
Syntax: SQL> SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON
table_name1.column_name=table_name2.column_name;
Query: SQL> select * from books RIGHT JOIN purchase on
books.book_id=purchase.book_id order by books.title;
Result
Thus a set of join queries were executed successfully.
SET OPERATIONS:
Aim
To execute a set of SQL statements implementing set operations.
TABLE PROJECTA :
select * from projecta;
TABLE PROJECTB:
select * from projectb;
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com
DBMS - LEARNER'S NOTES
SET OPERATIONS:
1.UNION ALL: Merges the output of two or more queries into a single set of rows and
columns with repetition.
Write a query to display all the employees who work for either projecta or projectb.
Syntax: select (column_name 1,...,column_name n) from <table_name>
union all
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta union all select ename from projectb;
2.UNION : Merges the output of two or more queries into a single set of rows and columns.
Write a query to display the employees who work for either projecta or project.
Syntax: select (column_name 1,...,column_name n) from <table_name>
union
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta union select ename from projectb;
3.MINUS : Outputs the rows produced by first query, after filtering the rows retrieved by the
second query.
Write a query to display the employees who work only for projecta.
Syntax: select (column_name 1,...,column_name n) from <table_name>
Minus
select (column_name 1,...,column_name n) from <table_name> ;
Query: select ename from projecta minus select ename from projectb;
Result:
Thus a set of SQL statements implementing set operations were executed successfully.
© 2018 Ethnus Consultancy Services Pvt. Ltd. All rights reserved. This material or any portion thereof may not be reproduced or used in any manner. To report violation, ask query, or
provide feedback e-mail us at reachus@ethnus.com