Beruflich Dokumente
Kultur Dokumente
1. SQL BASICS
PROGRAM TO DELETE
5.
RECORDS
6.
PROGRAM TO ALTER TABLE
PROGRAM TO UPDATE THE
7.
CONTENTS OF A TABLE
1
2
1. SQL BASICS
The Structured Query Language (SQL) is a language that enables you to create and
operate on relational databases, which are sets of related information stored in tables.
SELECT:- The select clause is used to list the attributes desired in the result of a query.
It corresponds to the projection operation of the relational algebra.
FROM:- The from clause lists the relation to be scanned in the evaluation of the
expression. It correspond to the Cartesian product operation of the relational algebra.
WHERE:- The where clause consists of a predicate involving attributes of the relations
that appear in the from clause.
Select A1,A2………,An
from r1,r2 .......... ,rm
where P
Here,
Each Ai represents to an attribute.
Each ri represents a relation.
P is the predicate.
3
2. PROGRAM TO CREATE TABLE
SYNTAX:
CREATE TABLE <TABLENAME>
(<ColumnName1> <DataType>(<size>), <ColumnName2> <DataType>(<size>));
Table created.
4
3. PROGRAM TO INSERT VALUES
SYNTAX:
METHOD1
METHOD2
METHOD3
5
METHOD-1
1 row created.
SQL> /
Enter value for clientno: C2
Enter value for name: MAMTA
Enter value for city: CHENNAI
Enter value for pincode: 780001
Enter value for state: TN
Enter value for balancedue: 0
1 row created.
OUTPUT:
6
METHOD-2
1 row created.
OUTPUT:
METHOD-3
1 row created.
OUTPUT:
7
4. PROGRAM TO VIEW DATA IN THE TABLES
SYNTAX:
SELECT * FROM TABLENAME;
SYNTAX:
SELECT COLUMNAME1, COLUMNAME2 FROM TABLENAME;
SYNTAX:
SELECT * FROM TABLENAME WHERE CONDITION;
SYNTAX:
SELECT COLUMNAME1, COLUMNAME2 FROM TABLENAME WHERE CONDITION;
8
A. ALL ROWS AND ALL COLUMNS
9
B. FILTERING TABLE DATA
OUTPUT
OUTPUT
OUTPUT
10
5. PROGRAM TO DELETE RECORDS
SYNTAX:
DELETE FROM TABLENAME;
SYNTAX:
DELETE FROM TABLENAME WHERE CONDITION;
TRUNCATING TABLES
SYNTAX:
TRUNCATE TABLE TABLENAME;
DESTROYING TABLES
SYNTAX:
11
REMOVAL OF ALL ROWS
SQL> DELETE FROM CLIENT_MASTER;
4 rows deleted.
OUTPUT:
no rows selected
// Output is no rows selected because all the four rows are deleted from table
CLIENT_MASTER. But data can be ROLLBACK //
ROLLBACK
SQL> ROLLBACK;
Rollback complete.
// TABLE IS DELETED //
TRUNCATING TABLES
12
REMOVAL OF SPECIFIC ROWS
1 row deleted.
OUTPUT:
2 rows deleted.
OUTPUT:
13
6. PROGRAM TO ALTER TABLE
SYNTAX:
ALTER TABLE TABLENAME
ADD ( NEWCOLUMNNAME DATATYPE(SIZE),
NEWCOLUMNNAME DATATYPE(SIZE));
SYNTAX:
ALTER TABLE TABLENAME
MODIFY (COLUMNNAME NEWDATATYPE(NEWSIZE));
SYNTAX:
ALTER TABLE TABLENAME
DROP COLUMN COLUMNAME:
SYNTAX:
ALTER TABLE TABLENAME
RENAME COLUMN OLDCOLNAME TO NEWCOLNAME;
14
ADDING NEW COLUMNS
Table altered.
OUTPUT:
// Two new Columns “dept”, “section” are added in the table CLIENT_MASTER //
15
MODIFYING EXISTING COLUMNS
Table altered.
OUTPUT:
//An ALTER TABLE MODIFY statement attempted to decrease the scale or precision of a numeric
column containing data. In order to decrease either of these values, the column must contain only NULL
values. An attempt to increase the scale without also increasing the precision will also cause this
message//
16
DROPPING A COLUMNS FROM THE TABLE
Table altered.
OUTPUT:
17
7. PROGRAM TO UPDATE THE CONTENTS OF A TABLE
SYNTAX:
UPDATE TABLENAME
SET COLUMNAME1 = ‘EXPRESSION1’, COLUMNAME2 =’EXPRESSION2’ ;
SYNTAX:
UPDATE TABLENAME
SET COLUMNAME1 = ‘EXPRESSION1’, COLUMNAME2 =’EXPRESSION2’
WHERE CONDITION;
18
UPDATING ALL ROWS
4 rows updated.
OUTPUT:
// All the rows are updated in the table CLIENT_MASTER with the value of column dept
as” IT “//
2 rows updated.
OUTPUT:
// Only specific rows are updated in the table CLIENT_MASTER where the condition
that is City is “mum” is satisfied.//
19
8. PROGRAM TO APPLY DATA CONSTRAINTS ON THE TABLE
SYNTAX
COLUMNAME DATATYPE(SIZE) PRIMARY KEY
SYNTAX
COLUMNAME DATATYPE(SIZE)
REFERENCES TABLENAME (COLUMNAME)
SYNTAX
COLUMNAME DATATYPE(SIZE) UNIQUE
NULL CONSTRAINTS
SYNTAX
COLUMNAME DATATYPE(SIZE) NOT NULL
CHECK CONSTRAINTS
SYNTAX
COLUMNAME DATATYPE(SIZE) CHECK(LOGICAL EXPRESSION)
20
CREATING TABLE WITH DATA CONSTRAINTS
Table created.
OUTPUT:
// PRIMARY KEY CONSTRAINT is added to the table “DEPT” and column “ DNUM”//
Table created.
OUTPUT
// FOREIGN KEY CONSTRAINT is added to the table “EMP” and column “ DNO”//
// PRIMARY KEY CONSTRAINT is added to the table “EMP” and column “ SSN”//
21
UNIQUE KEY CONSTRAINTS
Table created.
// PRIMARY KEY CONSTRAINT is added to the table “EMP” and column “ SSN”//
// FOREIGN KEY CONSTRAINT is added to the table “EMP” and column “ DNO”//
// UNIQUE KEY CONSTRAINT is added to the table “EMP” and column “ ADDRESS”//
NULL CONSTRAINTS
Table created.
// PRIMARY KEY CONSTRAINT is added to the table “EMP” and column “ SSN”//
// FOREIGN KEY CONSTRAINT is added to the table “EMP” and column “ DNO”//
// NULL CONSTRAINT is added to the table “EMP” and column “ ADDRESS”//
CHECK CONSTRAINTS
Table created.
// PRIMARY KEY CONSTRAINT is added to the table “EMP” and column “ SSN”//
// FOREIGN KEY CONSTRAINT is added to the table “EMP” and column “ DNO”//
// NULL CONSTRAINT is added to the table “EMP” and column “ ADDRESS”//
// CHECK CONSTRAINT is added to the table “EMP” and column “ SALARY”//
22
DEFINING DATA CONSTRAINTS VIA THE ALTER TABLE COMMAND
Table altered.
// PRIMARY KEY CONSTRAINT is added to the table “EMP” and column “ SSN”//
Table altered.
Table altered.
// FOREIGN KEY CONSTRAINT is added to the table “EMP” and column “ DNO”//
Table altered.
23
ADDING NOT NULL
Table altered.
Table altered.
Table altered.
24