Uploaded by avreen87

SQL & Oracle Database Tutorial: Commands & Data Types

advertisement
INTRODUCTION
Structured Query Language (SQL) is the language used to manipulate relational databases. It
is simple and powerful language used to create, manipulate ant retrieve data and structures in
the database. SQL is a non-procedural language. It is a industry standard language fore
relational database management system such as Oracle, Microsoft SQL Server IBM DB2,
MYSQL. Today, SQL is accepted as the universal standard database access language.
ORACLE DATA TYPES:
Oracle tables are used for storing data which consists of both rows and columns. The
columns are used for storing an attribute’s data. Each column in a table has a column name
and a data type. .The following are the different data types.
CHAR (n):This data type is used for storing fixed length character strings. A character can
be any letter, number or a punctuation mark.
VARCHAR (n):This data type is used to store variable length character strings, but Oracle
recommends to use VARCHAR2(n) data type instead of VARCHAR(n) data type.
VARCHAR2(n): This data type is used to store variable length character string. When
declaring a column of data type VARCHAR2, you must specify its size. The VARCHAR2
(n) columns can have a maximum size of 4000 characters.
NCHAR(n): This data type is used to store fixed length national character set. This character
set enables developers and administrators to extend the standard database character set, in
order to store data in languages and character sets other than English.
NVARCHAR2(n): This datatype is used to store variable-length string in the database’s
national character set. When you create a table with NVARCHAR2 column, you define the
column length either in characters or in bytes.
LONG (n): This data type is used to store a large amount of variable length character strings.
It is mostly used to store long text strings. Long columns can be up to 2GB in size. A table
can neither contain more than one long column nor long column can appear in integrity
constraints.
RAW and LONGRAW datatypes: RAW and LONGERAW data type are used to store
binary data such as sound, graphics documents etc.
NUMBER (p,s) : The NUMBER data type have a precision (p) and a scale(s). The precision
(p) is the total number of digits in the number and can range from 1 to 38 digits.
CONNECTING TO SERVER
•
•
•
•
Open Run SLQ command line
Type connect and press enter.
Type username and press enter.
In next line type password and press enter.
CREATE TABLE
The CREATE TABLE statement is used to create table.
Syntax:
Create table table_name(column 1(datatype),column2(datatype)…);
INSERT STATEMENT
The INSERT statement is used to add new row or record in a table. To insert new row(s) into
a table, the table must be in your own schema or you must have INSERT privilege on the
table. Only one row is inserted at a time with this syntax. After adding values press enter
hence row is created
SYNTAX: INSERT INTO TABLE_NAME VALUES ([,column value2>]….[,]);
DESC COMMAND
DESC command is used to describe the structure of table. After writing desc table_name
press enter than the structure of table is described as shown below.
Syntax:
desc table_name;
COMMIT COMMAND
In oracle, any changes that you make to your data are not permanent until you tell oracle to
make them so. This is accomplished using a SQL statement known as COMMIT. A
COMMIT ends you transaction and makes the changes you have made permanent. Besides
this, whenever you exit the SQL * PLUS environment, it will automatically commit you
work.
The syntax is:COMMIT;
DELETE STATEMENT
The DELETE statement is used to remove the row(s) from the table. You don’t need to know
the physical ordering of rows in a table to perform a DELETE. Oracle database engine
determines the internal locations of rows. To delete a row from a table, it must be in your
own schema or you must have delete privilege on table.
SYNTAX:
DELETE FROM <TABLE_NAME>[WHERE CONDITION];
SELECET COMMAND
The SELECT command is used to retrive rows from the table. This statement is a
tremendously powerful tool and its syntax is bit complicated because of many ways that
table, columns, functions and operators can be combined into legal statements.
The SYNTAX is:
1) To retrieve all the columnsSELECT*FROM <TABLE_NAME>;
2) To retrieve selected columnsSELECT [COLUMN NAMES] FROM<TABLENAME>
3) To retrieve from selected row and selected columnSELECT(COLUMN NAME)FROM<table_name>[WHERE CONDITION];
ROLLBACK COMMAND
The rollback statement is used to undo all the changes that the user has made to the database
since the last COMMIT statement was issued or since the database session begin whichever
is later.
Syntax; SQL> ROLLBACK;
As in this example commit was done before deleting 3rd row so 3rd row is rolled back using
rollback; command.
ADD COLUMN (ALTER COMMAND)
We can alter the structure of the using ALTER TABLE statement, once the table is being
created. The ALTER TABLE statement helps the designer to make necessary changes to the
existing table. SYNTAX:ALTERTABLE <TABLE_NAME> ADD(column name);
UPDATE COMMAND
It is required to make changes or modification in the records of the table, so in order to make
these changes, the UPDATE statement is used. With this statement, the user can modify the
exiting data stored in the table. It can update zero or more rows in a table.
Syntax: UPDATE <table_name>SET<column_name>=[where condition];
MODIFY TABLE USING ALTER COMMAND
The ALTER TABLE command also offers he ability to modify columns in the existing table
without impacting the other data in the table.
The syntax is:
ALTER TABLE<TABLE_NAME>MODIFY(COLUMN SPECIFICATION/CONSTRAINT
SPECIFICATION…));
DROP COMMAND
We use the SQL DROP Table command to drop a tablefrom the database. It completely
removes the table structure and associated indexes, statistics, permissions, triggers and
constraints. You might have SQL Views and Stored procedures referencing to the SQL
Syntax DROP TABLE table_name;.
TRUNCATE COMMAND
TRUNCATE command is used to delete the entire values of table .
SYNTAX: TRUNCATE TABLE <TABLE_NAME>;
CONCATINATION OPERATOR
The concatenation operators are used to concatenate two or more strings. It is one of the most
important string operator in SQL. The concatenation operator is used to embed strings in the
values returned by the query, to combine strings and to create new values that can be
assigned to a column.
COMPARISON OPERATOR
The comparison operators are used to compare one expression with another. The result of
such a comparison can be TRUE, FALSE or UNKNOWN.
SYNTAX: SELECT *FROM <table_name> WHERE( comparison operator) condition;
1) (>) if (exp1>exp2)
2)(<) if(exp1<exp2)
3)(=) if(exp1=exp2)
4) (<=) if(exp1<=exp2)
5) (>=) if(exp1>=exp2)
6) (!=) if(exp1 is not equal to exp2)-
USE OF IN OPERATOR
The IN operator compares the values of a column or expression with a list of values within a
set. The IN comparison operator returns a Boolean value which is either TRUE or FALSE. If
the expression is equal to one of the values in the set, then it return TRUE otherwise FALSE.
SYNTAX:
SELECT*FROM <table_name> [WHERE CONDITION]IN(CONSTRAINTS…);
USE OF BETWEEN OPERATOR
The ‘BETWEEN’ operator is used to test whether the value is within the range or not. It is an
inclusive operator, as it includes the value within the range as well as boundary values. It
works with numeric, string and data values.
SYNTAX:
SELECT*FROM <table_name> [WHERE CONDITION] BETWEEN _ AND_;
USE OF LIKE OPERATOR
Like comparison operator is the pattern matching operator used to search through the rows of
a column for values that look like a pattern you describe. It is mostly used if you are not sure
of the exact spelling for the value, you are interested in finding . It uses two special characters
percent sign (%) known as wild card and a underscore (_) known as position maker in
combination with literal characters to search the value. 1. A percent sign ( %) in the patter
can match zero , one or more character. It cannot match a null. 2. An underscore (_) in the
pattern matches exactly one character in the value.
SYNTAX :
SELECT[CONDITION]FROM <TABLE_NAME> WHERE NAME LIKE ‘ A% ‘ ;
SELECT [CONDITION] FROM <TABLE_NAME> WHERE NAME LIKE ‘_ N _ H_’;
SELECT [CONDITION] FROM <TABLE_NAME> WHERE NAME NOT LIKE ‘A%’;
VIEW
A view is a query of one or more tables that provides another way of presenting information.
In other words, a view is a “stored query”. It can be used just as if it was a table. So a view
can be considered as a virtual table. Views do not actually store data rather they derive their
data from the tables on which they are based, referred to as the base tables of the views. Base
tables themselves may be tables or views. All operations performed on a view actually affect
the base tables of the view.
SYNTAX:CREATE [OR REPLACE] VIEW < view name>SELECT(column name) FROM
<table_name>;
DROPPING VIEW
A view can be dropped and then recreated. When you drop a view,all associated grants are
also dropped.
SYNTAX:
DROP VIEW <view name>;
Download