SQL
SQL
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
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.
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
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
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
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:
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
SUBMITTED BY – BHAWNA
CLASS – M.SC. 1ST YEAR
ROLL NO. -
SUBMITTED TO –MRS. POOJA