0% found this document useful (0 votes)
14 views30 pages

SQL

The document provides a comprehensive guide on SQL commands for various database operations including creating tables, inserting, updating, and deleting records, as well as using functions like ROUND, COUNT, and UPPER. It also covers advanced topics such as constraints, transaction control, and joins. Each command is illustrated with examples to demonstrate its usage effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
14 views30 pages

SQL

The document provides a comprehensive guide on SQL commands for various database operations including creating tables, inserting, updating, and deleting records, as well as using functions like ROUND, COUNT, and UPPER. It also covers advanced topics such as constraints, transaction control, and joins. Each command is illustrated with examples to demonstrate its usage effectively.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 30

SR.

PROGRAMS SIGNATURE
NO.
1. Write SQL command to create a
table with columns of your
choice. Write an SQL command to
add a new column to the table.
Write an SQL command to delete
the table from the database.
2. Write SQL command to insert a
new record into a table. Write an
SQL command to update a
column value in the table. Write
an SQL command to delete a
record from the table.
3. Write SQL commands showcase
the use ‘ROUND’, ‘COUNT’,
‘UPPER’, ‘SYSDATE’, and
‘TO_CHAR’ functions.
4. Write SQL command to perform
an arithmetic calculation on
numeric columns.
5. Write SQL command to find
records that satisfy either of two
conditions using logical operators.
6. Write SQL command using the
‘BETWEEN’ operator to find
records within a specific range.
7. Write SQL command to perform a
natural join between two tables.
8. Write SQL command to group
records by a column and count
the number of records in each
group, displaying only groups that
meet a specific condition.
9. Write SQL command to order
records by a specific column in
ascending or descending order.
10. Write SQL command to create a
view that displays specific
columns from a table.
11. Write SQL commands to
implement ‘PRIMARY KEY’ ,
‘FOREIGN KEY’, ‘UNIQUE’, ‘CHECK’,
and ‘NOT NULL’ constraints on a
table.
12. Write SQL commands to
demonstrate transaction control
using ‘ROLLBACK’, ‘COMMIT’, and
‘SAVEPOINT’.
13. Write SQL command to create a
new database or tablespace.
14. Write SQL commands to create a
user and delete a user.
15. Write SQL commands to grant and
revoke roles.
1.Write SQL command to create a table with columns
of your choice. Write an SQL command to add a new
column to the table. Write an SQL command to delete
the table from the database.
Create table command is used to define a table. Syntax of
CREATE TABLE command is
CREATE TABLE <table name >
(<column specification>,<column specification>…..);
Where <table name >gives the table name and <column
specification>includes
• Column name
• Data type
• Length of column ,if any
• Constraints ,if applicable
EXAMPLE
SQL> CREATE TABLE STUDENT (S_CODE CHAR(4),NAME
CHAR(30),FNAME CHAR(20),ADDRESS CHAR (30));
Table created.
SQL> SELECT*FROM STUDENT;
no rows selected

ALTER TABLE<table name>ADD<column specifications>


Or
ALTER TABLE<table name>
Modify<column name>
<new details for the column>
EXAMPLE
SQL> ALTER TABLE STUDENT ADD(CLASS INT);
Table altered.
DELETE COMMAND
Rows of a table may be deleted using a delete .it is of the
form
DELETE from<table name>
[Where <condition(s)>]
This command delete rows from the table specified. If the
WHERE clause is given; only rows meeting the condition(S)are
deleted. otherwise all rows are deleted. Example:
SQL> delete from student where s_code='1106'
1 row deleted.

This command will delete the student row whose s_code is


1106.

2.Write SQL command to insert a new record into a


table. Write an SQL command to update a column
value in the table. Write an SQL command to delete a
record from the table.
Insert command:- For inserting data into the tables,sql
has the INSERT command which is of form
INSERT INTO <table name>
[<column names>]
Values(<values>);
An insert is done for a row at a time .if the column names
are not specifies that values are mapped onto the columns
in the order as defined in the CREATE TABLE of the table
EXAMPLE
SQL> INSERT INTO STUDENT VALUES('1101','AARTI','PARAMJEET','SILANA');
1 row created.
SQL> INSERT INTO STUDENT VALUES('1102','BHAWNA','RAJBIR','BIDHLAN');
1 row created
SQL> INSERT INTO STUDENT VALUES('1103','KHUSHI','RITUPARAN','BAWANA');
1 row created.Ini
SQL> INSERT INTO STUDENT VALUES('1104','POOJA','JAGBIR','SILANA');
1 row created.
SQL> INSERT INTO STUDENT VALUES('1105','AKUL','RAMESH','BIDHLAN');
1 row created.
OUTPUT:-
S_CODE NAME FNAME ADDRESS
1101 AARTI PARAMJEET SILANA
1102 BHAWNA RAJBIR BIDHLAN
1103 KHUSHI RITUPARAN BAWANA
1104 POOJA JAGBIR SILANA
1105 AKUL RAMESH BIDHLAN
1106 TANNU KIRSHAN SONIPAT
This command will insert the values into specified columns
only.
UPDATE COMMAND
To modify data values within one or more columns for one
or more rows of a table, sql has a command called
update.it is of the form:
UPDATE<table name>
Set<column name>=<value expression>
[,<column name>=<value expression>
……
]
[Where<condition>]
The column whose values have to be updated and the
expressions to derive these values are included in the set
clause. The rows to be updated are those that meet the
condition(s)in the WHERE clause.
Example
SQL> UPDATE STUDENT SET ADDRESS='BIDHLAN'WHERE
S_CODE='1106';
1 row updated.

This command will change address of the student whose


s_code=1106.
S_CODE NAME FNAME ADDRESS
---------- -------------------- --------------- ------------------ ----1101
AARTI PARAMJEET SILANA
1102 BHAWNA RAJBIR BIDHLAN
1103 KHUSHI RITUPARAN BAWANA
1104 POOJA JAGBIR SILANA
1105 AKUL RAMESH BIDHLAN
1106 TANNU KIRSHAN BIDHLAN
6 rows selected.

DELETE COMMAND
Rows of a table may be deleted using a delete .it is of the
form
DELETE from<table name>
[Where <condition(s)>]
This command delete rows from the table specified. If the
WHERE clause is given; only rows meeting the
condition(S)are deleted. otherwise all rows are deleted.
SQL> delete from student where s_code='1106'
1 row deleted.

This command will deleted the student row whose s_code is


1106.

3.Write SQL commands showcase the use ‘ROUND’,


‘COUNT’, ‘UPPER’, ‘SYSDATE’, and ‘TO_CHAR’
functions.
ROUND
Returns n, rounded to m places to the right of a decimal
point. If m is omitted, n is rounded to 0 places.
Syntax is
ROUND(n[,m])
Example
SELECT ROUND (1.7825,1) FROM DUAL;
OUTPUT
SQL> SELECT ROUND (1.7825,1) FROM DUAL;
ROUND
…………..
1.8

COUNT
This function returns the number of rows or non-null
values for column x. When we use * in place of x, it returns
the total number of rows in the table.
SYNTAX is
COUNT([distinct I all] <x>)
Where x is the column name.
Count the number of employees in the EMPL table.
SQL>Select count(eno) from empl;
The output is
SQL>Select count(eno) from empl;
COUNT (END)
……………………….
4
UPPER(string)
This function converts the string into upper case.
Example
SQL>Select upper(ename) from empl;
OUTPUT
SQL>Select upper(ename) from empl;
UPPER(ENAME)
…………………………
AMIT
ANU
SONU
AMIT

SYSDATE
Example
SQL>SELECT ADD_MONTHS(SYSDATE,4)”Add Months”
FROM DUAL;
OUTPUT
SQL>SELECT ADD_MONTHS(SYSDATE,4)”Add Months” From
Dual;
Add Month
……………………..
29 - OCT - 10
CHR(X)
This function gives the result as a character corresponding
to the decimal number x in the database character set.
Example
SQL>Select chr (98) “first”, chr(99) “second” from dual;
OUTPUT
SQL>Select chr (98) “first”, chr(99) “second” from dual;
f s
- -
b c

4.Write SQL command to perform an arithmetic


calculation on numeric columns.
Command is
SELECT ENO, BASIC, BASIC * 0.3 FROM EMP;
Output is
SQL> SELECT ENO, BASIC, BASIC * 0.3 FROM EMP;
ENO BASIC BASIC * 0.3
………….. ……………….. ……………………….
1 5000 1500
2 8000 2400
3 8000 2400
4 6000 1800
5.Write SQL command to find records that satisfy
either of two conditions using logical operators.
The and operator allows creating an SQL statement based
on two or more conditions being met.
Query *
Get the employees whose basic pay greater than 6500 and
employee number less than 3.
Command is
SELECT * FROM EMP
WHERE BASIC > 6500 AND ENO < 3;
Output is
SQL > SELECT * FROM EMP
2 WHERE BASIC > 6500 AND ENO < 3;
ENO ENAME AGE BASIC
…………. …………….. ………….. ………….
1 Aman 17 8000

6.Write SQL command using the ‘BETWEEN’


operator to find records within a specific range.
Between Operator:-
This operator is used to search for values that are within
a set of values, given the minimum value and maximum
value.
The minimum value must be written first. The two
values in between the range must be linked with the
keyword AND.
Example
List the employees name and basic pay whose basic pay
between 5500 and 8000.
Command is
SELECT ENAME, BASIC
FROM EMP
WHERE BASIC BETWEEN 5500 AND 8000;
OUTPUT:-
SQL> SELECT ENAME, BASIC
2 FROM EMP
3 WHERE BASIC BETWEEN 5500 AND 8000;
ENAME BASIC
………………………… …………………
Amit 8000
Ritu 8000
John 6000

7.Write SQL command to perform a natural join


between two tables.
When the data to be retrieved from more than one
relation then both the relation names is specified in the
FROM clause and the join condition in the WHERE part.
Example
Find names of employees working in deptt. No. 10
SELECT ENAME
FROM EMP, WORK_IN
WHERE EMP.ENO = WORK_IN.ENO
AND WORK_IN.DNO =10;
OUTPUT
SQL> SELECT ENAME
2 FROM EMP, WORK_IN
3 WHERE EMP,ENO = WORK_IN.ENO
4 AND WORK_IN.DNO = 10;
ENAME
…………………………….
Amit
Aman
John

8.Write SQL command to group records by a column


and count the number of records in each group,
displaying only groups that meet a specific condition.
SELECT deptno, job, sum(salary)
FROM empll
GROUP BY deptno, job;
OUTPUT:-
SQL> SELECT deptno, job, sum(salary)
2 FROM empll
3 GROUP BY deptno, job;
DEPTNO JOB SUM(SALARY)
………………… ……………. ………………………..
10 Clerk 11000
10 Manager 9000
20 Analyst 21000
20 President 20000
30 Manager 9000
30 Salesman 11000

9.Write SQL command to order records by a specific


column in ascending or descending order.
Oracle allows data from a table to be viewed is a stored
order. The columns retrieved from the table will be sorted
in either ascending or descending order depending on the
condition specified in the SELECT sentence.
Example
Show the detail of all students according to the marks.
Command is
SELECT * FROM Student
ORDER BY Marks asc;
Default sort order is ascending therefore, you can also
write the above statement as
SELECT * From student
ORDER BY Marks;
Output is
SQL > SELECT * FROM STUDENT;
2 ORDER By Marks;
ROLL_NO NAME CLASS MARKS
……………… ……………… ……………. …………
1002 Gourav BIM 67
1008 Amit BBA – II 76
1009 Anil BBA 76
1007 Shankar BCA 77
1005 Ravi B.Sc – III 80
1003 Ashish BCA – II 89
6 rows selected.

10.Write SQL command to create a view that displays


specific columns from a table.
Example if you want to view only the information of two
columns Roll_no and name of table Student, you may write
your query as
SELECT Roll_no, Name FROM student;
This will display all rows present in the student table.
Output is
SQL > SELECT Roll_no, Name FROM student;
ROLL_NO NAME
………………….. ………………………………….
1005 Ravi
1003 Ashish
1007 Shankar
1002 Gourav
1009 Anil
5 rows selected.

11.Write SQL commands to implement ‘PRIMARY KEY’


, ‘FOREIGN KEY’, ‘UNIQUE’, ‘CHECK’, and ‘NOT NULL’
constraints on a table.
Primary key constraint
A primary key is used to identify each row of the table
uniquely. It is a combination of both the UNIQUE key
constraint as well as the NOT NULL constraint.
Example
CREATE TABLE Student
(Roll_No Number(4) PRIMARY KEY,
Name Varchar2(25) UNIQUE,
Class Char(8),
Marks Number(2) );
In this example, Roll_No column declares as the primary
key of the table student.
Therefore, in Roll_no column neither we can store Null
value nor two rows have the same value.
Foreign key constraint
A foreign key is a column (or an attribute) of a table that is
the primary key of another table. For example
EMP
Emp_No NAME DNO SALARY

1 A 10 12000
2 B 20 10000
3 A 10 10000

DEPT
DNO Dlocation
10 Karnal
20 Rohtak
DNO is a foreign key since DNO in Emp table is the primary
key in Dept table.
Example
CREATE TABLE dept
(DNO number(2) PRIMARY KEY,
Dlocation Varchar2(15));
CREATE TABLE emp
(Emp_No Number(4) PRIMARY KEY,
Name Varchar2(25),
DNO number(2) references dept(DNO),
Salary Number(6));
In this example, Both the tables (Emp and dept) are related
through common column DNO. The column DNO is
primary key in parent table dept and it is declared foreign
key in child table emp.
Unique constraint
The unique key allows unique values to be entered into the
column i.e. every value is a distinct value in that column.
Therefore, this constraint ensures that no two rows have
the same value in the specified column(s).
Example
CREATE TABLE Student
(Roll_No Number(4) UNIQUE,
Name Varchar2(25),
Class Char(8),
Marks Number(2) );
Student
Roll_No Name Class Marks
1001 Ram BBA - II 80
1003 Vansh BIM - III 70
1004 Sohan BBA - II 80

1004 Shivank BIM - III 80

Check Constraint
Check constraints allow Oracle to verify the validity of data
being entered on a table against a set of constant values.
The check constraint consists of the keyword CHECK
followed by parenthesized conditions.
Example
CREATE TABLE Student
(Roll_No Number(4) PRIMARY KEY,
Name Varchar2(25),
Class Char(8),
Marks Number(2) CHECK (Marks <101));
This statement ensures that the value inserted for Marks
column must be less than 101.
Student
Roll_No Name Class Marks
1001 Ram BBA -II 80
1003 Vansh BIM - III 70
1004 Sohan BBA - II 80
1005 Shivank BIM - III 102

Not Null Constraint


Syntax is
[Constraint <name>] NOT NULL
Example
CREATE TABLE Student
(Roll_no Number(4) NOT NULL,
Name Varchar2(25) CONSTRAINT SNAME NOT NULL,
Class Char (8),
Marks Number (2) );
Student
Roll_No Name Class Marks
1001 Ram BBA - II 80
1003 Vansh BIM - III 70
1004 Sohan BBA - II 80

Aditi BIM - III 90

12.Write SQL commands to demonstrate transaction


control using ‘ROLLBACK’, ‘COMMIT’, and
‘SAVEPOINT’.
Commit Command
It is the transactional command used to save changes
invoked by a transaction to the database.
Syntax
COMMIT [WORK];
Where COMMIT is the keyword and WORK is optional
keyword and it is used to make the commend more user
friendly.
Example
DELETE FROM EMP
WHERE BASIC>7000
COMMIT;
ROLLBACK Command
It is used to undo transactions that have not already been
saved to the data base.
SYNTAX
ROLLBACK [WORK];
Example
DELETE FROM EMP
WHERE BASIC < 4000;
ROLLBACK;
SAVEPOINT Command
The SAVEPOINT statement gives a name to and marks a
point in the processing of current transaction.
SYNTAX
SAVEPOINT <savepoint_name>;
Where
<savepoint_name> is an identifier and it is not to be
declared in the DECLARE section.
Example
Consider the following code:

BEGIN

UPDATE student

SAVEPOINT after_update;

DELETE FROM student

ROLLBACK TO SAVEPOINT after _ update

END;
13.Write SQL command to create a new database or
tablespace.
Syntax of the SQL CREATE DATABASE statement is
CREATE DATABASE DB_NAME;
Here, CREATE DATABASE command creates a database.
Example
The following SQL statement creates a database called
“testDB”:
CREATE DATABASE testDB;
Make sure you have admin privilege before creating any
database. Once a database is created, you can check it in
the list of databases with the following SQL command:
SHOW DATABASES;
• The CREATE TABLESPACE statement allows you to create
a new tablespace.
Syntax
Create_tablespace::=
The following illustrates how to create a new tablespace
named tbs1 with size 1mb:
CREATE TABLESPACE tbs1
DATAFILE ‘tbs1_data.dbf’
SIZE 1m;

In this statement:
• First, specify the name of the tablespace after the
CREATE TABLESPACE keywords. In this example, the
tablespace name is tbs1.
• Second, specify the path to the data file of the
tablespace in the DATAFILE clause. In this case, it is
tbs1.dbf. Note that you can use the datafile full
path.
• Third, specify the size of the tablespace in the SIZE
clause. In this example, 1m stands for 1MB, which
is quite small.
Once the tablespace is created, you can find its
information by querying data from the
dba_data_files view:
SELECT
tablespace_name,
file_name,
bytes / 1024/ 1024 MB
FROM
dba_data_files;
Here are all the tablespaces in the current database:

14.Write SQL commands to create a user and delete a


user.
Syntax :
Mysql> CREATE USER
‘username’ IDENTIFIED BY
‘password’;
• Username is a unique username given to the user profile
of the new user.
• Password is a Strong password given for a new user
profile.
Example:
Let's see one example of creating a new user in MySQL in
Windows Command Line using the above command.
Query:
mysql> CREATE USER 'sahil1' IDENTIFIED BY 'Sahil1@1717';
Output in MySQL Cmd Line:

Syntax :
DROP USER ‘user’@ ‘host’;
User : The user you want to drop.
• Using the DROP USER to delete a user in the current
database example
First, create a new login jin with a password:
CREATE LOGIN jin
WITH PASSWORD ='uJIKng12.';
• Second, create a new user and map it with the login
jin:
CREATE USER jin
FOR LOGIN jin;
• Third, drop the user jin from the current database:
DROP USER IF EXISTS jin;
15.Write SQL commands to grant and revoke roles.
GRANT Command
Privileges can be granted to other users through GRANT
command. Syntax is:
GRANT (privileges)
On objectname
To username
[WITH GRANT OPTION];
The WITH GRANT OPTION of GRANT command allows the
user (to whom this privilege has been granted) to in turn
grant this privilege to other users.
Example
Give all the privilege to user ‘shivank’ on table emp.
Command is
GRANT ALL
ON emp
To Shivank;
Example
Grant select, Insert and delete privileges to ‘Savita’ for
table emp. Command is
GRANT SELECT, INSERT, DELETE
ON emp
To Savita;
Revoke Command
Privileges once granted can be revoked later if needed. The
REVOKE command is used to do so. Syntax is
REVOKE (privileges)
ON objectname
FROM username;
Example
Revoke all the privileges from user Savita for table emp.
Command is
REVOKE ALL
ON emp
FROM Savita;
Example
All privileges has been granted to Shivank for emp table.
Revoke UPDATE, and INSERT privileges from him.
Command is
REVOKE UPDATE, INSERT
On emp
FROM Shivank;
TIKA RAM P.G. GIRLS COLLEGE
SONIPAT

PRACTICAL FILE OF DBMS ON SQL

SUBMITTED BY – BHAWNA
CLASS – M.SC. 1ST YEAR
ROLL NO. -
SUBMITTED TO –MRS. POOJA

You might also like