Sunday, 27 October 2013

SQL Command Types



As we've seen in the last discussion, the heart of any database lies in the query language. This is similar to the case that sql is the heart of database systems. There are three types of SQL Commands
  • Data Definition Language
  • Data Manipulation Language
  • Data Control Language
Data Definition Language:
This is what a database needs to be created: a basic set of commands used to create, and drop the data from the database. We use these commands with one flexibility, that they are auto saved commands. This is one we need to look at now:

There's a command called commit, in the Oracle's Sql list. This command saves the previously modified data. Since its all related to Data manipulation, every DML command must be accompanied by a commit command.


DDL command are auto saved in this sense: once they are executed, they are auto committed. Hence we need not explicitly save them.


The commands in the DDL class are:


CREATE

This command creates and entirely new table, we specify the attributes of the table along with the entity at this level. we also use the data types for each attribute.

Syntax:

                                  CREATE TABLE table_name (
                                              data_1 TYPE,
                                              data_2 TYPE,
                                              data_3 TYPE,
                                                        .
                                                        .
                                                        .
                                                        .
                                   ); 

Example: 


CREATE TABLE student (sno number(5), sname varchar2(10), course varchar2(5), avg number(5,2));       

One thing we have to observe here is that Oracle command line is dependent on the ';' used. Unless and until you put a ; all the thing written will be treated as a single statement only!

ALTER:

this command alters the things such as column names, types and new columns. we can use two commands in sync with this command, they are:
  • ADD
  • MODIFY
  • DROP

ADD command adds a new attribute to the table, this can be used to append new columns to the created table.

MODIFY command can be used to change the data type or the name of the column, in an already created table.

DROP command can be used to simply drop a column form the table. The data associated with it, will also be deleted.

syntax


ALTER TABLE table_name ADD data_1 type1;

ALTER TABLE table_name MODIFY data_name new_type; 

ALTER TABLE table_name DROP column_name;

DROP:

this command simply drops the entire table, it deletes all the data and associated structures, and the deletion is unchangeable. This means once dropped, we cannot bring back the data. 

Syntax
DROP TABLE table_name;

A Summary of the Data Definition Language commands is given below:



No comments:

Post a Comment