S. no.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
Name of practical
Database, DBMS definition
Introduction to SQL
Components of SQL, Data types
SQL Queries create, insertion
Create a table
Describe the table
Insert the values
To view the table
Sorting the data
To rename the table
Delete a particular row
Lower case
Upper case
Drop the table
Maximum
Minimum
Average
Like
Between
Primary key
Unique key
Count
Sum
Alter
Update
index
Remarks
2
DBMS & SQL queries
Database
A database is an organized collection of facts. In other words, we
can say that it is a collection of information arranged and
presented to serve an assigned purpose.
An example of a database is a dictionary
3
DBMS & SQL queries
4
DBMS & SQL queries
in a part of memory called the SQL buffer and remains there until
a new SQL statement is entered.
Components of SQL
1) DDL (Data Definition Language):- It is a set of SQL commands used
to create, modify and delete database structures but not data. They
are normally used by the DBA not by user to a limited extent, a
database designer or application developer. These statements are
immediate i.e. they are not susceptible to ROLLBACK commands. It
should also be noted that if several DML statements for example
UPDATES are executed then issuing any DDL command would COMMIT
all the updates as every DDL command implicitly issues a COMMIT
command to the database. Anybody using DDL must have the CREATE
object privilege and a table space area in which to create objects.
For example: - CREATE, ALTER, DROP, TRUNCATE, COMMENT etc.
5
DBMS & SQL queries
CHAR
VARCHAR (size) or VARChAR2 (size)
NUMBER
DATE
LONG.
CHAR: - This data types is used to store character strings
6
DBMS & SQL queries
number of characters (i.e. the size) this data type can hold is
255 characters. The data held is right- padded with spaces to
whatever length specified.
VARCHAR or VARCHAR2:- This data type is used to store
variable length alphanumeric data. It is a more flexible form
of the CHAR data type. The maximum this data type can hold
up to 4000 characters. One difference between this data type
and char data type is oracle compares varchar values using
non padded comparison semantics i.e. the inserted values
will not be padded with spaces. VARCHAR can hold 1 to 255
characters. Varchar is usually a wiser choice than char due to
its variable length format characteristics but keep in mind
that char is much faster than varchar sometimes up to 50%.
NUMBER: - The number data type is used to store numbers
(fixed or floating point).The precision (P) determines the
length of the data while(s), the scale, determines the number
of places after the decimal. The NUMBER data type that is
used to store number data can be specified either to store
integers or decimals with the addition of a parenthetical
precision indicator. If we do not use then the default value is 0
and if we dont use precision then by default value stored can
be of 38 digits.
DATE:- The DATE data type stores date and time information.
Although date and time information can be represented in
both character and number data types, the DATE data type
has special associated properties. For each DATE value, Oracle
stores the following information: century, year, month, date,
hour, minute, and second.
7
DBMS & SQL queries
The use of LONG values is subject to some restrictions:
You can declare a variable or argument of a PL/SQL program unit using the
LONG datatype. However, you cannot then call the program unit from SQL.
Within a single SQL statement, all LONG columns, updated tables, and locked
tables must be located on the same database.
QUERY
A query is a concise memo submitted to an editor by a writer seeking
publication. It is basically an in query to see whether the writers work is of
interest to a particular publication. A query briefly details a writers
experience and knowledge of the subject matter, and gives a summary or
synopsis of the article the writer hopes to have published. An approximate
word count for the proposed article or feature is also generally included.
1) THE CREATE TABLE COMMAND :- The CREATE TABLE command
defines each
Column of the table uniquely. Each
column has a minimum of three attributes, a name, data type and
size (i.e. column width).
Syntax: - CREATE TABLE<table name> (<column Name 1> <data type>
(<size>), <columnname2> <data type> (<size>));
Example:
SQL> create table student (name varchar (23),roll_no number(12),class
varchar2(12),address varchar(23));
Table created.
8
DBMS & SQL queries
1 row created.
Site svsu meerut |
9
DBMS & SQL queries
FOR inserting more values we use / slash after SQL> as below but after above syntax
used:
SQL> /
Enter value for name: Vinay
Enter value for roll_no: 08
Enter value for class: BCA
Enter value for address: Barnoti
old
1: insert into student
values('&name','&roll_no','&class','&address')
new
1:
1 row created.
3) FOR VIEWING DATA IN THE TABLE: - Once data has been inserted into
a
table, the next most logical
operation would be to view what has been inserted. The SELECT
SQL verb is used to achieve this. The SELECT command is used to
retrieve rows selected from one or more tables.
ADDRESS
---------Hatlimore
Nagri
airwan
barnoti
1 row created.
When we use the command SELECT* FRM TAB; the output is displayed as:SQL> select * from tab;
TNAME
-----------------------------ABC
ANKU
BONUS
TABTYPE CLUSTERID
------- ---------TABLE
TABLE
TABLE
10
DBMS & SQL queries
DEPARTMENTS
DEPT
EMP
EMPLOYEE
EMPLOYEES
STUDENT
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
9 rows selected.
ROLL_NO
---------06
viewed in
sorted order. The rows retrieve from
the table will be sorted either in ascending or descending order
depending on the condition specified in the select sentence.
Syntax: - SELECT * FROM <tablename>ORDER BY<column
name1>,<column name 2> <[sort order]>;
Example:SQL> SELECT * FROM STUDENT ORDER BY name;
NAME
ROLL_NO CLASS
----------------------- ---------- -----Amarjeet
30
BCA
Atinder
04
BCA
Dushyant
34
BCA
Kishore
01
BCA
Prabhat
06
BCA
Vinay
08
BCA
6 rows selected
Site svsu meerut |
ADDRESS
--------------airwan
sawanchak
jagatpur
Nagri
Hatlimore
barnoti
11
DBMS & SQL queries
08
06
01
34
04
30
BCA
BCA
BCA
BCA
BCA
BCA
barnoti
Hatlimore
Nagri
jagatpur
sawanchak
airwan
6 rows selected.
SQL> SELECT * FROM STUDEN ORDER BY roll_no desc;
NAME
ROLL_NO CLASS
ADDRESS
----------------------- ---------- ------------ --------------Kishore
Atinder
Prabhat
Vinay
Amarjeet
Dushyant
01
04
06
08
30
34
BCA
BCA
BCA
BCA
BCA
BCA
Nagri
sawanchak
Hatlimore
barnoti
airwan
jagatpur
6 rows selected.
12
DBMS & SQL queries
(c) MODIFYING EXISTING COLUMNS
Syntax: - ALTER TABLE<Table name>MODIFY(<COLUMN NAME> <NEW
DATATYPE>(<NEW SIZE>));
Example:- alter table prabhat modify(name varchar(22));
13
DBMS & SQL queries
DEPT_ID
DEPT_NAME
SALARY
NUMBER(10)
NAME(12)
NUMBER(21)
CONSTRAINTS
11) NOT NULL:- The NOT NULL column constraint ensures that a table
column cannot be left
empty. When a column is defined as not null, then that
column becomes a mandatory column. It implies that a value must be
entered into the column if the record is to be accepted for storage in the
table.
Syntax:- <Column Name> <data type>(<size>) NOT NULL ;
Example:-name varchar(22) not null;
14
DBMS & SQL queries
SQL> create table student name varchar2(12), roll_no
number(12) primary key, class varchar2(21) NOT NULL, dob
date);
or
SQL> create table student(name varchar2(12), roll_no
number(12) constraint pk_roll primary key ,class varchar2(21)
not null, dob date);
12) THE FOREIGN KEY (SELF REFERENCE) CONSTRAINT:-Foreign key
represent
relationships between tables. A foreign key is a column (or a group of
columns) whose values are derived from the primary key or unique key of
some other table. The table in which the foreign key is defined is called a
FOREIGN TABLE or DETAIL TABLE. The table that defines the primary or
unique key and is referenced by the foreign key is called the PRIMARY KEY
or MASTER KEY.
Syntax: - Foreign key (<column name>) REFERENCES <table
name>(column name);
Example:SQL> create table department(dept_no number(10) primary
key,dept_name varchar2(25),dept_loc char(5,e_no number(11),
foreign key(e_no) references employee (e_no);
Table created
SQL> describe department;
Name
-------------------------DEPT_NO
DEPT_NAME
DEPT_LOC
E_NO
Null?
-------NOT NULL
Type
------NUMBER(10)
VARCHAR2(25)
CHAR(5)
NUMBER(11).
13) THE UNIQUE KEY CONSTRAINT:- The unique key constraint permits
multiple
entries of NULL into the column. These NULL values
are clubbed at the top of the column in the order in which they were
entered into the table. This is the essential difference between the
primary key and the unique constraints when applied to table column(s).
Key point about UNIQUE constraint:
Unique key will not allow duplicate values.
Unique index is created automatically.
Site svsu meerut |
15
DBMS & SQL queries
A table can have more than one unique key which is not possible in
primary key.
ORACLE FUNCTIONS
NAME
DEPT_ID
SALARY
-------------------- ---------- ---------sourabh
21
55000
sonu
22
55000
anku
4
55000
anku
21
55000
panku
22
75000
16
DBMS & SQL queries
COUNT(DISTINCTNAME)
------------------4
SQL> select count(salary) from employees;
COUNT(SALARY)
---------5
15) COUNT (*) function: - Returns the number of rows in the table, including
duplicates
and those with nulls.
Syntax: - COUNT(*)
Example:SQL> select count(*) from employees;
COUNT(*)
---------5
SQL>
salary
---------5
17
DBMS & SQL queries
75000
18) THE MIN FUNCTION: - Returns the minimum value of expression.
Syntax: - MIN ([<distinct>][<all>] <expression>)
Example:SQL> select min (salary) from employees;
MIN(SALARY)
----------55000
20) LIKE OPREATOR :- The LIKE predicate allows comparison of one string
value with
another string value, which is not identical. This is
achieved by using wildcard characters. Two wildcard characters that are
available are:
% allows to match any string of any length(including zero length)
_allows to match on a single character.
Example:SQL> select emp_id,name,dept_id,salary from employees where
name like 'a%';
EMP_ID
NAME
-----------3
anku
5
anku
NOT LIKE OPERATOR:-
DEPT_ID
----------4
21
Example:-
SALARY
--------55000
55000
18
DBMS & SQL queries
SQL>select emp_id,name,dept_id,salary from employees where
name not like 'a%';
EMP_ID
--------1
2
3
NAME
--------------sourabh
sonu
panku
DEPT_ID
---------21
22
22
SALARY
---------55000
55000
75000
NAME
---------------anku
anku
DEPT_ID
SALARY
---------- ---------4
55000
21
55000
NAME
DEPT_ID
-------------------- ---------sonu
22
panku
22
SALARY
---------55000
75000
NAME
DEPT_ID
SALARY
--------
--------------------
----------
----------
21
4
21
55000
55000
55000
1
3
5
22)
sourabh
anku
anku
BETWEEN OPERATOR:-
19
DBMS & SQL queries
SQL> select emp_id,name,dept_id,salary from employees where
dept_id between 22 and 30;
EMP_ID
--------2
3
NAME
-------------------sonu
panku
DEPT_ID
---------22
22
SALARY
---------55000
75000
NAME
-------------------sourabh
anku
DEPT_ID
---------21
21
SALARY
---------55000
55000
STRING FUNCTIONS
23) UPPER function :- Returns char, with all letters forced to uppercase.
Syntax: - UPPER(char)
Example: SQL> select upper(name) from employees;
UPPER(NAME)
-------------------SOURABH
SONU
ANKU
ANKU
PANKU
20
DBMS & SQL queries
-------------------sourabh
sonu
anku
anku
panku
25) INITCAP function: - Returns a string with the first letter of each word
in upper case.
Syntax:- INITCAP(char)
Example:SQL> select initcap(name) from employees;
INITCAP(NAME)
-------------------Sourabh
Sonu
Anku
Anku
Panku