Types of SQL Commands
Types of SQL Commands
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:
Example
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.
Example:
You can also add constraints to an existing table. For example: If you forget to
add a primary key constraint to a table during creation, you can add it using
the ALTER TABLE statement.
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:
Example:
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.
o You cannot drop columns in a table. If you want to drop a column from
a table, the deletion is permanent so you cannot undo the column if you
accidentally drop the wrong column.
o You cannot drop a column whose username is SYS.
o If you want to drop a primary key column unless you drop the foreign
keys that belong to it then use cascade keyword for this.
Example:
o You can also enable or disable the key constraint in a table. It can be
done in various situations such as: when loading large amount of data
into table, performing batch operations, migrating the organizations
legacy data.
Example: To disable constraint
Syntax:
Syntax:
Syntax:
Example:
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
The following the list of points should be remembered while executing the
UPDATE statement.
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.
For example:
1. SELECT expressions
2. FROM TABLES
3. WHERE conditions;
For example:
1. SELECT emp_name
2. FROM employee
3. WHERE age > 20;
Example
Syntax:
Example
These operations are automatically committed in the database that's why they
cannot be used while creating tables or dropping them.
1. COMMIT;
Example:
1. ROLLBACK;
Example:
Syntax:
1. SAVEPOINT SAVEPOINT_NAME;
In the above syntax, SAVEPOINT_NAME is the name given to savepoint.
1. Rollback TO <save_point_name>
Example: