DBMS file
DBMS file
Table of Contents:
• What is Data Manupulation Language(DML)?
• INSERT INTO Commmand
• UPDATE Command
• DELETE Command
Syntax:
INSERT INTO NAME_OF_TABLE (1_column, 2_column, 3_column,….N_column)
VALUES(1_value, 2_value, 3_value, …N_value);
Or
INSERT INTO NAME_OF_TABLE
VALUES (1_value, 2_value, 3_value, ….N_value);
Example:
INSERT INTO Student(STU_NAME, DOB, Phone, Mail)
VALUES(‘Altamash’, 2002-05-01 , 9027693960, ‘user@xyz.com’);
o UPDATE Command
This Statement in SQL is used to update the data that is present in an existing table of a
databases.The UPDATE statement can be used to update single or multiple columns on the basis of
our specific needs.
Syntax:
UPDATE name_of_table SET 1_column=1_value, 2_column=2_value, 3_column=3_value, …,
N=column=N_value
WHERE condition;
And here,
Name_of_table: name of the table
1_column, 2_column,3_column,…N_column:name of the first,second,third,…nth column.
1_value, 2_value, 3_value,…N_value: the new value for the first,second,third,…nth column.
Condition: the condition used to select those rows for which the column values need to be updated.
Example:
UPDATE Student SET Phone= 9027693960 WHERE Stu_Name= ‘Altamash’;
The WHERE clause in the preceding query is used to select the rows for which the columns need to
be adjusted , and the SET statement has been used to assign new values to a particular column. If
the WHERE clause is not used at all,then all of the rows ,columns will be modified.As a result, the
WHERE clause is used to pick specific rows from the table.
Thus, the example query would update the phone number of the Student with the name ‘Altamash’.
o DELETE Command:
The DELETE Statement can be used in SQL to delete various records from a given table.On the
basis of the condition that has been set in the WHERE clause, one can delete single or multiple
records.
Syntax:
DELETE FROM name_of_table[WHERE condition];
Example:
DELETE FROM Student WHERE Stu_Name= ‘Altamash’;
The Command given above would delete the record for the student with the name ‘Altamash’from
the ‘Student’ table.Apart from this, one can also use the LOCK Table statement to explicitly acquire
the shared or exclusive table lock on a specified table.
a. DDL: A data definition language or data description language (DDL) is syntax
similar to a computer programming language for definition data structures
,especially database schemas-
Commands in DDL are:
a. CREATE
b. DROP
c. TRUNCATE
d. RENAME
e. ALTER
o DDL Commands
The Data definition Languages (DDL) Commands are as follows-
• Create-It is used to create a new table or a new database.
• Alter-It is used to alter or change the structure of the database table.
• Drop-It is used to delete a table ,index or views from the database.
• Truncate-It is used to delete the records or data from the table ,but its structure
remains as it is.
• Rename-It is used to rename an object from the database.
o DDL Commands with example
Let’s see each DDL command with example.
➢ Create
It is used to create a new table or a new database.
An example of create command is as follows-
Create table student(std-name var char(20), branch var char(20),college var char(20), age
number, telephone number , address var char(20));
A student table is create with the field given below-
➢Alter
It is used to alter or change the structure of the database table
An example of the alter command is as follows-
ALTER TABLE student ADD birth-data DATATIME
➢Drop
It is used to delete a table, index, or views from the database.
An Example of the drop command is as follows –
DROP TABLE Student :-
Q6. Write a query to drop am existing table employee.SQL>
DROP table employee;
Table deleted.
➢ DML QUERIES:
Q1. Write a query to insert the records in to employee?
SQL>INSERT INTO EMP VALUES(103,’Altamash’,’ASST_PROF’,50000);
1 row created.
Q3. Write a query to insert the records in to employee using substitution method?
SQL> INSERT INTO EMPLOYEE
VALUES(&EMPID,’ENAME’,&DESIGNATION’,’&SALARY’);
Enter value for empid:108
Enter value for name: SHILPI Enter the value for designation:ASST_Prof Enter value for
salary:49000.
Old 1:INSERT INTO emply values(&Empid,’&Name’,’&Designation’,’&Salary’);
New 1:INSERT into employee values(108,’Shilpi’,’ASST_PROF’,60000);
1 Row created.
Q4. Write a Querry to Update the records from employee?
SQL> UPDATE employee SET Salary = 49000 WHERE EmpId= 108;
1 Row Updated.
SQL> Select * from employee;
AIM-2: Write SQL Querries using Logical Operator.
Theory:
An Operator is a reserved word or a character used primarily in an SQL statement’s
WHERE clause to perform operation(s).Such as Comparisons and arithmetic operations.These
operators are used to specify conditions in an SQL statement and to serve as conjuctions for
multiple conditions in a statement.
• Arithmetic Operators
• Comparison Operators
• Logical Operators
• Operators used to negate conditions
Pte-Requisite Data:
CUSTOMER TABLE
Querries;
Q1. Write a Querry to find the salary of a person where age is <=26 and Salary
>=25000 from customer Table.
SQL>SELECT from Customers Where AGE <=26 and Salary >= 25000.
Output:-
2 Rows selected.
AIM-3: Write SQL Querry using Group by Function.
Theory:
The GROUP BY Statement in SQL is used to arrange identical data into groups with the
help of some functions. i.e if a particular column has same values in different rows then it will
arrange these rows in a group. Important Points:
• GROUP BY clause is used with the SELECT statement.
• In the query, GROUP BY clause is placed after the WHERE clause.
• In the query, GROUP BY clause is placed before ORDER BY clause if used any
• In the query, GROUP BY clause is placed before having clause.
• Placed condition in the having clause.
Syntax:
SELECT column1, function_name(column2)
FROM table_name
WHERE condition
GROUP BY column1,column2
ORDER BY column1 ,column2;
Function_name: Name of the function used for example, SUM(), AVG().
Table_name: Name of the Table.
Condition: Condition Used.
➢ Sample Table is as Follows:
Employee
Student
Example:
➢ Group by single Column: Group by single column means,to place all the rows with
same value of only that particular column in one group.Consider the querry as shown
below:
SELECT NAME, SUM(SALARY) from Employee
Group by NAME;
The above querry will produce the below output:
As you can see in the above output, the rows with duplicate NAMEs are grouped under the same
name and their corresponding SALARY is the sum of the SALARY of duplicate rows.The SUM()
function of SQL is used here to calculate the sum.
Output: As you can see in the above output the students with both same SUBJECT and YEAR
are placed in same group. And those whose SUBJECT is same but not YEAR belong to
different groups. So here we have grouped the table according to two columns or more than
one column.
Outcome:
To understand the SQL Querry using group by Function.
AIM-6: Write an SQL querry to implement JOINS.
Theory:
A SQL join clause combine columns from one or more tables in a relational database.It
creates a set that can be saved as a table or used as it is. A JOIN is a means for combining
columns from one (self-table) or more table by using values comman to each.ANSI-standard
SQL specifies five types of JOIN: INNER, LEFT, OUTER and CROSS. As a special case, a
table (base table, view, joined table) can JOIN to itself in a self-join.
A Programmer declares a JOIN statement to identify rows for joining.If the evaluated
predicate is true, the combined rows is then produced in the expected format, a row set or a
temporary table.
QUERRIES:
EMPLOYEE TABLE
DEPARTMENT TABLE
Q1. Display the employee details, departments that the departments are same in
both the empand dept.
SQL> select * from emp, dept where emp.deptno= dept.deptno;
Outcome:
To understand the implementation of JOINS using SQL.
Q9. Consider following databases and draw ER diagram and convert entities and
relationships to relation table for a given scenario.
1. COLLEGE DATABASE:
STUDENT (USN, SName, Address, Phone, Gender)
SEMSEC (SSID, Sem, Sec)
CLASS (USN, SSID)
SUBJECT (Subcode, Title, Sem, Credits)
IAMARKS (USN, SSID, Test1, Test2, Test3, FinalIA)