You are on page 1of 26
DBMS LAB PRACTICAL INTEGRATED INSTITUTE OF TECHNOLOGY DWARKA, SEC-09, NEW DELHI Submitted by - Prashant Kumar Univ Roll No - 01350102019 Class - BCA 2™Sem Submitted to - Esha Saxena Mam INDEX Exp No. Name Of Experiment Page | Date | Sign 41 Introduction to SQL: Its features and data types used 3-4 2 SQL query to create a table 5 3 SQL query for inserting data into table 6 4 SQL query to display table 7 5 SQL query for AND and OR operator 8 6 SQL query using WHERE clause 9 7 SQL query using ORDER BY and GROUP BY clause 10 8 Aggregate functions of SQL 11-13 9 Arithmetic Functions of SQL 14-15 10 ‘SQL query for updating rows 16 4 SQL query for ADD , DROP and RENAME 17 12 ‘SQL query for deleting rows from a table 18 13 Create a table using integrity constriant 19-20 14 Create an EMPLOYEE table 21 15 Create given table and perform queries on it 22-26 EXPERIMENT - 1 > Aim: Introduction to SQL: Write its features and data types used. + Features of SQL Structured Query Language (SQL) is the standard language used for writing queries in a databases. SO (International Standard Organization) and ANSI (American National Standards Institute) approved it. SQL contains of some important features and they are: 4. Data Definition tanguage (DDL) - It contains of commands which defines the data. The commands are: (a) Create - Itis used to create a table. (b) Drop - It is used to delete the table including all the attributes. (c) Alter - Alter is a reserve word, which modifies the structure of the table. (d) Rename - A table name can be changed using the keyword ‘rename’ Data Manipulation Language (DML) - Data Manipulation Language contains commands used to manipulate the data. The commands are: (a) Insert - This command is generally used after the create command to insert a set of values into the table. (b) Delete - A command used to delete particular tuples, rows, or cardinality from the table. (c) Update - It updates the rows and columns of a table. Triggers- Triggers are actions performed when certain conditions meet on the data. A trigger contains of three parts. (a) Event - The change in the database that activates the trigger is event. (b) Condition - A query is executed when the trigger is activated. (c) Action - A procedure runs when trigger activates and the condition is true. Client server execution and remote database access - Client server technology maintains a many to one relationship of clients (many) and server (one). We have commands in SQL that control how a client application can access the database over a network. 5. Security and authentication - SQL provides a mechanism to control the database meaning it makes sure that only the particular details of the database is to be shown the user and the original database is secured by DBMS. 6. Embedded SQL - SQL provides the feature of embedding host languages such as C, COBOL, Java for query from their language at runtime. 7. Transaction Control Language - Transactions are an important element of DBMS and to control the transactions, TCL is used which has commands like commit, rollback and savepoint. (a) Commit - It saves the database at any point whenever database is consistent. (b) Rollback - It rollbacks/undo to the previous point of the transaction. (c) Savepoint - It goes back to the previous transaction without going back to the entire transaction. 8. Advanced SQL - The current features include OOP (Object Oriented Programming) ones like recursive queries, decision supporting queries and also query supporting areas like data mining, spatial data and XML (Extensible Markup Language). * SQL Data Types SQL data types can be broadly divided into following categories: Numeric data types such as INT, TINYINT, BIGINT, FLOAT, REAL etc. Date and Time data types such as DATE, TIME, DATETIME etc. Character and String data types such as CHAR, VARCHAR, TEXT etc. Unicode character string data types, for example NCHAR, NVARCHAR, NTEXT etc. Binary data types such as BINARY, VARBINARY etc. Miscellaneous data types - CLOB, BLOB, XML, CURSOR, TABLE etc. Pons oa EXPERIMENT - 2 > Aim: To write SQL query to create a table. SYNTAX: CREATE TABLE TABLE_NAME ( COLUMN1 DATATYPE, COLUMN2 DATATYPE, COLUMNS DATATYPE...); For Example: create table Students ( Roll int primary key not null, Name varchar(20) not null, Marks int not null, Gender varchar(1@) not null); EXPERIMENT - 3 > Aim: To write SQL query for inserting data into a table. SYNTAX: INSERT INTO TABLE_NAME (COLUMN1, COLUMN2, COLUMNS...) VALUES (VALUE1, VALUE2, VALUES...); For Example: insert into students values Jatin", 72, "Male”), » 78, "Female”), “Gautam”, 65, “Male"), "Siddharth", 71, "Male"); EXPERIMENT - 4 > Aim: To write SQL query to display table. SYNTAX: SELECT * FROM TABLE_NAME, For Example: select * from Students; Roll Name Marks Gender 33 Jatin 72 Male Kad Devanshi 70 Female 36 Gautam 65 Male 37 Siddharth 71 Male EXPERIMENT —-5 > Aim: To write SQL query for AND and OR operator. « AND Operator SYNTAX: SELECT COLUMN1, COLUMN2... FROM TABLE_NAME WHERE CONDITION1 AND CONDITIONZ2...; select * from Students where Marks > 78 And Gender = “Male”; Roll Name Marks Gender 33 Jatin 72 Male 37 Siddharth 71 Male OR Operator: SYNTAX: SELECT COLUMN1, COLUMN2... FROM TABLE_NAME WHERE CONDITION‘ OR CONDITIONZ...; select * from Students where Marks > 63 or Gender = “Female”; Roll Name Marks Gender 33 Jatin 72 Male 34 Devanshi 70 Female 37 Siddharth 71 Male EXPRIMENT - 6 > Aim: To write SQL query using WHERE clause. SYNTAX: SELECT COLUMN1, COLUMN2... FROM TABLE_NAME WHERE CONDITION; For Example: select name from Students where Gender = "Male"; name Jatin Gautam Siddharth EXPERIMENT - 7 > Aim: To write SQL query using ORDER BY and GROUP BY clause. ORDER BY Clause: SYNTAX: SELECT COLUMN1, COLUMN2... FROM TABLE_NAME ORDER BY COLUMN1, COLUMN2... ASC|DESC; Roll Name Marks Gender 33 Jatin 72 Male select * from Students 37 siddhath 71 Male 4 Devanshi 70 Female order by Marks Desc; 36 Gautam 65 Male GROUP BY Clause: SYNTAX: SELECT COLUMN1,COLUMN2... FROM TABLE_NAME GROUP BY COLUMN3; select count(Roll) as Number_of_Male Students from Students group by Gender having gender = "Male"; Number _of_Male_Students 3 10 EXPERIMENT -8 > Aim: To write the functions listed in aggregate functions. COUNT() The COUNT() function returns the number of rows that matches a specified criteria. SYNTAX: SELECT COUNT(COLUMN_NAME) FROM TABLE_NAME WHERE CONDITION; select (Roll) as Number_of Male Students from Students group by Gender having gender = "Male"; Number_of_Male_Students S AVG() The AVG() function returns the average value of a numeric column. SELECT AVG(COLUMN_NAME) FROM TABLE_NAME WHERE CONDITION; select avg(Marks) as Average_Marks from Students; Average_Marks 69,5000 + SUM() The SUM() function returns the total sum of a numeric column. SYNTAX: SELECT SUM(COLUMN_NAME) FROM TABLE_NAME WHERE CONDITION; select sum(Marks) as Marks Combined Marks_Combined from Students; 278 * MIN The MIN() function returns the smallest value of the selected column. SYNTAX: SELECT MIN(COLUMN_NAME) FROM TABLE_NAME WHERE CONDITION; select min(Marks) as Minimum Marks Minimum _Marks from Students; 65 12 « MAX() The MAX() function returns the largest value of the selected column. SYNTAX: SELECT MAX(COLUMN_NAME) FROM TABLE_NAME; select max(Marks) as Maximum_Marks Maximum _Marks from Students; T2 13 EXPERIMENT - 9 > Aim: To write Arithmetic Functions of SQL. « POW() The POW() function returns the value of a number raised to the power of another number. Name Square_of_Marks Jatin 5184 select Name,pow(Marks, 2) Devanshi 4900 Gautam 4225 as Square_of_Marks Siddharth 5041 from Students; ¢ DATE MySQL comes with the following data types for storing a date or a date/time value in the database: = DATE - format YYYY-MM-DD = DATETIME - format: YYYY-MM-DD HH:MI:SS = TIMESTAMP - format: YYYY-MM-DD HH:MI:SS = YEAR - format YYYY or YY « UPPER() The UPPER() function converts a string to upper-case. select upper(Name) as Names_in_ Uppercase from Students; + LOWER() Names_in_Uppercase JATIN DEVANSHI GAUTAM SIDDHARTH The LOWER() function converts a string to lower-case. select lower(Name) as Names_in_Lowercase from Students; Names_in_Lowercase jatn devanshi gautam siddharth 15, EXPERIMENT - 10 > Aim: To write SQL query for updating rows. SYNTAX: UPDATE TABLE_NAME SET COLUMN1 = VALUE1, COLUMN2 = VALUE2, ... WHERE CONDITION; update Students set Marks = 68 where Name = “Gautam”; select * from Students; Roll Name Marks Gender 33 Jatin 72 Male 4 Devanshi 70 Female 36 Gautam 68 Male 37 Siddharth 71 Male 16 EXPERIMENT - 11 > Aim: To write SQL query for ADD , DROP and RENAME. « ADD ADD function is used to add a new column in your table. alter table Students add (Branch varchar(20) default “Unknown"); select * from Students; Roll Name Marks Gender Branch 33 Jatin 72 Male Unknown 34 Devanshi 70 Female Unknown 36 Gautam 68 Male Unknown 37 Siddharth 71 Male Unknown « DROP DROP function is used to delete a table. Example: DROP table table_name; « RENAME RENAME function is used to rename column name or table name. alter table Students rename to Classmates; select * from Classmates; 7 EXPERIMENT - 12 > Aim: To write SQL query for deleting rows from a table. DELETE FROM table_name WHERE condition; delete from Classmates where Name = "Gautam"; select * from Classmates; Roll Name Marks Gender Branch 33 Jatin 72 Male Unknown 4 Devanshi 70 Female = Unknown 37 Siddharth 71 Male Unknown 18 EXPERIMENT - 13 > Aim: To create a table using integrity constriant. Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement. The following constraints are commonly used in SQL: + NOT NULL - Ensures that a column cannot have a NULL value. UNIQUE - Ensures that all values in a column are different. + PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table. + FOREIGN KEY - Uniquely identifies a row/record in another table. + CHECK - Ensures that all values in a column satisfies a specific condition. + DEFAULT - Sets a default value for a column when no value is specified. + INDEX - Used to create and retrieve data from the database very quickly. = Using CREATE table command: SYNTAX: CREATE TABLE TABLE_NAME ( COLUMN1 DATATYPE CONSTRAINT, COLUMN2 DATATYPE CONSTRAINT, COLUMNS DATATYPE CONSTRAINT...); create table Students ( Roll int primary key not null, Name varchar(2@) not null, Marks int not null, Gender varchar(1@) not null); = Using ALTER table command: ALTER TABLE TABLE_NAME ADD CONSTRAINT (COLUMN_NAME); alter table Students add (Branch varchar(2@) default “Unknown"); select * from Students; 20 EXPERIMENT - 14 > Aim: To create an EMPLOYEE table. Create table Employee ( EMPNO int, ENAME varchar(26), JOB varchar(15), HIREDATE varchar(20), MGR int, SAL int, CoM int null, DEPTNO int); describe Employee; | Field Type Null Key Default |EMPNO i il 7 | int(11) YES |ENAME varchar(20) YES /208 varchar(15) YES |HIREDATE varchar(20) YES MGR int(i1) YES SAL int(11) YES |comm int(11) YES |DEPTNO —_—int(11) YES 21 EXPERIMENT - 15 > Aim: To create given table and perform queries on it. EMPNO | ENAME | JOB HIREDATE | MGR| SAL | COMM] DEPTNO 7369 | SMITH | CLERK 17-DeCc-80 | 7902 | 800 20 7499 | ALLEN | SALESMAN | 20-FEB-81 | 7698 | 1600 | 300 30 7521 | WARD | SALESMAN | 22-FeB-81 | 7698 | 1250 | 500 30 7566 | JONES | MANAGER | 02-APR-81 | 7839 | 2975 20 7654 | MARTIN | SALESMAN | 28-SEP-81 | 7698 | 1250| 1400 | 30 7698 | BLAKE | MANAGER | 01-MAyY-81 | 7839 | 2850 30 7782 CLARK | MANAGER | 09-JUN-81 | 7839 | 2450 10 7788 SCOTT | ANALYST 19-APR-87 | 7566 | 3000 20 7839 | KING PRESIDENT | 17-NOV-81 5000, 10 7344__| TURNER | SALESMAN | 08-SEP-81 _| 7698 | 1500 | 0 30 7876 | ADAMS | CLERK 23-MAY-87 | 7788 | 1100 20 7900 | JAMES | CLERK 03-DEC-81 | 7698 | 950 30 7902 | FORD | ANALYST | 03-Dec-81 | 7566 | 3000 20 7934 | MILLER | CLERK 23-JAN-82_[ 7782 | 1300 10 o Create the above mentioned table. Create table Employee ( EMPNO int, ENAME varchar(2@), 308 varchar(15), HIREDATE varchar(20), MGR int, SAL int, COMM int null, DEPTNO int); describe Employees 22 Field Type Null Key Default EMPNO _int(11) YES ENAME ——varchar(20) YES 108 varchar(15) YES HIREDATE —varchar(20) YES MGR int(11) YES SAL int(11) YES comm int(1) YES DEPTNO _int(11) YES 1. Insert all the data in the table mentioned above. insert into employee values( 7365," SMITH’, "CLERK" ,'17-DEC-82' ,7902,800,nul1, 28); insert into employee values( 7499, ALLEN’, SALESMAN’, '2@-FEB-81' , 7698, 1600, 300,35 insert into employee values( ~ 7521, ‘WARD’ , "SALESMAN" ,'22-FEB-81' ,7698,1250,500, 30); insert into employee values( 7566, ' JONES’, MANAGER" y'@2-APR-61' ,7839,2975,null,20)5 © insert into employee values( ‘7654, ‘MARTIN’ , "SALESMAN’ , 28-SEP-81’ ,7698,1250, 1400, 38); ) insert into employee velues( 7698, "BLAKE", "MANAGER", MAY-B4' 7839 ,285@,null,30)5 ) insert into employee values( ‘7782, "CLARK" , "MANAGER" ,'@3-JUN-BL' ,7834, 2458,nul1, 1 insert into employee values( 7788, 'SCOTT* , "ANALYST" y'19-APR-87' ,7566,3000,nul1,20)5 insert into employee values( 7839, KING", PRESIDENT’ ,' 17-NOV~-81" ,nul1,5@00,nul1, 19) insert into employee values( 7844, ' TURNER’ , ‘SALESMAN’, "@8-SEP-81' ,7698,1500,nul1,30)5 23 insert into employee values( 7876, ' ADAMS’, ‘CLERK’ ,'23-NAY-87' ,7788,1100,nu11,20)5 insert into employee values( 7900, "JANES", ‘CLERK’ ,"@3-DEC-81' ,7698,950,null,3@)5 ) insert into employee values( 7902,‘ FORD’ , ANALYS” @3-DEC-B1' 57566, 3000,nu11,20); insert into employee values( 7934, "MILLER" y "CLERK" »'23-JAN-82' ,7782,1300,nul1,10)3 [ EMPNO ENAME 108 HIREDATE MGR SAL COMM _DEFTNO 7369 «SMITH CLERK T7DECSO 7902 800 MEY ag 7989 ALLEN SALESMAN 20-FEBS1 7698 1600 300 30 7521 WARD «SALESMAN 22-FEBS1 7698 1250 500 © 30 7566 JONES MANAGER OZ-APR-81. 7839-2975 2 7654 MARTIN SALESMAN 28-SEP-81 7698 30 7698 “BLAKE. © MANAGER) (OL-MAY-81 7839 0 7782 CLARK «MANAGER = 09-JUN-81 7839 10 7788 SCOTT ANALYST ~—«1S-APR'87 7566 2» 7839 «KING PRESIDENT 17-NOV81 10 7e44 TURNER SALESMAN 08-SEP-81 7698 30 7876 ADAMS = CLERK 23-MAY-87 7788 2 7300 «JAMES CLERK O3-DEC-81 7698 30 7902 FORD «ANALYST = OB-DEC-81 7566 20 7934 MILLER CLERK 2BJANS2 7782 10 24 2. List all employees, whose job is CLERK. SELECT * FROM employee WHERE JOB='CLERK'5 EMPNO ENAME 308 -HIREDATE MGR SAL COMM __DEPTNO > |7369 SMITH CLERK 17-DEC-80 7902 800 20 7876 ADAMS CLERK = 23-MAY-87 7788 1100 20 7900 JAMES CLERK ©» O3-DEC-81 30 7934 MILLER CLERK © -23-JAN-82 10 25 3. List all employees in the following format: EMPNO , ENAME and MGR. SELECT EMPNO, ENAME, MGR FROM employee; | |EMPNO ENAME MGR > (7369 «SMITH ~=—-7802 |7499 ALLEN ©7698 7521 WARD (7698 | 7566 JONES 7839 |7654 © MARTIN 7698 (7698 BLAKE 7839 | 7782 CLARK 7839 l7788 © SCOTT «7556 (7339 —(KING |7944 TURNER 7598 |7876 ADAMS 7788 | 7900 JAMES 7698, [7902 FORD = 7566 [7934 MILLER-782

You might also like