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

SQL Help

The document provides an overview of Data Definition Language (DDL) commands in Relational Database Management Systems (RDBMS), detailing commands such as CREATE, ALTER, DROP, and TRUNCATE, along with their syntax and examples. It also covers Data Manipulation Language (DML) commands, including INSERT, SELECT, UPDATE, and DELETE, explaining their usage and providing examples for each command. The document emphasizes the structure and rules for defining tables and manipulating data within a database.
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)
3 views6 pages

SQL Help

The document provides an overview of Data Definition Language (DDL) commands in Relational Database Management Systems (RDBMS), detailing commands such as CREATE, ALTER, DROP, and TRUNCATE, along with their syntax and examples. It also covers Data Manipulation Language (DML) commands, including INSERT, SELECT, UPDATE, and DELETE, explaining their usage and providing examples for each command. The document emphasizes the structure and rules for defining tables and manipulating data within a database.
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

1. Data Definition Language (DDL) commands in RDBMS.

It is used to communicate with database. DDL is used to:


 Create an object
 Alter the structure of an object
 To drop the object created.
The commands used are:
 Create
 Alter
 Drop
 Truncate

CREATE TABLE
The Create Table Command: - it defines each column of the table uniquely. Each has minimum of three
attributes, a name , data type and size.

Syntax:
CREATE TABLE <TABLE NAME> (<COL1> <DATATYPE>(<SIZE>),<COL2> <DATATYPE><SIZE>));

Example:
1. CREATE TABLE EMP(EMPNO NUMBER(4) PRIMARY KEY, ENAME CHAR(10));

2. CREATE TABLE PROG20 (PNAME VARCHAR2(20) NOT NULL, DOJ DATE NOT NULL,
DOB DATE NOT NULL, SEX VARCHAR(1) NOT NULL, PROF1 VARCHAR(20),
PROF2 VARCHAR(20),SALARY NUMBER(7,2) NOT NULL);

3. CREATE TABLE EMP ( EMPNO NUMBER(5), ENAME VARCHAR(15),


JOB CHAR(10) CONSTRAINT UNIK1 UNIQUE,
DEPTNO NUMBER(3) CONSTRAINT FKEY2 REFERENCES DEPT(DEPTNO));

Rules:
1. Oracle reserved words cannot be used.
3. Underscore, numerals, letters are allowed but not blank space.
3. Maximum length for the table name is 30 characters.
4. 2 different tables should not have same name.
5. We should specify a unique column name.
6. We should specify proper data type along with width.
7. We can include “not null” condition when needed. By default it is ‘null’.

ALTER TABLE
Alter command is used to:
1. Add a new column.
2. Modify the existing column definition.
3. To include or drop integrity constraint.

Syntax: alter table tablename add/modify (attribute datatype(size));

Example:
1. ALTER TABLE EMP ADD (PHONE_NO CHAR (20));
2. ALTER TABLE EMP MODIFY(PHONE_NO NUMBER (10));
3. ALTER TABLE EMP ADD CONSTRAINT Pkey1 PRIMARY KEY (EmpNo);

Modifying the structure of tables.


a) Add new columns
Syntax:
ALTER TABLE <TABLENAME> ADD(<NEWCOL> <DATATYPE(SIZE), <NEWCOL>DATATYPE(SIZE));
Example:ALTER TABLE EMP ADD(SAL NUMBER(7,2));

Dropping a column from a table.


Syntax: ALTER TABLE <TABLENAME> DROP COLUMN <COL>;
Example:ALTER TABLE EMP DROP COLUMN SAL;

Modifying existing columns.


Syntax: ALTER TABLE <TABLENAME> MODIFY(<COL><NEWDATATYPE>(<NEWSIZE>));
Example:ALTER TABLE EMP MODIFY(ENAME VARCHAR2(15));

RENAMING THE TABLES


Syntax: RENAME <OLDTABLE> TO <NEW TABLE>;
Example: RENAME EMP TO EMP1;

DROP TABLE
It will delete the table structure provided the table should be empty.
Syntax: DROP TABLE <TABLENAME>;
Example: DROP TABLE PROG20; //Here prog20 is table name

TRUNCATE TABLE
If there is no further use of records stored in a table and the structure has to be retained
then the records alone can be deleted.
Syntax: TRUNCATE TABLE <TABLE NAME>;
Example: TRUNCATE TABLE CUSTOMER;

DESC
This is used to view the structure of the table.
Syntax: DESC <TABLENAME>;
Example: DESC EMP;
NAME NULL? TYPE
EMPNO NOT NULL NUMBER(5)
ENAME NOT NULL VARCHAR(15)
JOB NOT NULL CHAR(10)
DEPTNO NOT NULL NUMBER(3)
PHONE_NO NUMBER (10)

DML COMMANDS

DML commands are the most frequently used SQL commands and is used to query and
manipulate the existing database objects. Some of the commands are :

 Insert
 Select
 Update
 Delete.

INSERT Command
Insert Command This is used to add one or more rows to a table. The values are separated by
commas and the data types char and date are enclosed in apostrophes. The values must be
entered in the same order as they are defined.

Example:
First create a table named STD
CREATE TABLE STD (SNO NUMBER(5),SNAME VARCHAR2(20), AGE NUMBER(5),
SDOB DATE,SM1 NUMBER(4,2),SM2 NUMBER(4,2),SM3 NUMBER(4,4));

To insert values into the STD table


Syntax:
INSERT INTO STD VALUES(101,‟AAA”,16,‟03-JUL-88”,80,90,98);

INSERT INTO STD VALUES(102,‟BBB”,18,‟04-AUG-89”,88,98,90);

SELECT Command
Select query is used to retrieve data from a tables. It is the most used SQL query. We can retrieve complete
tables, or partial by mentioning conditions using WHERE clause.

Syntax:

SELECT column-name1, column-name2, column-name3, column-nameN from table-name;


Example:
Consider the following Student table,

S_id S_Name age address


101 Adam 15 Noida
102 Alex 18 Delhi
103 Abhi 17 Rohtak
104 Ankit 22 Panipat

SELECT S_ID, S_NAME, AGE FROM STUDENT;

The above query will fetch information of S_ID, S_NAME and AGE column from Student table

S_ID S_NAME AGE


101 ADAM 15
102 ALEX 18
103 ABHI 17
104 ANKIT 22

Example to Select all Records from Table

A special character asterisk * is used to address all the data(belonging to all columns) in a query.

Select statement uses * character to retrieve all records from a table.

SELECT * FROM STUDENT;

The above query will show all the records of Student table that means it will show complete Student table
as result.

S_ID S_NAME AGE ADDRESS


101 ADAM 15 NOIDA
102 ALEX 18 DELHI
103 ABHI 17 ROHTAK
104 ANKIT 22 PANIPAT
Example to Select particular Record based on Condition
SELECT * FROM STUDENT WHERE S_NAME = 'ABHI';
103 ABHI 17 ROHTAK

Example to Perform Simple Calculations using Select Query

Conside the following Employee table.

EID NAME AGE SALARY


101 ADAM 26 5000
102 RICKY 42 8000
103 ABHI 22 10000
104 ROHAN 35 5000

SELECT EID, NAME, SALARY+3000 FROM EMPLOYEE;

The above command will display a new column in the result, showing 3000 added into existing salaries of
the employees.

EID NAME SALARY+3000


101 ADAM 8000
102 RICKY 11000
103 ABHI 13000
104 ROHAN 8000

UPDATE command

Update command is used to update a row of a table. A single column may be updated or more than
one column could be updated.

Following is its general Syntax,


UPDATE table-name set column-name = value where condition;

Example:
UPDATE STUDENT SET AGE=18 WHERE S_ID=102;
S_ID S_NAME AGE
101 ADAM 15
102 ALEX 18
103 CHRIS 14

Example to Update multiple columns


UPDATE STUDENT SET S_NAME='ABHI',AGE=17 WHERE S_ID=103;

The above command will update two columns of a record.

S_ID S_NAME AGE


101 ADAM 15
102 ALEX 18
103 ABHI 17

DELETE command
Delete command is used to delete data from a table. Delete command can also
be used with condition to delete a particular row. The delete command consists
of a from clause followed by anoptional where clause.

Following is
its general
syntax,
DELETE
from table-
name;

Example to Delete all Records from a Table


DELETE FROM STUDENT;
The above command will delete all the records from Student table.

Example to Delete a particular Record from a Table


Consider the following Student table

S_ID S_NAME AGE


101 ADAM 15
102 ALEX 18
103 ABHI 17

DELETE FROM STUDENT WHERE S_ID=103;


The above command will delete the record where s_id is 103 from Student table.
S_ID S_NAME AGE
101 ADAM 15
102 ALEX 18

You might also like