Modifying Table :
Modifying table is modifying the schema or contents of the table. The schema can be altered by using ALTER,UPDATE,DELETE command. Modifying involves,
ALTER:
ALTER command changes the schema of the tables.
1) Altering the data type or adding new constraints or changing names of
column ,
ALTER TABLE <table_name> CHANGE COLUMN <column_name>
<new_column_name> data_type constraints ;
For_example : If i want to change the car_id from largeint to int with
AUTO INCREMENT constraint we can do this by,
ALTER TABLE cars CHANGE COLUMN car_id car_id INT
PRIMARY_KEY AUTO INCREMENT;
2) We can also change the column name ,
ALTER TABLE cars ADD <column_name> AFTER/BEFORE
<column_name>
For_example : If we want to add car_color column after the car_id we
can add it using,
ALTER TABLE cars ADD car_color after car_id;
UPDATE:
Update table is used to change the values inside the table. This is done by selecting particular rows and changing the attributes in those rows.
UPDATE TABLE <table_name> set <column_name1>=value WHERE
<column_name1>=value1;
For_example : If we want to change the color of car having id=102 to
'Yellow'
UPDATE TABLE cars set car_color='Yellow' WHERE
car_id=102;
DELETE:
DELETE is used to delete either selected records from the table or the entire records in a table. The DELETE command can be used in either way as follows
DELETE FROM <table_name> where.....
The above mentioned is used to delete the selected records depending on the where clause.
DELETE * FROM <table_name>
The above mentioned will delete all the columns from the table.
TRUNCATE:
TRUNCATE is used to delete the entire contents from the table. The syntax is,
TRUNCATE TABLE <table_name>
TRUNCATE VS DELETE:
Applying DELETE for all records in a table and TRUNCATE may seem similar. They give the same output when they execute. But there is a difference between the both. DELETE logs before deleting each record. DELETE is costlier as it involves resources like locking and logging. But DELETE is more safe to apply than TRUNCATE.
DROP:
DROP differ from TRUNCATE and DELETE because DROP deletes the schema along with the entries. DROP is applied either to a column or a table. When the schema is deleted all the integrity constraints, access privileges will be deleted.
Lets quote two examples to differentiate between DROP and TRUNCATE.
eg.1: TRUNCATE TABLE CAR;
SELECT count(*) from car;
output :
count(*)
--------------
0
e.g.2: TRUNCATE TABLE CAR;
SELECT count(*) from car;
output :
ERROR at line 1: ORA-00942: table or view does not exist.
Modifying table is modifying the schema or contents of the table. The schema can be altered by using ALTER,UPDATE,DELETE command. Modifying involves,
ALTER:
ALTER command changes the schema of the tables.
1) Altering the data type or adding new constraints or changing names of
column ,
ALTER TABLE <table_name> CHANGE COLUMN <column_name>
<new_column_name> data_type constraints ;
For_example : If i want to change the car_id from largeint to int with
AUTO INCREMENT constraint we can do this by,
ALTER TABLE cars CHANGE COLUMN car_id car_id INT
PRIMARY_KEY AUTO INCREMENT;
2) We can also change the column name ,
ALTER TABLE cars ADD <column_name> AFTER/BEFORE
<column_name>
For_example : If we want to add car_color column after the car_id we
can add it using,
ALTER TABLE cars ADD car_color after car_id;
UPDATE:
Update table is used to change the values inside the table. This is done by selecting particular rows and changing the attributes in those rows.
UPDATE TABLE <table_name> set <column_name1>=value WHERE
<column_name1>=value1;
For_example : If we want to change the color of car having id=102 to
'Yellow'
UPDATE TABLE cars set car_color='Yellow' WHERE
car_id=102;
DELETE:
DELETE is used to delete either selected records from the table or the entire records in a table. The DELETE command can be used in either way as follows
DELETE FROM <table_name> where.....
The above mentioned is used to delete the selected records depending on the where clause.
DELETE * FROM <table_name>
The above mentioned will delete all the columns from the table.
TRUNCATE:
TRUNCATE is used to delete the entire contents from the table. The syntax is,
TRUNCATE TABLE <table_name>
TRUNCATE VS DELETE:
Applying DELETE for all records in a table and TRUNCATE may seem similar. They give the same output when they execute. But there is a difference between the both. DELETE logs before deleting each record. DELETE is costlier as it involves resources like locking and logging. But DELETE is more safe to apply than TRUNCATE.
DROP:
DROP differ from TRUNCATE and DELETE because DROP deletes the schema along with the entries. DROP is applied either to a column or a table. When the schema is deleted all the integrity constraints, access privileges will be deleted.
Lets quote two examples to differentiate between DROP and TRUNCATE.
eg.1: TRUNCATE TABLE CAR;
SELECT count(*) from car;
output :
count(*)
--------------
0
e.g.2: TRUNCATE TABLE CAR;
SELECT count(*) from car;
output :
ERROR at line 1: ORA-00942: table or view does not exist.
No comments:
Post a Comment