A1186950856 23641 31 2019 3 SQL Queries

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 20

SQL QUERIES-1

DDL-COMMANDS
CREATE
• SQL CREATE TABLE statement is used to create
table in a database.
• If you want to create a table, you should name the
table and define its column and each column's data
type.
• Let's see the simple syntax to create the table.
• create table table-name ( column-name1
datatype1, column-name2 datatype2, column-
name3 datatype3, column-name4 datatype4 );
• create table Student(id number(10), name
varchar(20), age number(4));
• You can verify it, if you have created the table
successfully by looking at the message displayed by
the SQL Server, else you can use DESC command as
follows:
• SQL> DESC STUDENTS;
• Create a table Employee having attributes as
id,name,department,age.
• SQL> CREATE TABLE STUDENTS ( ID number (10)
NOT NULL, NAME VARCHAR (20) NOT NULL, AGE
number(10) NOT NULL, ADDRESS CHAR (25) );
• It will get that no null value is taken;

Inserting Data into Table

• Though it is DML class just to start working with


table lets learn it.
• Insert command is used to insert data into a table.
Following is its general syntax,
• INSERT into table-name values(data1,data2,..);
• Eg:INSERT into Student values(101,'Adam',15);
2.Alter
• alter command is used for alteration of table
structures. There are various uses
of alter command, such as,
• to add a column to existing table
• to rename any existing column
• to change datatype of any column or to modify its
size.
• alter is also used to drop a column.
• To Add Column to existing Table
• Using alter command we can add a column to an
existing table. Following is the Syntax,
• alter table table-name add(column-name
datatype);
• Here is an Example for this,
• alter table Student add(address char);
• The above command will add a new
column address to the Student table
• To Add Multiple Column to existing Table
• Using alter command we can even add multiple
columns to an existing table. Syntax:
• alter table table-name add(column-name1
datatype1, column-name2 datatype2, column-
name3 datatype3);
• Here is an Example for this,
• alter table Student add(father-name varchar(60),
mother-name varchar(60), dob date);
• The above command will add three new columns to
the Student table
• To Add column with Default Value
• alter command can add a new column to an
existing table with default values. Following is the
Syntax,
• alter table table-name add(column-name1
datatype1 default data);
• Here is an Example for this,
• alter table Student add(dob date default '1-Jan-99');
The above command will add a new column with
default value to the Student table.
• To Modify an existing Column
• alter command is used to modify data type of an
existing column . Following is the Syntax,
• alter table table-name modify(column-name
datatype);Here is an Example for this,
• alter table Student modify(address varchar(30));
The above command will modify address column of
the Student table
• To Rename a column
• Using alter command you can rename an existing
column. Following is the Syntax,
• alter table table-name rename column old-column-
name to column-name;
• Here is an Example for this,
• alter table Student rename column address to
Location;
• The above command will rename address column
to Location.
• To Drop a Column
• alter command is also used to drop columns also.
Following is the Syntax,
• alter table table-name drop(column-name);
• Here is an Example for this,
• alter table Student drop(address);
• The above command will drop address column from
the Student table
Truncate command
• truncate command removes all records from a
table. But this command will not destroy the table's
structure. When we apply truncate command on a
table its Primary key is initialized. Following is its
Syntax,
• truncate table table-name
• truncate table Student;
• The above query will delete all the records
of Student table.
• truncate command is different
from delete command. delete command will delete
all the rows from a table whereas truncate
command re-initializes a table(like a newly created
table).
RENAME query
• rename command is used to rename a table.
Following is its Syntax,
• rename old-table-name to new-table-name;
• Here is an Example explaining it.
• rename Student to Student-record;
• The above query will rename Student table
to Student-record.
DROP command
• drop query completely removes a table from database. This
command will also destroy the table structure. Following is its
Syntax,
• drop table table-name;
• EG:drop table Student;
• The above query will delete the Student table completely.
• It can also be used on Databases. For Example, to drop a database,
• drop database Test;The above query will drop a database
named Test from the system.

PRACTICE

• create table ‘YOUR NAME’ with attributes


name,char,reg_no,section and phone_number.
• 1.Add two column in the existing table with
Address and Percentage.
• 2.Change the Reg_No name to ID.
• 3.delete the column address
• 4.rename the table.
• 5.drop the table.
MCQ

• In SQL, which command(s) is(are) used to change a


table's storage characteristics?
• A.ALTER TABLE
• B.MODIFY TABLE
• C.CHANGE TABLE
• D.All of the above
• In SQL, which of the following is a data definition
language commands?
• A.RENAME
• B.REVOKE
• C.GRANT
• D.UPDATE
• A command that lets you change one or more
fields in a record is
• A.Insert
• B.Modify
• C.Look-up
• D.All of the above

You might also like