You are on page 1of 10

BASIC SQL COMMANDS

SQL STATEMENTS
There are a number of SQL statements, few of which are explained below: Data Retrieval Statement: SELECT is the data extracting statement which retrieves the data from the database. Data Manipulation Language (DML): This language constitutes the statements that are used to manipulate with the data. It has three commands, which are INSERT, UPDATE and DELETE. Data Definition Language (DDL): This is the language used to define the tables. It sets up, changes, and removes data structures from the tables. It uses 5 commands, which are CREATE, ALTER, DROP, RENAME and TRUNCATE. Transaction Control Language: Commit,Rollback, SavePoint

Data Control Language: Grant, Revoke

Hari Seetha,SCS,VIT University

CREATING A TABLE
Syntax: Create table <Table Name> ( <Field1> <Type> <(width)> , <Field2> <Type> <(width)>, ..................................) : Example: Create table student_table (Reg_No number(6) Name varchar2 (25), Class char(6));
Hari Seetha,SCS,VIT University 2

DATA TYPES
NUMBER(p,s) CHAR VARCHAR2 DATE LONG CLOB RAW and LONG RAW BLOB BFILE Variable length numeric data, pprecision,s-scale Fixed length character data Variable length character data Date and time values Variable length character data upto 2 Giga bytes Character data upto 4 gigabytes Raw Binary Data Binary data upto 4 giga bytes Binary data stored in an external file,upto 4 GB
Hari Seetha,SCS,VIT University 3

Naming Rules
Table names and Column names Must begin with a letter Must be 1-30 characters long Must contain only A-Z,a-z,0-9,_,$,# Must not duplicate the name of another object owned by the same user Must not be a reserved word
Hari Seetha,SCS,VIT University 4

To Add a field to the table (structure) Syntax: Alter Table <Table-Name> Add <Field Name> <Type> (width); Ex: Alter Table Student_Table Add Roll_no Char (4); To Modify a field of the table Syntax: Alter Table <tablename> MODIFY ( <column name > < newdatatype>); Ex: Alter table Student_Table Modify Roll_No Char(6); To Drop a field of the table Syntax: Alter Table <tablename> DROP COLUMN < column name>; Ex: Alter table Student_Table Drop Column Roll_No; To Delete a Table along with all contents Syntax: Drop Table <Table-Name>; Ex: Drop Table Student_table; To Rename a table Syntax: Rename <oldtablename> To <newtablename> Ex: Rename Student_table To stud_tab; To rename a column Syntax: ALTER TABLE <tablename> RENAME COLUMN <oldcolumnname> TO <newcolumn name> Ex: Alter Table Student_Table Rename Column Reg_No to Roll_No

Hari Seetha,SCS,VIT University

To Delete all rows from a table ,retaining its structure Syntax: Truncate Table <tablename> Ex: Truncate Table stud_tab;

DML
To Insert Data Into a Table
a. Inserting values from user Syntax: Insert Into <tablename> Values ( val1,val2 );

Ex:Insert into Student_Table Values (1211, "Umar", "MCS"); Syntax: Insert Into <Table-Name> (Fieldname1, Fieldname2,
Fieldname3,..) Values (value1, value2, value3,..);

Ex:Insert Into Student_Table (Name, class,Roll_No)Values (Umar, BIT,05BIT1);


b. Inserting interactively Syntax: Insert Into <tablename> Values( &<column name1> , &

<column name2> );

Hari Seetha,SCS,VIT University

To Delete rows from a table Syntax:Delete from <table name> [where <condition>]; Ex: a) Delete from student_table; b)Delete from student_table (conditional deletion) Where class=MCS; Modifying (Updating) Records: Syntax:UPDATE <table name> Set <Field Name> = <Value> Where <Condition>; Ex: UPDATE Student Set Semester = 5, Class = MS where Semester = 4 or Class = MCS Retrieving (Displaying) Data: Syntax:Select <field1, field2, fieldn> from <table name> where <condition>; Ex: a) SELECT *FROM Student_table; b) SELECT Reg_no, Name FROM Student_table; c)Select Reg_No from student_table where name=Krithi;
Hari Seetha,SCS,VIT University 7

using Alias name for a field


Syntax:SELECT <col1> <alias name 1> , <col2> < alias name 2> FROM < tab1>;

Ex: select Roll_No as Reg_No from Student _table; With distinct clause:
Syntax: SELECT DISTINCT <col2> FROM < tab1>;

Ex: Select Distinct Name from Student _table;

Hari Seetha,SCS,VIT University

Tables in Oracle Database


User_Tables: A collection of tables created and maintained by the user To see the tables created by the user Select table_name from user_tables; Data Dictionary: Is a collection of tables created and maintained by the Oracle Server. Select * from user_catalog;

Hari Seetha,SCS,VIT University

Creating Table using subquery Syntax:Create table <new _table_name> as Select <column names> from <old_table_name>; Ex: Create table Student_IT as Select * from Student_table where class=IT; To create a table with same structure as an exising table Syntax:Create table <new _table_name> as Select <column names> from <old_table_name> where 1=2; Ex: Create table Student_MCA as Select * from Student_table where 1=2; Inserting into table using a subquery: Syntax : Insert into <new_table_name>(Select <columnnames> from <old_table_name>); EX: Insert into Student_MCA (Select * from Student_table where class=MCA);

Hari Seetha,SCS,VIT University

10

You might also like