Mysql1: database table operation, for example CRUD

Database tables related operations

Adding a database table

grammar:

  

 

 Example:

  

 

 

View database tables

grammar:

  show tables;

Example:

  

 

View the database table structure

1) using the DESCRIBE / DESC

  

 

 grammar:

   

Example:

  

 

   

 

 2)SHOW CREATE TABLE

 

grammar:

  

 Example:

  

 

   When you use this method to view the database table structure, not only can view the table to create a detailed statement of the time, but you can also view the storage engine and character encoding.

  

 Modify the table name

grammar:

  

    to an optional parameter, used or not shall not affect the

 Example:

  The user_info table instead user_data

  

 

 

Modifying the data type field

grammar:

   

 Example:

  The id data type by the varcahr (125) to varchar (11)

  

 

Modify field name

 

grammar:

  

 

    

 Example:

  The name was changed to the username user_data

    

  CHANGE also may modify only the data type, and MODIFY achieve the same effect, is a new field names and field names old set the same name, only change the data type. Because different types of data storage and the length is not the same as in the machine, modifying an existing data type can affect data recorded in the data table. So when the existing data in database tables, do not easily modify the data type.

 

Add field

 

Syntax :

  

  

 

  

 

 Example:

1) no added field integrity constraints

  Add user_data table without integrity constraints INT type field

   

 

2) adding an integrity constraint field

  Add a VARCHAR not empty in the user_data (12) type field

   

3) add a field in the first column of the table

  In the first column user_data field num INT add a field

  

 

 

4) add a field after a specified column

  User_data in the table, adding after the age field username

  

  

Delete field

grammar:

  

 

 Examples :

  Delete table user_data age field

  

  

Modified arrangement position of the field

grammar:

  

 

   Field 1: To modify the field position

  Data type: 1 refers to the field data type

  FIRST: an optional parameter field 1 refers to the table to modify the first field

  AFTER 2 fields: Field 1 is inserted into the back of the field 2

Example: 

1) modify the field for the first field in the table

  The user_data table id field to the first column

  

 

 2) to modify the field after a specified list of

  After changes to the username field location

  

 

Change table storage engine

  Storage engine when different technologies are used in the file or data is stored in memory. According to the needs, choose a different engine, can even choose different storage engines each table.

  show engines; view supported by the system storage engine.

grammar:

  

 

 

Delete the foreign key table

  Once you remove the foreign key, the primary table will be lifted and relationships between tables from  

grammar:  

 

 

Not delete the associated table

grammar:

  

 

   Table n: represents a plurality of tables can be deleted at the same time, the use of a comma between each other.

  If you delete the table does not exist, mysql will prompt an error message. Parameters IF EXISTS for judgment before deleting the deleted table exists, after adding the parameters, and then delete the table does not exist, it can be smoothly executed, but the warning will be issued a warning.

Example:

  

 

 

 Delete the associated table other tables

  In the presence of a foreign key between the data table, delete if the parent table is deleted failed due to damage to delete table referential integrity. If you want to delete, you can delete the child table, and then delete the parent table, but it needs two tables are deleted.

  Another case is the need to delete the parent table, the child table reservations, at this time you need to remove the foreign key table, and then delete the parent table alone.

 

      

Guess you like

Origin www.cnblogs.com/juankai/p/11608978.html