0% found this document useful (0 votes)
16 views

SQL Commands Notes

Uploaded by

tanvibehera10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

SQL Commands Notes

Uploaded by

tanvibehera10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

SQL Commands

o SQL commands are instructions. It is used to communicate with the database. It is


also used to perform specific tasks, functions, and queries of data.
o SQL can perform various tasks like create a table, add data to tables, drop the table,
modify the table, set permission for users.

Types of SQL Commands

Data Definition Language (DDL)


o DDL changes the structure of the table like creating a table, deleting a table, altering
a table, etc.
o All the command of DDL are auto-committed that means it permanently save all the
changes in the database.
Following are the some commands that come under DDL:

a. CREATE It is used to create a new table in the database.

Syntax:

1. CREATE TABLE TABLE_NAME (COLUMN_NAMES DATATYPES [ ...]);

In above statement, TABLE_NAME is the name of the table, COLUMN_NAMES is the


name of the columns and DATATYPES is used to define the type of data.

Example:

1. CREATE TABLE EMPLOYEE(Name VARCHAR2(20), Email VARCHAR2(100), DOB DATE);

b. DROP: It is used to delete both the structure and record stored in the table.

Syntax: To DROP a table permanently from memory

Example

1. DROP TABLE EMPLOYEE;

c. ALTER: It is used to alter the structure of the database. This change could be either
to modify the characteristics of an existing attribute or probably to add a new
attribute.

Following are the list of modifications that can be done using ALTER command.

o With the use of ALTER commands we can add or drop one or more columns
form existing tables.
o Increase or decrease the existing column width by changing the data type
o Make an existing mandatory column to optional.
o Enable or disable the integrity constraints in a table. We can also add, modify
or delete the integrity constraints from a table.
o We can also specify a default value for existing column in a table.

Adding new columns in Table:

With the use of ALTER table command we can add new columns existing table.

Syntax: To add a new column in the table

1. ALTER TABLE table_name ADD column_name column-definition;

EXAMPLE:

1. ALTER TABLE STU_DETAILS ADD (ADHAR_NUM VARCHAR2 (15));

Syntax: To ADD a multiple column from a table.

ALTER TABLE table_name ADD column_name1, column_name2;

Example:

ALTER TABLE STU_DETAILS ADD ADHAR_NUM, NAME;

Modifying Column using ALTER:

With the use of ALTER table we can modify column and constraint in the existing
table. These statements can increase or decrease the column widths and changing a
column from mandatory to optional.

Syntax:

1. ALTER TABLE table_name MODIFY (column definitions....);

Example:

1. ALTER TABLE STU_DETAILS MODIFY (ADHAR_NUM VARCHAR2 (20));

Drop column and constraints using ALTER


You cannot only modify columns but you can also drop them entirely if it is no
longer required in a table. Using drop statement in alter command we can also
remove the constraints form the table.

Syntax: To drop a column from a table.

1. ALTER TABLE table_name DROP COLUMN column_name;

Example:

ADVERTISEMENT
ADVERTISEMENT

1. ALTER TABLE STU_DETAILS DROP COLUMN ADHAR_NUM;

Syntax: To drop a multiple column from a table.

1. ALTER TABLE table_name DROP COLUMN column_name1, column_name2;

Example:

1. ALTER TABLE STU_DETAILS DROP COLUMN ADHAR_NUM, NAME;

RENAMING TABLE

SQL provides the facility to change the name of the table by using a ALTER TABLE
statement.

Syntax:

1. ALTER TABLE <OLD_TABLENAME> Rename to <NEW_TABLENAME>;

Example:

1. ALTER TABLE STU_NAME Rename to STUDENT_NAME;

d. TRUNCATE: It is used to delete all the rows from the table and free the space
containing the table.

Syntax:

1. TRUNCATE TABLE table_name;

Example:
1. TRUNCATE TABLE EMPLOYEE;

e. Rename: It is used to rename the table.

Syntax:

1. Rename <OLD_TABLENAME> to <NEW_TABLENAME>

2. Data Manipulation Language


o DML commands are used to modify the database. It is responsible for all form of
changes in the database.
o The command of DML is not auto-committed that means it can't permanently save all
the changes in the database. They can be rollback.

Following are the some commands that come under DML:

a. INSERT: The INSERT statement is a SQL query. It is used to insert data into the row
of a table. To insert a new row into a table you must be your on schema or INSERT
privilege on the table.

Following are the list of points should be considered while inserting the data
into tables.

o SQL uses all the columns by default if you do not specify the column name
while inserting a row.
o The number of columns in the list of column name must match the number of
values that appear in parenthesis after the word "values".
o The data type for a column and its corresponding value must match.

Syntax: To add row in a table


1. INSERT INTO TABLE_NAME
2. (col1, col2, col3,.... col N)
3. VALUES (value1, value2, value3, .... valueN);

or

4. INSERT INTO TABLE_NAME


5. VALUES (value1, value2, value3, .... valueN);

Syntax: To add multiple rows in a table

1. INSERT INTO TABLE_NAME


2. (col1, col2, col3,.... col N)
3. VALUES (value1, value2, value3, .... valueN), (value1, value2, value3, .... valueN);

For example:

1. INSERT INTO javatpoint (Author, Subject) VALUES ("Sonoo", "DBMS"), ("Raman", "DB
MS"), ("Priya", "DBMS");

b. UPDATE: This command is used to update or modify the value of a column in the
table.

Syntax: To update record in a table

1. UPDATE table_name SET [column_name1= value1,...column_nameN = valueN] [WHER


E CONDITION]

In the above syntax, table_name is the name of the table, the column_name is the
name of column in the table to be modified, and value1 corresponds to the valid SQL
values. The "WHERE" is a condition that restricts the rows updated for which the
specified condition is true. If condition is not specified is not defined then SQL
updates all the rows in the table. It contains comparison and logical operators etc.

The following the list of points should be remembered while executing the
UPDATE statement.

o It references only one table.


o In the SET clause atleast one column must be assigned an expression for the
update statement,
o In the where clause you could also give multiple conditions for update
statement.
For example:

1. UPDATE students
2. SET User_Name = 'Sonoo'
3. WHERE Student_Id = '3'

c. DELETE: It is used to remove one or more row from a table. To delete rows from
the table, it must be in your schema or you must have delete privilege.

Syntax: To Delete a record from table

1. DELETE FROM table_name [WHERE condition];

DQL

• The SELECT statement is used to select data from a database.

Syntax

select column_name from table_name;

• To SELECT all the fields available in the table

Syntax

select * from table_name;

The MySQL SELECT DISTINCT Statement


• The SELECT DISTINCT statement is used to return only distinct (different) values.

• Inside a table, a column often contains many duplicate values; and sometimes you only want
to list the different (distinct) values.

SELECT DISTINCT Syntax

SELECT DISTINCT column_name


FROM table_name;

Where clause
• The where clause is used to filter records.

• It is used to extract only those records that fulfill a specified condition

Syntax

Select column_name from table_name where conditions;


Example

select name from customer where id=1 and address="bbsr";

Limit clause
• The limit clause is used to set an upper limit on the number of tuples returned by sql.

Syntax

Select column_name from table_name

limit5;

Example

Select * from customer limit 3;

ORDER BY CLAUSE
• The ORDER BY is used to sort the result-set

In ascending(ASC) or descending (DESC)

Example

Select * from customer ORDER BY id asc;

Select * from customer ORDER BY id desc;

You might also like