Notes Dbms
Notes Dbms
“Develop and create women technocrats who can meet the challenges of the corporate
world and emerge as leaders contributing ton industry and society.”
To empower women technocrats in the field of computer engineering and prepare the
students for higher studies and professional career through technology, innovations
and human values to serve the changing needs of our nation.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources,
and modern engineering and IT tools including prediction and modeling to
complex engineering activities with an understanding of the limitations.
PEO1 Demonstrate the technical skills to analyse and design suitable solutions for
problems using standard practices, tools and techniques.
PEO2 Carry out higher education and research in Artificial Intelligence and Data
Science areas to address the basic needs of the society.
PSO1 Apply the concepts learnt through courses like Data Structures, Data Mining,
Cloud Computing, Machine Learning, Data Science, Computer Vision, Data
Visualization and programming languages to solve real life problems.
PSO2 Acquaint with the contemporary trends in industries and thereby innovate novel
solutions to existing problems.
INDEX
Sl.No. DATE NAME OF THE EXPERIMENT PAGE MARKS SIGNATURE
No.
1. Data Definition Language (DDL)
commands -Data Manipulation
Language (DML) and Data Control
Language (DCL).
2. Programs using Data definition –Table
creation and Constraints.
9. Creation of Views-Synonyms-Sequence-
Indexes-save point.
AIM:
To study and implement the Data Definition Language, Data
Manipulation Language and Data Control Language commands using MySQL
Workbench.
DESCRIPTION:
DATABASE:
A database is a collection of information that is organized so that it can be
easily accessed, managed and updated.
DATABASE MANAGEMENT SYSTEM:
A Database management system (DBMS) is a set of programs that
manages any number of databases.
DDL (DATA DEFINITION LANGUAGE):
DDL or Data Definition Language actually consists of the SQL commands that
can be used to define the database schema. It simply deals with the descriptions
of the database schema and is used to create and modify the structure of data
base objects in the database.
DDL commands:
CREATE
DROP
ALTER
TRUNCATE
RENAME
CREATE Command:
CREATE is used to create the database or its objects (like table,
index, functions, views, store procedure and triggers).
SYNTAX:
CREATE TABLE table_name
(
Column_1 datatype,
Column_2 datatype,
Column_3 datatype,
……………………
);
CREATE COMMAND:
Output:
ALTER Command:
This command is used to alter the structure of the database.
SYNTAX:
ALTER TABLE table_name ADD Column_name datatype;
ALTER COMMAND:
Output:
RENAME Command:
This command is used to rename an object existing in the database.
SYNTAX:
ALTER TABLE old_table_name RENAME TO new_table_name;
RENAME COMMAD:
Output:
DML (DATA MANIPULATION LANGUAGE):
Data Manipulation Language (DML) allows us to modify the
database instance by inserting, modifying, and deleting its data. It is
responsible for performing all types of data modification in a database.
DML Commands are:
INSERT
UPDATE
DELETE
SELECT
INSERT Command:
This is a statement in a SQL query. This command is used to insert
data into the rows of a table.
SYNTAX:
INSERT INTO table_name VALUE (value1, value2, value3, ...…. ,valueN);
INSERT COMMAND:
UPDATE Command:
This command is used to update or modify the value of a column in the table.
SYNTAX:
UPDATE table_name SET [Column_name1=Value1, ……… ,
Column_nameN = ValueN WHERE Condition];
UPDATE COMMAND:
output
DELETE Command:
This command is used to remove one or more rows from a table.
SYNTAX:
DELETE FROM table_name WHERE Condition;
DELETE COMMAND:
Output:
TRUNCATE Command:
This command is used to remove all rows from the table, but the structure of
the table still exits.
SYNTAX:
TRUNCATE TABLE table_name;
TRUNCATE COMMAND:
Output:
DROP Command:
This command is used to remove an existing table along with its
structure from the database.
SYNTAX:
DROP TABLE table_name;
DROP COMMAND:
Output:
Preparedness 5
Performance 5
Result 5
Record 5
Inference:
Total 20
Hence,I have learned the data
definition languages , data manipulation languages and data control languages.
AIM:
To implement programs using the data definition language, table
creation and constraints.
DESCRIPTION:
DATABASE:
A database is a collection of information that is organized so that it can be
easily accessed, created and updated.
DATABASE MANAGEMENT SYSTEM:
A database managemet system (DBMS) is a set of programs that manages any
number of databases.
SQL CONSTRAINTS
To maintain accuracy and integrity of the data inside.
NOT NULL.
UNIQUE.
PRIMARY KEY.
FOREIGN KEY.
CHECK.
NOT NULL
The not null constraints enforces a column to not accept the null values.
SYNTAX
CREATE TABLE table_name(column_name1 datatype NOT
NULL,column name2 datatype[size]);
QUERY:
Output:
UNIQUE
Unique constraint uniquely identifies each record in a database table.
SYNTAX
CREATE TABLE table_name(column name datatype UNIQUE);
QUERY:
Create table student1
(
ID int not null unique,
Name varchar(100) not null,
Age int
);
Output:
PRIMARY KEY
The primary key constraint uniquely identifies each record in database
table.Primary key cannot contain null values.
SYNTAX
CREATE TABLE table_name(column_name1 datatype PRIMARY
KEY);
QUERY:
Create table student1
(
ID int not null,
Name varchar(100) not null,
Age int,
Primary key(ID)
);
Output:
FOREIGN KEY
It is used to relate two tables.
SYNTAX
[CONSTRAINT constraint_name] FOREIGN KEY (column_name)
REFERENCES referenced_table_name (column_name);
QUERY:
Create table branch
(
branch_id int primary key,
Branch_name varchar(100) not null,
Location varchar(100)
);
Create table employee
(
emp_id int primary key,
ename varchar(100) not null,
Job varchar(100),
Salary int,
branch_id int,
FOREIGN KEY(branch_id) references branch(branch_id)
);
Output:
CHECK
Check constraint is used to check the value of a column between a
range.
SYNTAX
[CONSTRAINT constraint_name] CHECK (condition);
QUERY:
Create table student3
(
ID int not null,
Name varchar(100) not null,
Age int,
CHECK(Age>=18)
);
Output:
DEFAULT CONSTRAINT:
Used to set default values
SYNTAX
Column_name DEFAULT ‘value’
QUERY:
Create table student4
(
ID int not null,
Name varchar(100) not null,
Age int,
City varchar(100) DEFAULT ‘Chennai’
);
Output:
Preparedness 5
Performance 5
Result 5
Record 5
Total 20
Inference:
Hence I have learned to implement the data definition-table creation and
SQL constraints using SQL
INSERT
It is used to insert data into a table.
SYNTAX
INSERT into table_namevalues(data1,data2,……);
QUERY:
Insert into students
values(“Anupama”,”CSE”,101,18)
UPDATE
It is used to update the existing data data in a table.
SYNTAX
UPDATE table_name set column_name=value WHERE condition;
QUERY:
UPDATE students set age=19 WHERE rollno=101;
Output:
DELETE
It is used to delete all the records from a table.
SYNTAX
DELETE from table_name;
QUERY:
DELETE from students where rollno=109;
SELECT
The select statement is used to select data from a database.
SYNTAX
SELECT * from TABLE table_name;
QUERY:
SELECT * from students;
Preparedness 5
Performance 5
Result 5
Record 5
Total 20
Inference:
Hence I have learned to implement the data manipulation languages insert,
select, update and delete using SQL.
EX. NO:4 PROGRAMS USING NESTED QUERIES AND JOIN
QUERIES
DATE:
_______________________________________________________________
AIM:
To write a program using nested queries and join queries.
DESCRIPTION:
NESTED QUERIES
A Sub query or Inner query or a Nested query is a query within another
SQL query and embedded within the WHERE clause.
A sub query is used to return data that will be used in the main query as
a condition to further restrict the data to be retrieved.
Sub queries can be used with the SELECT, INSERT, UPDATE, and
DELETE statements along with the operators like =, <, >, >=, <=, IN,
BETWEEN, etc.
A few rules that sub queries must follow −
Sub queries must be enclosed within parentheses.
A sub query can have only one column in the SELECT clause, unless
multiple columns are in the main query for the sub query to compare its
selected columns.
An ORDER BY command cannot be used in a sub query, although the
main query can use an ORDER BY. The GROUP BY command can be
used to perform the same function as the ORDER BY in a sub query.
Sub queries that return more than one row can only be used with
multiple value operators such as the IN operator.
The BETWEEN operator cannot be used with a sub query. However, the
BETWEEN operator can be used within the sub query.
SUB QUERIES WITH THE SELECT STATEMENT
Sub queries are most frequently used with the SELECT statement.
syntax :
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
Query:
Select ID,dept_Name,Age from dept
Where ID in(select ID from employees where FirstName=’Banu’);
Output:
SUB QUERIES WITH THE INSERT STATEMENT
Sub queries also can be used with INSERT statements. The INSERT
statement uses the data returned from the sub query to insert into another table.
The selected data in the sub query can be modified with any of the character,
date or number functions.
syntax:
INSERT INTO table_name[ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Query:
insert into Employee values(35,"Rathi","Rani",(select Dept_Code from
Department where Dept_Code = 214),70000);
select * from Employee;Output:
Query:
Output:
Output:
JOIN QUERIES
The SQL Joins clause is used to combine records from two or more
tables in a database. A JOIN is a means for combining fields from two tables
by using values common to each.
There are different types of joins available in SQL
INNER JOIN − returns rows when there is a match in both tables.
LEFT JOIN − returns all rows from the left table, even if there are no
matches in the right table.
RIGHT JOIN − returns all rows from the right table, even if there are no
matches in the left table.
FULL JOIN − returns rows when there is a match in one of the tables.
SELF JOIN − is used to join a table to itself as if the table were two
tables, temporarily renaming at least one table in the SQL statement.
CARTESIAN JOIN − returns the Cartesian product of the sets of
records from the two or more joined tables.
SQL - INNER JOINS
The most important and frequently used of the joins is the INNER JOIN.
They are also referred to as an EQUIJOIN.
The INNER JOIN creates a new INFERENCE table by combining column
values of two tables (table1 and table2) based upon the join-predicate. The
query compares each row of table1 with each row of table2 to find all pairs of
rows which satisfy the join-predicate. When the join-predicate is satisfied,
column values for each matched pair of rows of A and B are combined into
aINFERENCE row.
syntax :
SELECT table1.column1, table2.column2...
FROM table1
INNER JOIN table2
ON table1.common_field = table2.common_field;
Query:
Select e.emp_id,e.ename,e.Job,b.branchName
From employee As e
Inner join branch As b
On e.branch_id=b.branch_id
Order by e.emp_id
Output:
CREATING A TABLE:
INNER JOIN
Output:LEFT JOIN:
OUTPUT:RIGHT JOIN:
SQL - FULL JOINS
The SQL FULL JOIN combines the INFERENCEs of both left and right
outer joins.
The joined table will contain all records from both the tables and fill in NULLs
for missing matches on either side.
syntax :
SELECT table1.column1, table2.column2...
FROM table1
FULL JOIN table2
ON table1.common_field = table2.common_field;
Query:
select
e.Dept_Code,e.Employee_Id,e.Firstname,e.Lastname,e.Salary,d.Dept_Name
From Employee as e
Full outer join Department as d
on e.Dept_Code=d.Dept_Code
order by e.Employee_Id
OUTPUT:
SQL - SELF JOINS
The SQL SELF JOIN is used to join a table to itself as if the table were
two tables; temporarily renaming at least one table in the SQL statement.
syntax :
Output:
syntax:
OUTPUT:
Preparedness 5
Performance 5
Result 5
Record 5
Total 20
INFERENCE:
Hence,I have learned the program using nested queries and join
queries
AIM:
DESCRIPTION:
Sub queries are most frequently used with the SELECT statement.
syntax :
SELECT column_name [, column_name ]
FROM table1 [, table2 ]
WHERE column_name OPERATOR
(SELECT column_name [, column_name ]
FROM table1 [, table2 ]
[WHERE])
Query:
Output:
INSERT STATEMENT
Sub queries also can be used with INSERT statements. The INSERT
statement uses the data returned from the sub query to insert into another table.
The selected data in the sub query can be modified with any of the character,
date or number functions.
syntax :
INSERT INTO table_name[ (column1 [, column2 ]) ]
SELECT [ *|column1 [, column2 ]
FROM table1 [, table2 ]
[ WHERE VALUE OPERATOR ]
Query:
insert into grade(Id,Course, grades, marks) select Id, 'new subject', 'A', 90 from
grade
where marks >(select avg(marks)from grade);
select * from grade
Output:
UPDATE STATEMENT
The sub query can be used in conjunction with the UPDATE statement.
Either single or multiple columns in a table can be updated when using a sub
query with the UPDATE statement.
syntax :
UPDATE table
SET column_name = new_value
[ WHERE OPERATOR [ VALUE ]
(SELECT COLUMN_NAME
FROM TABLE_NAME)
[ WHERE) ]
Query:
Output:
Preparedness 5
Performance 5
Result 5
Record 5
Total 20
INFERENCE: