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

Alter Command

The Alter command is used to modify database objects and their structures. It allows modifying table names, adding or dropping columns, and adding or dropping primary key and foreign key constraints. Some key uses of Alter include renaming tables and columns, adding or modifying columns, adding or dropping primary keys and foreign keys, and modifying column data types.

Uploaded by

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

Alter Command

The Alter command is used to modify database objects and their structures. It allows modifying table names, adding or dropping columns, and adding or dropping primary key and foreign key constraints. Some key uses of Alter include renaming tables and columns, adding or modifying columns, adding or dropping primary keys and foreign keys, and modifying column data types.

Uploaded by

samuel noah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Using of SQL Alter command

Alter command is used to modify the database objects or to modify the structure of the database
objects like modifying name of table, adding /dropping columns from table, adding/droppingprimary key
and foreign key constraints and other various constraints from table

1. To rename the database table name


Alter table tableName rename to newTableName;
2. To rename column name of the database table
Alter table tableName change ocldColumnNamenewcolumnName datatype;
3. To add new columns into the existing or already created database table
Alter table tableName add columnNamecolumn Definition first/after columnName, add
columnNamecloumnDefination
4. To change the data type of column
Alter table tableName modify column columnName datatype
5. To drop column from the database table
Alter table tablName drop columnName;
6. To drop foreign key constraint
Alter table tableName drop foreign key foreignKeyConstraint
7. To find foreign key constraint
Show create tabletableName
8. To drop primary key from the database table first you have to remove the extra constraints like
auto_increment constraints.
Alter table tableName modify columnName datatype not null;
9. To drop primary key from the database table
Alter table tableName drop primary key;
10. To add primary key to the table once it is created and to make it auto_increment
Alter table tableNamemodify columnName datatype not null primary key auto_increment;
11. Toadd foreign key constraints once the table is created
Alter table tableName add foreign key(cloumnName) references primarykeytable(columnName)
on delete action on update action;
 The ON DELETE clause allows you to define what happens to the records in the
child table when the records in the parent table are deleted. If you omit the ON
DELETE clause and delete a record in the parent table that has records in the
child table refer to, MySQL will reject the deletion. In addition, MySQL also
provides you with actions so that you can have other options such as ON
DELETE CASCADE that ask MySQL to delete records in the child table that
refers to a record in the parent table when the record in the parent table is
deleted. If you don’t want the related records in the child table to be deleted, you
use the ON DELETE SET NULL action instead. MySQL will set the foreign key
column values in the child table to NULL when the record in the parent table is
deleted, with a condition that the foreign key column in the child table must
accept NULL values.
 The ON UPDATE clause enables you to specify what happens to the rows in the
child table when rows in the parent table are updated. You can omit the ON
UPDATE clause to let MySQL reject any updates to the rows in the child table
when the rows in the parent table are updated. The ON UPDATE
CASCADE action allows you to perform a cross-table update, and the ON UPDATE
SET NULLaction resets the values in the rows in the child table to NULL values
when the rows in the parent table are updated. The ON UPDATE NO
ACTION or UPDATE RESTRICT actions reject any updates.
12. To insert data into a table
 To insert row of data values in all coloumn
“insert into tableName values( ‘value1’,‘value2’, ‘value3’,…..‘valuen’ )” the order of
value must be the same as the order of the columns we defined.
 To insert row data values in specific column
“insert into tableName (colName1,colName2….colNamen) values( value1,value2…
valuen);”
 To insert values in all rows
“insert into tableName values(value1,value2,….valuen),(value1,value2….valuen);”
 To insert values in all row in a specific column
Insert into tableName (list of column) values(value1, value2….valuen),(value1,value2,
….valuen);
NB: put null in the auto_increment columns
Missing of value in auto_increment result in maximum_value+1

13. To drop table


 Drop table tableName
14. To drop database
 Drop database database_name
15. Retrieving all values Syntax:
 SELECT * FROM table_name;
16. Retrieving specific values Syntax:
 SELECT column 1, column 2, … , column n FROM table_name;
17. Retrieving all values using where condition Syntax:
 SELECT * FROM table_name WHERE condition;
18. Retrieving specific values using where condition Syntax:
 SELECT column 1, column 2,…, column n FROM table_name WHERE condition;
1. NB : you can specify a condition using comparison like >,<,=…. () or logical operators (or &
and)
19. To modify values of a single cell value
 Update tableName set colName=newValue where criteria.
20. To update value of column
 Update tableName set columnName=values;
21. To modify more than one column value
 Update tablName set columnName=value,columnName=value where criteria;
22. To delete row of data from the table
 Delete from tableName where criteria
23. To delete all row of data from the database table
 Delete from tableName;

You might also like