Some important mysql statement usage

xiaoxiao2021-04-09  374

Add a field (one column)

ALTER TABLE TABLE_NAME ADD Column Column_name Type Default Value; Type refers to the type of this field, the value refers to the default value of the field.

For example: ALTER TABLE MYBOOK ADD Column Publish_house varchar (10) default ';

2. Change a field name (you can also change the type and default)

ALTER TABLE TABLE_NAME CHANGE SORCE_COL_NAME DEST_COL_NAME TYPE DEFAULT VALUE; source_col_name Refers the original field name, Dest_col_name Remifes the name of the field name

For example: Alter Table Board_info Change Ismobile IstelPhone Int (3) Unsigned Default 1;

3. Change the default value of a field

Alter Table Table_name alter column_name set default value;

For example: ALTER TABLE BOOK ALTER FLAG SET DEFAULT '0';

4. Change the data type of a field

Alter Table Table_name Change Column Column_Name Column_name Type

For example: ALTER TABLE Userinfo Change Column Username Username Varchar (20);

5. Add a column to a table as a primary key

Alter Table Table_name Add Column Column_name Type Auto_Increment Primary Key;

For example: ALTER TABLE BOOK ADD Column ID Int (10) Auto_Increment Primary Key;

6. The backup of the database, entered in the command line:

Mysqldump -u root -p Database_name Table_name> Bak_File_Name

For example: mysqldump -u root -p f_info user_info> user_info.dat

7. Export data

SELECT_STAMENT INTO OUTFILE "dest_file";

For example: Select Cooperatecode, CreateTime from Publish Limit 10 INTO OUTFILE "/HOME / MZC/Temp/tempbad.txt";

8. Import data

Load data infile "file_name" inTo Table Table_name

For example: load data infile "/home/mzc/temp/tempbad.txt" INTO TABLE PAD;

9. Insert the data in both tables to another table. The following example describes that the value of the COM2 in the T1 table is spliced ​​into the field corresponding to the TX table after splicing the value of the COM1 field in the T2 table.

For example: INSERT INTO TX SELECT T1.COM1, Concat (T1.COM2, T2.COM1) from T1, T2;

转载请注明原文地址:https://www.9cbs.com/read-133148.html

New Post(0)