Data 6.MySQL data table to add, query, modify, delete

1. Prepare

. 1  the CREATE  DATABASE mahaiwuji;
 2  the USE mahaiwuji;
 . 3  the CREATE  TABLE Goods (
 . 4      ID the INT the COMMENT ' No ' ,
 . 5      name VARCHAR ( 32 ) the COMMENT ' product name ' ,
 . 6      . Price the INT the COMMENT ' price ' ,
 . 7      Description VARCHAR ( 255 ) the COMMENT ' product description ' 
. 8 ) ENGINE = INNODB DEFAULT CHARSET = utf8;

Prerequisite : By default, the data table is created in MySQL if not specify the character set, data table and field in a table will use the character set latin1.

Problem : the user inserts the data contained in Chinese, an error message appears.

2. Add data

1) add all the data fields

INSERT [INTO] table name {VALUES | VALUE} (value 1 [value 2] ...);

In strict accordance with structure of the table (field position) inserted into the corresponding value.

List value "value 1 [value 2] ..." comma-separated values ​​between a plurality.

. 1  the INSERT  the INTO Goods the VALUES ( . 1 , ' book ' , 25 , ' which is the book ' );

2) Add the specified field data

INSERT [INTO] Data table (field 1 [, field name 2] ...) {VALUES | VALUE} (value 1 [value 2] ...);

Field name must correspond to the data.

. 1  the INSERT  the INTO Goods (ID, name, Description) the VALUES ( 2 , ' keyboard ' , ' which is a keyboard ' );

3) add a plurality of data

When multiple data inserted, if a data insertion fails, the entire insert statement will fail.

. 1  the INSERT  the INTO Goods the VALUES  
2 ( . 3 , ' MySQL book ' , 25 , ' which is MySQL book ' ),
 . 3 ( 4 , ' JAVA book ' , NULL , ' which is JAVA book ' ),
 4 ( 5 , ' mouse ' , 50 , NULL );

3. query data

1) All data look-up table

SELECT * FROM data table;

The asterisk "*" wildcard instead of all the field names in the data table.

1 SELECT * FROM goods;

2) field data lookup table section

Field name SELECT {1, 2 field names, field names 3, ...} FROM table name;

1 SELECT name,price FROM goods;

3) simple conditional query data

SELECT * | {field names 1, 2 field names, field names 3, ...} FROM table name WHERE field name = value;

1 SELECT * FROM goods WHERE id=1;

1 SELECT name,price FROM goods WHERE id=1;

1 SELECT name,price FROM goods WHERE name='键盘';

4. Modify Data

SET UPDATE table name field name value 1 = 1 [, field name value of 2 = 2, ...] [the WHERE Conditional Expression];

There WHERE condition, modify the corresponding field to meet the requirements.

WHERE condition without modifying all the corresponding fields in the table. Therefore, when modifying data, please exercise caution.

1 UPDATE goods SET name = '新书' WHERE id=1;

1 UPDATE goods SET name = '',price=30 WHERE id=1;

. 1  the UPDATE Goods the SET name =  ' book ' ;

5. Delete Data

DELETE FROM table name [Conditional Expression the WHERE];

There WHERE condition, delete the condition of the recording

No WHERE condition, the system will automatically delete all records in the table, and therefore requires careful operation.

1 DELETE FROM goods WHERE id=1;

1 DELETE FROM goods;

Guess you like

Origin www.cnblogs.com/mahaiwuji/p/12589304.html