Usage of alter, update, insert, delete, and show in MySQL

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.