The alter table command can be used to change many design details of a data table, such as adding or deleting data columns, changing the attributes of Data columns, defining and deleting various indexes.
1. Add a data column
ALTER TABLE Tblname
Add Newcolname coltype coloptions
2. modify a data column
ALTER TABLE Tblname
Change Oldcolname newcolname coltype coloptions
For example, alter table Table1 change ID auto_increment indicates that the column is not renamed or can be renamed.
3. delete a data column
ALTER TABLE Tblname
Drop Colname
4. Add an index Alter Table Tblname Add Primary Key (Indexcols)
Alter Table Tblname Add Index [ Indexname ] (Indexcols)
Alter Table Tblname Add Unique [ Indexname ] (Indexcols)
5. Add a foreign key constraint Alter TableTblnameAdd Foreign Key [Indexname](Column1)ReferencesTable2 (column2)
6. delete an index Alter Table Tblname Drop Primary Key
Alter Table Tblname Drop Index Indexname
Alter Table Tblname Drop Foreign Key Indexname
the update command is used to modify existing data records in the database
1. Update Statement defined by where
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Update tablename
set column1 = value1, column2 = value2
where columnn = value
2. Update without the where restriction modifies the entire data table.
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Update titles set year = 2005
Update titles set price = price * 1.05
3. Edit the data records in the sorting list
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Update tablename set mydata = 0 order by name limit 10
4. Update data records in the associated data table UpdateTable1, Table2
SetTable1.columna=Table2.columnb
WhereTable1.table1id=Table2.table1id
The INSERT command inserts data into a table.
1. Insert multiple data records with one command Insert Into Table (Columna columnb columnc)
Values ( ' A ' , 1 , 2 ),( ' B ' , 12 , 13 ),( ' C ' , 22 , 33 ),
The DELETE command is used to delete records in a table.
Delete from titles where titleid = 8 // because deletion is definitely to delete a row of records, you do not need to add *
1. Delete association records
code highlighting produced by actipro codehighlighter (freeware)
http://www.CodeHighlighter.com/
--> Delete T1, T2 from T1, T2, T3 where condition1 and condition2
2. Output data records in the sorting list Delete FromAuthorsOrder ByTSDescLimit1
The show command is used to view the original data.
Show Databases
Show tables From Dbname
Show [ Full ] Columns From Tablename // Returns the details of all data columns.
Show Index From Tablename