Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
16 views6 pages

Familiarization of SQL

Uploaded by

Alvin Saji John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Familiarization of SQL

Uploaded by

Alvin Saji John
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3

EXPERIMENT NO. 1: FAMILARISATION OF SQL

SQL * Plus : It is a part of Oracle kernel package. It has two distinct parts – Interactive SQL
and PL/SQL.
Interactive SQL is used to create, access and maintain all data structures like tables, indexes etc. It
can also be used for interactive data manipulation.
PL/SQL can be used to create full fledged programs by users – for validation and manipulation of
data. It provides all facilities of a modern standard programming environment.

Data Types in SQL

The data types are used to specify the type of data that will be stored in each column of the table.
The following are the data types used in Oracle.
CHAR(W) -Values of this data type are fixed length character strings. It can
have a maximum of 255 Characters padded with space for
remaining length
VARCHAR2(W) - Variable length character strings not padded with space for
remaining length
NUMBER(W) - Stores numbers – fixed point and floating point upto 38 digits
precision.
NUMBER(P, S) – where P is precision and S is the scale. Thus NUMBER(7 , 2) is a
number that has 5 digits before and 2 digits after the decimal
point. The maximum precision is 38 digits.
DATE - The default format is DD-MON-YY (2digits for Day, 3 Characters for
Month and 2 digits for Year). Data entered in date format is
enclosed in single quotes. No length is specified for Date type
data.
RAW(W) - It is used for binary data in Hexadecimal format form – 0-9, A-F or
a – f. It accepts only Hex numbers. Maximum 255 (‘FF’) bytes. Data
is entered as a string and enclosed in single quotes. RAW(2) can
accept up to ‘FFFF’
LONG - Can store variable length character strings up to 2 GB.
(eg Content Long )

Data Definition Language (DDL) Commands (CREATE, ALTER, DROP, TRUNCATE)

These are commands used to create and maintain the data structure
ALTER SPACE - Changes A space definition
ALTER TABLE - To redefine existing columns in a table or to add
new Column
COMMENT - To insert comments table/column in data
dictionary
CREATE CLUSTER - which contains two or more tables.
CREATE INDEX - To create index for tables
CREATE SPACE - Space allocation properties for tables
CREATE SYNONYM - Synonym of a table or view name
CREATE TABLE - Creates a table and define its columns and other
4

properties
CREATE VIEW - View of one or more tables or views
CREATE INDEX - Creates an index for the table
DROP - Deletes cluster, table, view, index etc.
RENAME - Changes name of a table, view or Synonym
TRUNCATE - It is used to remove all rows from a table.
DDL commands are used to define and manage table(s).
- Define and create a new table.
- Remove a table that is no longer needed.
- Change the definition of an existing table.
- Define a virtual table (view) of data.
- Build an index to access a table faster.

Data Manipulation Language( DML ) Commands

DML commands are used for queries, to add new records, modify rows, or remove records
SELECT - Selects rows and columns from a table or view
INSERT - Adds new rows to a table or view
UPDATE - Changes values of fields in a table or view
DELETE - Deletes one or more rows from a table or view.

Data Control Language (DCL) Commands

These are used to control access to database and for transaction controls.
GRANT , REVOKE - For data access controls
COMMIT , ROLLBACK - For transaction controls

Creating a table

Table and column names should begin with an alphabet. It can have a maximum length of 30
characters. It should not contain quotes. It is not case sensitive. The name can contain characters a
– z , digits 0 to 9, underscore, $ or #. Reserved words can not be used as table or column names.
The columns or fields along with data type and size should be specified.
Format
CREATE TABLE < table name >
(
Column name1 data type(size),
Column name 2 data type(size),
……
…….
);
Comma is used to delimit columns and semi colon ( ; ) signifies the end of an SQL statement.
Constraints
These are rules to control data in a column. It is used to maintain the integrity of data. The
various constraints are
Primary Key : It can be specified for column(s) of a table. That column(s) should not have null
values. Same values should not repeat for all such columns.(that is it should be unique)
Not Null : This constraint specifies that the column should not have a null value.
Check : It ensures that data in a column is limited to specific values.
Table Constraint Definition
5

CONSTRAINT Constraint-name
- Primary-key-constraint…….
- Foreign-key-constraint…….
- Uniqueness-constraint……..
- Check-constraint………….
Primary-key-constraint
PRIMARY KEY(Column-name)
Foreign-key-constraint
FOREIGN KEY(Column-name) REFERENCES Table-name[Column-name]
Uniqueness-constraint
UNIQUE(Column-name)
Check-constraint
Check(Condition)

A column-level constraint follows a Column definition. Thus Nnull1 , Nnull2 etc are Column
level constraints.
A Table level constraint generally involves 2 or more columns. It follows the table definition. Thus
pkey1 and check1 constraints occur after defining all the columns of the table and are called table
level constraints..
NOT NULL constraint means that it is mandatory to provide a value for the column(s).
UNIQUE constraint on column(s) means that the values in the column(s) should be distinct,
although it can have NULL values.

A DBMS Primary key constraint implicitly imposes a NOT NULL and UNIQUE constraint. For a
Composite primary key , each of its attributes should be NOT NULL. The combination of
attributes constituting the primary key should offer a Unique value.
A Foreign key constraint on a set of attributes does not prevent them from having duplicate or null
values.

Changing the table structure


ALTER TABLE <table name >
MODIFY (Column [spec] [NULL| NOT NULL]
Column [spec] [NULL| NOT NULL ] ……);
Adding a column
ALTER TABLE <table name>
ADD (Column[spec] [NULL|NOT NULL] [, Column[spec] [NULL|NOT NULL] ….);
Adding a Primary key
ALTER TABLE <table name>
ADD CONSTRAINT Pkey1 PRIMARY KEY(colum name);
Adding a Foreign key
ALTER TABLE <table name>
ADD CONSTRAINT Fkey1 FOREIGN KEY(column name)
REFERENCES tablename(column name);
Adding a Check constraint
ALTER TABLE <table name>
ADD CONSTARINT Ccheque1
CHECK(<column name> between value1 and value2 );

Dropping a Column
ALTER TABLE <table name> or ALTER TABLE <table name>
DROP COLUMN <column name >; DROP(<column name>);
6

Dropping a primary key


ALTER TABLE <talble name >
DROP PRIMARY KEY;
or
ALTER TABLE <table name >
DROP <primary key constraint name>;
Modifying a Column definition
ALTER TABLE <table name>
MODIFY <column name> <datatype>;
Dropping a Table
DROP TABLE table-name
TRUNCATE
TRUNCATE TABLE table-name;
Will remove all rows from the specified table. But table structure is kept in tact.

To display structure of a table


DESCRIBE <table name >

Data manipulation Language commands

INSERT Command (for Inserting data )


INSERT INTO <table name>
VALUES(Value 1,Value 2,……….Value n);
Values should be specified in the same order as the columns in the table. Their data types should
match.
To add data interactively
INSERT INTO EMP VALUES(&EMPNO,’&NAME’,’&DOJ’);

SELECT Command (For Querying the database)


SELECT [ALL / DISTINCT] <column name 1, column name 2, …..>
FROM <table name >
[ WHERE condition]
[ GROUP BY grouping column ]
[HAVING group condition ]
[ORDER BY column list specification ]
There are six clauses in the query shown in bold. Only the first two (SELECT and FROM) are
compulsory.
SELECT EMPNO, NAME FROM EMP;
Here the WHERE clause is missing and it is called an Unspecified WHERE clause. Since no
condition is specified , it retrives all tuples from the table EMP.
When more than one relation is specified in the FROM clause, then it selects the Cartesian product
of all tuples in the specified relations.
To select all attributes , instead of specifying the attribute names explicitly , we can use the * (wild
card character)
SELECT * FROM <table name>; It selects all columns and all rows of the table.
To select tuples satisfying a condition , we can use the WHERE clause

1) SELECT <column name 1, column name 2, …..>


FROM <table name>
WHERE <condition > ;
7

To retrieve only tuples with distinct values , we can use


2) SELECT DISTINCT Column name 1 , Column name 2 , ……FROM <table name>
[WHERE condition ];

EXPLICIT SETS AND NULL VALUES


We can use explicit set of values in the WHERE clause. Such a set should be enclosed in parentheses.
It uses the IN or NOT IN predicates.
Example:
> SELECT EMPNO,NAME FROM EMP
WHERE NAME IN (‘RAJAN’,’JOS’,’LEELA’);
> SELECT NAME FROM EMP
WHERE DEPTNO NOT IN (10,20);
The BETWEEN predicate also can be used to specify limits.
> SELECT EMPNO,NAME FROM EMP WHERE SALARY BETWEEN 5000 AND 9000;

SQL allows queries to check whether a value is NULL or NOT NULL. But we should not use the =
or != while comparing an attribute value with NULL. Instead we should use
IS NULL or IS NOT NULL.

LIKE predicate

We can use this predicate to check for something about which we roughly know
something. It uses the symbol ‘%’ to match for any sub string and ‘_’ (underscore) to match for
one character. The patterns are case sensitive.
Suppose we want to retrieve all Supplier names starting with the character ‘R’, we can issue the
following statement.
>SELECT SUPNAME FROM SUPPLIERS
WHERE SUPNAME LIKE ‘R%’;
To retrieve all supplier numbers and names where all second characters are ‘R’ OR ‘H’
>SELECT SUPNO,SUPNAME FROM SUPPLIERS WHERE SUPNAME LIKE ‘_R%’ OR
SUPNAME LIKE ‘_H%’ ;
To retrieve names where names are 3 character long with first two characters as ‘Ja’
 SELECT SUPNAME FROM SUPPLIERS
WHERE SUPNAME LIKE ‘Ja_’;
To retrieve supplier names whose address contains M.G .Road in it
>SELECT SUPNAME FROM SUPPLIERS WHERE ADDRESS LIKE ‘%M.G.Road%’;

Dropping a table
To drop a table along with its contents , we use the DROP statement. The structure as well as the
data in the table is destroyed permanently.
DROP TABLE <table name >

Creating a table using SELECT


CREATE TABLE <new table name >
AS (SELECT colname1 , colname2, ……. FROM table name);
DELETE Command (For deleting the rows in a table)
We can delete rows from a table using this command
DELETE FROM <table name >
[WHERE <condition>] ;
The DELETE command will only remove records from the table. It will not destroy the structure of
the table.
8

UPDATE Command ( for changing existing data )


We can change the values in columns using the UPDATE command
UPDATE <table name>
SET Column name1 = expression 1 [, Column name2 = expression 2 , …..]
[WHERE condition] ;

Ordering tuples (ORDER BY Clause) (for sorting a table)

We can sort the tables using the above clause.


We can specify the words ASC for ascending order sort on the given field and DESC for
descending order sort on the given field. The default is ASC.
> SELECT REGNO, NAME , MARK FROM STUD ORDER BY MARK; or
> SELECT REGNO, NAME , MARK FROM STUD ORDER BY MARK ASC;
Will sort the table in ascending order of marks.
>SELECT REGNO, NAME , MARK FROM STUD ORDER BY MARK DESC;
Will sort the table in the descending order of marks,

Creating Sequences

Oracle provides an object called ‘Sequence’ that can generate numerical values (maximum 38
digits). A sequence can be defined to generate numbers in ascending / descending order, provide
interval between numbers , cache the sequence numbers in memory etc.
A sequence is an independent object and can be used with any table that needs its output.
CREATE SEQUENCE <sequence name >
[INCREMENT BY integer value
START WITH integer value
MAXVALUE integer value / NOMAXVALUE
MINVALUE integer value / NOMINVALUE
CYCLE/NOCYCLE
CACHE integer value/NOCACHE
ORDER/NOORDER]
 INCREMENT specifies interval between numbers. Should be a positive or negative value, but not
zero. If this clause is not specified, the default value for increment is taken as 1.
 START WITH specifies the starting value of the sequence. The default is 1.
 MAXVALUE specifies the maximum value of the sequence. NOMAXVALUE
assumes default values of 10 ^ 27 for ascending and –1 for descending sequence.
 MINVALUE specifies the minimum value for the sequence. NOMINVALUE
assumes a default value of 1 for ascending and –10 ^ 26 for descending
sequence.
 CYCLE is specified for repeated sequences.
 CACHE clause how many numbers are to be kept in the memory
 ORDER clause will generate in the order of request. The default is NOORDER.
Once a sequence is generated, SQL can be used to view the values held in the Cache.
To simply see the value we use
> SELECT sequence name.NEXTVAL FROM DUAL;
Or
>SELECT sequencename.CURRVAL FROM DUAL;
We can also use the sequence numbers generated for insertion into a table
>INSERT INTO ORDERS

You might also like