Aim DBMS2023 V
Aim DBMS2023 V
Aim DBMS2023 V
AIM:
To write SQL commands to implement Data Definition Language(DDL),Data Manipulation
Language(DML),Transaction Control Language(TCL),Data control languages(DCL) in Oracle.
COMMANDS DESCRIPTION
1)DATADEFINITIONLANGUAGE (DDL)DESCRIPTION:
- It is used to communicate with database.DDL defines the table structure in database.
- DDL is used to: Create an object, Alter the structure of an object , To drop the object created.
OVERVIEW:
1.CREATE - to create objects in the database
2.ALTER - alters the structure of the objects in the database DROP - delete objects from the
database
3.TRUNCATE - remove all records from a table, including all spaces allocated for the records
are removed
4.RENAME- change the table name and column name
5.DROP- Drop the database objects.
COMMENT - add comments to the data dictionary
//CREATE TABLE
It is used to create a table in SQL.
Syntax: Create table <tablename> (column_name1 data_ type constraints,column_name2
data_ type constraints …)
Example :
create table customer(cust_name varchar2(15),social_security_no number(11),cust_street
varchar2(7),cust_city varchar2(10));
//ALTER TABLE
Alter command is used to:
1. Add a new column.
3. Modify the existing column definition.
3. To include or drop integrity constraint.
Syntax: alter table <tablename> add/modify (attribute datatype(size));
Example:
1. Alter table emp add (phone_no char (20));
2. Alter table emp modify(phone_no number (10));
3. ALTER TABLE EMP ADD CONSTRAINT Pkey1 PRIMARY KEY (EmpNo);
//DROP TABLE
It will delete the table in database.
Syntax : drop table <tableame>
Example: drop table prog20; Here prog20 is table name
//TRUNCATE TABLE
If there is no further use of records stored in a table and the structure has to be retained then
the records alone can be deleted.
Syntax: TRUNCATE TABLE <TABLE NAME>;
Example: Truncate table customer;
//DESCRIPTION
This is used to view the structure of the table.
Syntax : desc <tablename>
Example: desc emp;
//DML Commands
1.INSERT – This is used to add one or more rows to a table.
The values are separated by commas and the data types char and date are enclosed in
apostrophes.
The values must be entered in the same order as they are defined.
Example :
a)Find the names of all branches in the loan table
SQL> select branch_name from loan;
b)List all account numbers made by brighton branch
SQL> select acc_no from account where branch_name = 'brighton';
c)List the customers who are living in the city harrison
SQL> select cust_name from customer where cust_city = 'harrison';
4.DELETE COMMAND
After inserting row in a table we can also delete them if required.
The delete command consists of a from clause followed by an optional where clause.
COMMANDS OVERVIEW
COMMIT - save work done & it is visible to other users.
SAVEPOINT - identify a point in a transaction to which you can later roll back.
ROLLBACK - restore database to original since the last COMMIT
SET TRANSACTION - Change transaction options like what rollback segment to use
SAVEPOINT
Syntax: savepoint username;
SQL> savepoint emp; savepoint created;
COMMIT
Syntax: commit;
SQL> commit; Output:
Commit complete.
ROLLBACK
Syntax:rollback to savepoint_text_identifier;
SQL> rollback to emp;
Output:Rollback complete.
By the above command user ram has granted permissions on accounts database object like he
can query or insert into accounts.
2.REVOKE :
Revoke command withdraw user privileges on database objects if any granted.
It does operations opposite to the Grant command.
When a privilege is revoked from a particular user U, then the privileges granted to all other
users by user U will be revoked.
Example: revoke insert,select on accounts from Ram
By the above command user ram’s permissions like query or insert on accounts database object
has been removed.
Result.
Thus the DDL.DML,TCL,DCL commands are executed and verified successfully.
EX.NO2 INTEGRITY CONSTRAINTS
Aim:
To write SQL query to implement the different types of Integrity Constraints.
COMMANDS DESCRIPTION
INTEGRITY CONSTRAINTS
- SQL Constraints are rules used to limit the type of data that can go into a table, to maintain the
accuracy and integrity of the data inside table.
- Integrity constraints are pre-defined set of rules that are applied on the table fields(columns) or
relations to ensure that the overall validity, integrity and consistency of the data present in the database
table is maintained.
- Integrity constraints are a set of rules. It is used to maintain the quality of information.
1. DOMAIN INTEGRITY
Domain constraints can be defined as the definition of a valid set of values for an attribute.
a) NOT NULL – It will not allow null values.
NOT NULL constraint restricts a column from having a NULL value.
Once NOT NULL constraint is applied to a column, you cannot pass a null value to that column.
Syntax : create table <table name>(columnname datatype(size)constraint constraint_name not null);
Example : CREATE table Student(s_id number NOT NULL,Name varchar(60),Age number)
b) CHECK - Use the CHECK constraint when you need to enforce integrity rules that can be
evaluated based on a condition (logical expression).
CHECK constraint is used to restrict the value of a column between a range.
It performs check on the values,before storing them into the database.
Its like condition checking before saving data into a column
Syntax : create table <table name>(columnname data type(size) constraint constraint_name
check(check_condition));
Example : create table Student(s_id number NOT NULL
CHECK(s_id>0),Name varchar(60)NOT NULL,Age number);
2. ENTITY INTEGRITY :
The entity integrity constraint states that primary key value can't be null.
a) UNIQUE
– Avoid duplicate values. A UNIQUE constraint field will not have duplicate data.
Syntax : create table <table name>(columnname data type (size) constraint constraint_name unique);
Example : CREATE table Student(s_id number NOT NULL UNIQUE, Name varchar(60), Age
number);
d) Composite PRIMARY KEY – Multicolumn primary key is called composite primary key
Syntax : create table <table name>(columnname1 datatype(size),
columnname2 datatype(size),constraintconstraint_name primary
key(columnname1,columnname2));
3. REFERENTIAL INTEGRITY
Reference key (foreign key):
- Its represent relationships between tables.Foreign key is a column whose values are derived from the
primary key of the same or some other table.
- FOREIGN KEY is used to relate two tables.
- FOREIGN KEY constraint is also used to restrict actions that would destroy links between tables.
- A referential integrity constraint is specified between two tables.
- In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary Key of
Table 2, then every value of the Foreign Key in Table 1 must be null or be available in Table 2.
Syntax : create table <table name>(columnname data type (size) constraint constraint_name
references parent_table_name);
Example : CREATE table Order_Detail(order_id number PRIMARY KEY,order_name varchar(60)
NOT NULL, c_id number FOREIGN KEY REFERENCES Customer_Detail(c_id));
Result.
Thus the Different types Integrity Constraints are executed and verified successfully.
EX.NO3 WHERE CLAUSE CONDITION AND AGGREGATE
FUNCTIONS
Aim:
To Write different SQL queries using Where conditions and Aggregate Functions.
While the SELECT clause specifies the columns to be returned from the table(s),
the WHERE clause contains the conditions that must evaluate to true for a row to be returned
as a result.
The WHERE clause is not only used in SELECT statements, it is also used
in UPDATE, DELETE, etc.!
SPECIAL OPERATORS :
In / not in – used to select a equi from a specific set of values.
Any - used to compare with a specific set of values.
Between /not between – used to find between the ranges.
%Like / not like – used to do the pattern matching.
ARITHMETIC OPERATIONS
- Various operations such as addition, multiplication, subtraction and division can be
performed using the numbers available in the table.
DISTINCT
- This keyword is used along with select keyword to display unique values from the specified
column.
- It avoids duplicates during display.
Example: SQL> select DISTINCT lastname from person
ORDER BY CLAUSE
The order by clause arranges the contents of the table in ascending order (by default)
or in descending order (if specified explicitly) according to the specified column.
Example :
SQL> select pid, firstname,age from person order by age;
CONCATENATION OPERATOR(||)
- This combines information from two or more columns in a sentence according to the
format specified.
LOGICAL OPERATORS
AND : The oracle engine will process all rows in a table and displays the result only
when all of the conditions specified using the AND operator are specified.
OR : The oracle engine will process all rows in a table and displays the result only when
any of the conditions specified using the OR operators are satisfied.
NOT : The oracle engine will process all rows in a table and displays the result only
when none of the conditions specified using the NOT operator are specified.
BETWEEN : In order to select data that is within a range of values, the between
operator is used. (AND should be included)
GROUP BY CLAUSE
The GROUP BY statement groups rows that have the same values into summary
rows,like "find the number of customers in each country".
Example: Select max(percentage), deptname from student group by deptname;
HAVING CLAUSE
This is used to specify conditions on rows retrieved by using group by clause.
Example: Select max(percentage), deptname from student group by deptname having
count(*)>=50;
Result.
Thus the Where clause usage and Aggregate functions are executed and verified
successfully.
EX.NO4 : SUB QUERY AND SIMPLE JOIN OPERATIONS
Aim.
To execute and verify the r nested queries ,Sub queries ,different types of Joins.
NESTED QUERIES :
Nesting of queries one within another is known as a nested queries.
2)JOINS:
- Join is a query in which data is returned from two or more tables.
- A JOIN clause is used to combine rows from two or more tables, based on a related
column between them.
- Types of Joins
1.Equi Join 2. Inner Join 3.Left Outer Join 4.Right Outer Join 5.Full join
Result:
Thus the SQL commands for Sub queries ,Joins are executed and verified
successfully.
EX.NO 5 TRIGGERS
Aim:
To write PL/SQL programs to implement the concept of Triggers in Oracle.
Commands Description
Triggers:
- Triggers are defined as stored programs which are automatically executed
whenever some events such as CREATE, ALTER, UPDATE, INSERT, DELETE
takes place.
- A trigger is a statement that is executed automatically by the system as a side
effect of a modification to the database.
- They can be defined on a database, table, view with which event is associated.
Types Of Triggers
1.Row Level Trigger –
- Row level triggers execute once for each and every row in the transaction.
Example: If 1500 rows are to be inserted into a table, the row level trigger would execute
1500 times.
Trigger syntax:
CREATE [OR REPLACE] TRIGGER <trigger_name>
{ BEFORE|AFTER } { INSERT|DELETE|UPDATE } ON <table_name>
[ REFERENCING [ NEW AS <new_row_name> ] [OLD AS <old_row_name> ] ]
[ FOR EACH ROW [ WHEN ( <trigger_condition> ) ] ]
<trigger_body>
Result:
Thus the PL/SQL program for triggers are created, executed successfully.
EX.NO 6 VIEWS AND INDEX
Aim:
To write PL/SQL program to implement views and index.
VIEW
- A View in SQL is simply a virtual table created based on a result set of another SQL
statement.
- Views were introduced to reduce the complexity of multiple tables and deliver data in a simple
manner.
- Views help us maintain data integrity and provide security to the data, thus acting as a
security mechanism.
- Views hide the complexity of the data in the database.
- Views provide security to the data, acting as a security mechanism.
- Being a virtual table, views take very little storage since the database contains only a view's
statements (definition) and not a copy of all the tables the view is creating.
UPDATING A VIEW
You can update a view by using the following syntax:
Syntax : update viewname set columnname =< value> where columnname=<value>
DROPPING A VIEW:
You can delete a view with the DROP VIEW command.
Syntax : DROP VIEW view_name
INDEX:
- Indexes are special lookup tables, that the database search engine can use to speed up
data retrieval.
- A SQL index is used to retrieve data from a database very fast.
- Simply put, an index is a pointer to data in a table.
- An index helps to speed up SELECT queries and WHERE clauses.
- indexes are intended to enhance a database's performance.
- Indexes should not be used on small tables.
COMPOSITE INDEXES :
- A composite index is an index on two or more columns of a table.
SYNTAX: CREATE INDEX index_name on table_name (column1, column2);
DROP INDEX:
SYNTAX: DROP INDEX index_name;
Result:
Thus the concept of index and views are created and successfully executed in oracle.
EX.NO7 FUNCTIONS AND STORED PROCEDURES
Aim:
To write PL/SQL programs that execute the concept of procedures and functions in Oracle.
Procedure Description
➢ A procedure is a set of instructions which takes input and performs a certain task.
➢ In SQL, procedures do not return a value.
➢ In SQL, a procedure is basically a precompiled statement which is stored inside the
database. Therefore, a procedure is sometimes also called a stored procedure.
➢ A procedure always has a name, list of parameters, and compiled SQL statements.
➢ In SQL, a procedure does not return any value.
➢ The procedures cannot be called from function.
➢ A procedure can be used to read and modify data.
➢ It cannot be called in a query.
➢ In a procedure, DML statements can be used.
SYNTAX:
Function Description
➢ In SQL, a function returns a value.
➢ In other words, a function is a tool in SQL that is used to calculate anything to produce
an output for the provided inputs.
➢ Each and every time functions are compiled(Not precompiled) they provide output
according to the given input.
➢ In SQL queries, when a function is called, it returns the resulting value.
➢ It also controls to the calling function.
➢ However, in a function, we cannot use some DML statements like Insert, Delete,
Update, etc.
➢ Also, a function can be called through a procedure.
➢ Based on definition, there are two types of functions namely, predefined function and
user defined function.
➢ Another important point about functions is that they may or may not return a value,
➢ i.e. a function can return a null valued as well.
➢ The function can be called using Stored Procedure.
➢ A function used only to read data.
➢ It can be called in a query.
➢ DML statements such as (Insert, Delete, and Update) cannot be used in a function.
SYNTAX:
CREATE [OR REPLACE ] FUNCTION function_name
[ (parameter_name type [, …] ) ]
BEGIN
// program code
[EXCEPTION
exception_section;
END [function_name];
Parameter:
The parameter is variable or placeholder of any valid PL/SQL datatype through which
the PL/SQL subprogram exchange the values with the main code.
This parameter allows to give input to the subprograms and to extract from these subprograms.
Types:
➢ IN Parameter
➢ OUT Parameter
➢ IN OUT Parameter
IN Parameter:
➢ This parameter is used for giving input to the subprograms.
➢ It is a read-only variable inside the subprograms.
OUT Parameter:
➢ This parameter is used for getting output from the subprograms.
➢ It is a read-write variable inside the subprograms. Their values can be changed inside
the subprograms.
IN OUT Parameter:
➢ This parameter is used for both giving input and for getting output from the
subprograms.
➢ It is a read-write variable inside the subprograms. Their values can be changed inside
the subprograms.
➢ In the calling statement, these parameters should always be a variable to hold the value
from the subprograms.
RETURN
➢ RETURN is the keyword that instructs the compiler to switch the control from the
subprogram to the calling statement.
➢ In subprogram RETURN simply means that the control needs to exit from the
subprogram.
➢ Once the controller finds RETURN keyword in the subprogram, the code after this will
be skipped.
Result:
Thus the PL/SQL Procedure and Functions were created and executed verified
successfully.
EX.NO 8 DOCUMENT BASED DB CREATION USING MONGODB
Aim:
To create document based database using MONGODB.
The below two methods are used for MongoDB Create Operations.
• db.collection_name.insertOne()
• db.collection_name.insertMany()
In the following example, we can see the Collection "users_detail" is created with the below
data.
• db.collection_name.find()
The following methods are used to perform the Update Operations in MongoDB.
• db.collection_name.updateOne()
• db.collection_name.updateMany()
• db.collection_name.replaceOne()
The following methods are used to perform the Delete Operations in MongoDB.
• db.collection.deleteOne()
• db.collection.deleteMany()
RESULT:
Thus the Document based Database created and executed CRUD operations using
MONGODB