Shubham Record Work
Shubham Record Work
EX.NO:1
SQL DATA DEFINITION LANGUAGE COMMANDS
DATE:
AIM:
To write a program for SQL Data Definition Language Commands on sample exercise.
DATADEFINITIONLANGUAGE[DDL]:
1. CREATECOMMAND:
Used to create objects in database.
SYNTAX:
Basic syntax of CREATE TABLE statement is as follows:
CREATE TABLE
table_name( column1
datatype,
column2datatype,
column3datatype,
.....
Column Ndatatype,
);
Input query:
create table stdinfo(
fnamevarchar(50),
lnamevarchar(50),
course varchar(30),
email varchar(15)
)
desc stdinfo;
2. ALTER Command:
The SQL ALTER TABLE command is used to add, delete or modify
columns in an existing table. We would also use ALTER TABLE command
to add and drop various constraints on an existing table.
SYNTAX:
2A)The basic syntax of ALTER TABLE to add a new column in an existing table
is as follows: ALTER TABLE table_name ADD column_name datatype;
2C)The basic syntax of ALTER TABLE to change the DATA TYPE of a column
in a table is as follows:
ALTER TABLE table_name MODIFYCOLUMN column_namedatatype;
Input query:
desc stdinfo;
Drop column:
alter table stdinfo drop column fname;
desc stdinfo
Modify COLUMN:
alter table stdinfo
modify lnamevarchar(100);
desc stdinfo;
Desc delete2
3 DROPCOMMAND:
The DROP TABLE statement is used to drop an existing table in a database.
SYNTAX:
DROP TABLE table_name
INPUT QUERY
4 TRUNCATE COMMAND:
The TRUNCATE TABLE statement is used to delete the data inside a table,
but not the table itself.
SYNTAX:
TRUNCATE TABLE table_name;
INPUT QUERY:
INPUT QUERY:
Alter table stdinfo rename to stdinfo2;
INPUT QUERY:
rename stdinfo to stdinfo2;
desc stdinfo;
BEFORE ALTER RENAME
RESULT:Thus, the query for SQL Data Definition Language Commands are written, executed
and the outputs are verified successfully.
AIM:
TowriteaprogramforSQLDataManipulationLanguageCommandsonsampleexercise.
DATAMANIPULATIONLANGUAGE:
DML commands are the most frequently used SQL commands and is
used to query and manipulate the existing database objects.
1. INSERT COMMAND:
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.
SYNTAX:
INSERTING A SINGLE ROW INTO A TABLE:
insertinto<tablename>values(fieldvalue-1,fieldvalue-2,…,fieldvalue-n);
CREATE TABLE
create table dta(
snovarchar(10),
sender char(10) not null,
age char(10) not null,
receiver char(10) not null
)
desc dta;
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
INPUT QUERY:
INSERT COMMAND:
insert into dta values('1','raj','20','mohan');
insert into dta values('2','manju','22','ram');
insert into dta values('3','manoj','25','shayam');
2. SELECT COMMAND:
This command is used to get data out of the database. It helps users of the database to
access from an operating system, the significant data they need. It sends a track result
set from one table or more.
SYNTAX:
SELECT *FROM <table_name>;
INPUT QUERY:
select age from dta;
OUTPUT
3. UPDATE:
It is used to alter the column values in a table. A single column may be
updatedor more than one column could be updated.
SYNTAX:
UPDATE<table name>set(fieldname-1 = value, fieldname-2 = value,…,fieldname-n
= value) [WHERE <condition/expression>];
INPUT QUERY:
update BGMIX set Age = '21' where Age= 20;
OUTPUT:
UPDATE COMMAND:
4. DELETE:
After inserting row in a tables, we can also delete them if required. The delete
command consists of a from clause followed by an optional where clause.
SYNTAX:
DELETEFROM<tablename>[where<condition/expression>];
INPUT QUERY:
delete from dta where age='22';
OUTPUT:
RESULT:Thus, the query for SQL Data Manipulation Language Commands on sample exercise
was executed and verified successfully.
AIM:
To write a program for SQL Data Control Language Commands and Transaction
Control Commands on sample exercise.
GRANT:
It is used to give user access privileges to a database.
SYNTAX:
GRANT SELECT ,UPDATE ON TABLE NAME TO
SOME_USER,ANOTHER_USER;
REVOKE:
It is used to take back permissions from the user.
SYNTAX:
REVOKE SELECT,UPDATE ON TABLE NAME FROM USER1,USER2;
CODE:
CREATE USER 'shubham'@'localhost' IDENTIFIED BY 'root@123';
CODE:
SELECT user, host FROM mysql.user WHERE user = 'shubham';
COMMIT:
Commit command is used to save all the transactions to the database.
SYNTAX:
commit;
SYNTAX:
rollback;
SAVEPOINT:
Save point command is to Seta save point with in a transaction . If transaction
happens in big data, then for checking and rollup can't do it with all the data, to
rollback the small part of the data we use save point query.
SYNTAX:
SAVE POINT savepoint _ name
SETTRANSACTION:
Set command is to Specify the characteristics of the transaction.
SYNTAX:
SET TRANSACTION Access NAME transaction_name
RESULT: Thus the query for SQL Data Control Language Commands and Transaction
Control Commands on sample exercise was executed and verified successfully.
AIM:
To implement query for inbuilt functions using SQL.
SQL Functions:
Functions in SQL Server are the database objects that contain a set of SQL statements to
perform a specific task. The main purpose of functions is to replicate the common task
easily. We can build functions one time and can use them in multiple locations based on our
needs. SQL Server does not allow to use of the functions for inserting, deleting, or updating
records in the database tables.
SQL functions are categorized into the following two categories:
1. Aggregate Functions
2. Scalar Functions
.
AGGREGATE SQL FUNCTIONS
The Aggregate Functions in SQL perform calculations on a group of values and then return a
single value. Following are a few of the most commonly used Aggregate Functions:
Function Description
SUM() Used to return the sum of a group of values.
COUNT() Returns the number of rows either based on a condition, or without a
condition.
AVG() Used to calculate the average value of a numeric column.
MIN() This function returns the minimum value of a column.
MAX() Return maximum value of a column.
FIRST() Used to return the first value of the column.
LAST() This function returns the last value of the column.
Function Description
LCASE() Used to convert string column values to lowercase
UCASE() This function is used to convert a string column values to Upper case.
LEN() Returns the length of the text values in the column.
MID() Extracts substrings in SQL from column values having String data type.
ROUND() Rounds off an numeric value to then nearest integer.
NOW() This function is used to return the current system date and time.
FORMAT( Used to for math a field must be displayed.
)
CHARACTER/STRING FUNCTION:
EXP.NO:5
CONSTRUCTING AN ER MODEL FOR THE APPLICATION
DATE:
AIM:
ENTITYRELATIONSHIPDIAGRAM:
An ER diagram shows the relationship among entity sets. An entity set is
a group of similar entities and these entities can have attributes.
Inter ms of DBMS,an entity is a table or attribute
ofatableindatabase,sobyshowingrelationshipamongtablesandtheirattributes,ERd
iagramshowsthecompletelogicalstructureofadatabase.
COMPONENTSOFERDIAGRAM:
An ER diagram has three main components:-
1. Entity:
Anentityisanobjectorcomponentofdata.AnentityisrepresentedasarectangleinanERdiagr
am.
Ithas2types:
- Weak Entity (represented by a double rectangle)
- Strong Entity (represented by a single rectangle).
2. Attributes:
An attribute describes the property of an entity.An attribute is represented as ER
diagram. There are four types of attributes:
- Key attribute
- Composite attribute
- Multi value attribute
- Derived attribute
3. Relationships:
A relationship is represented by a diamond shape in ER diagram ,its how the
relationship among entities.There are four types of relationships:
-One to One
-One to Many
-
-Many to One
-Many to Many
KEYCONSTRAINTS:
FOREIGNKEY:
Foreign Key is used to relate two tables. The relationship between the two tables
matches the Primary Key in one of the tables with a Foreign Key in the second table.
This is also called are ferencing key.
ER DIAGRAM:
RESULT :Thus the Entity Relationship Diagram for student database has been constructed
and verified successfully.
EXP :6
Nested Queries
DATE:
RESULT: Thus the Nested Query commands on a sample exercise using SQL was executed and
verified successfully.
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
EXP:7 JOINS
DATE:
JOIN QUERIES:
● SQL joins are used to query data from two or more tables, based on a
relationship
between certain columns in these tables.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;
CREATE:
create table first(
course_idvarchar(10),
title varchar(10),
dept_namevarchar(10),
credits varchar(10)
)
INPUT QUERY:
SELECT first.course_id
FROM first
INNER JOIN second
ON first.course_id = second.course_id;
SELF JOIN:
A self join is a regular join, but the table is joined with itself.
Syntax:
SELECT column_name(s)
FROM table1 T1, table1 T2
WHERE condition;
INPUT QUERY:
SELECT
f1.course_id AS course_id1,
f1.title AS title1,
f2.course_id AS course_id2,
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
INPUT QUERY:
SELECT first.course_id, first.title, first.dept_name, first.credits, second.faculty_name
FROM first
LEFT OUTER JOIN second
ON first.course_id = second.course_id;
Syntax:
SELECT column_name(s)
FROM table1
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
RIGHT JOIN table2
ON table1.column_name = table2.column_name;
INPUT QUERY:
select first.course_id, first.title, first.dept_name, first.credits, second.faculty_name
from first
right join second
on first.course_id=second.course_id;
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name = table2.column_name
WHERE condition;
INPUT QUERY:
SELECT first.course_id, first.title, first.dept_name, first.credits, second.faculty_name
FROM first
FULL OUTER JOIN second
ON first.course_id = second.course_id;
Syntax:
SELECT column_lists
FROM table1
CROSS JOIN table2;
INPUT QUERY:
SELECT *
FROM first
CROSS JOIN second;
RESULT:Thus the implementation of join queries using SQL was executed and verified
successfully.
EXP:8
SET OPERATORS & VIEWS
DATE:
SET Operators:
SET operators are special type of operators which are used to combine the result of
two queries.
Union:
UNION will be used to combine the result of two select statements. Duplicate rows
will be eliminated from the results obtained after performing the UNION operation.
Syntax:
SELECT *FROM (table_name1) UNION SELECT *FROM (table_name2);
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
UNION:-
select *
from std
union select * from extra;
Union All:
This operator combines all the records from both the queries. Duplicate rows will be
not be eliminated from the results obtained after performing the UNION ALL operation.
Syntax:
SELECT *FROM (table_name1) UNION ALL SELECT *FROM (table_name2);
UNION ALL:-
select *
from std
union ALL select * from extra;
Intersect:
It is used to combine two SELECT statements, but it only returns the records which
are common from both SELECT statements.
Synatx:
SELECT *FROM (table_name1) INTERSECT SELECT *FROM (table_name2);
INTERSECT:-
SELECT name
FROM std
INTERSECT
SELECT name
FROM extra;
Minus(Except):
It displays the rows which are present in the first query but absent in the second query
with no
duplicates.
Synatx:
SELECT *FROM(table_name1) MINUS SELECT *FROM (table_name2);
MINUS:-
select *
from std
DATABASE MANAGEMENT SYSTEM LAB-18CSC303J
Syed Hasan Ahmed RA2111008020174
MINUS select * from extra;
VIEWS:
1. CREATE VIEW command is used to define a view.
Syntax:
CREATE VIEW (view_name) AS SELECT (column1, column2.....)
FROM (table_name) WHERE (condition);
VIEW:-
CREATE VIEW VIEW1 AS SELECT NAME,CITY
FROM STD ;