« MySQL/Manipulation de données » : différence entre les versions

Contenu supprimé Contenu ajouté
Page créée avec « {{MySQL}} == INSERT == The syntax is as follows: Insert value1 into Column1, value2 into Column2, and value3 into Column3: INSERT INTO TableName (Column1, Column2, Colum... »
 
Aucun résumé des modifications
Ligne 4 :
 
Insert value1 into Column1, value2 into Column2, and value3 into Column3:
<source lang=sql>
INSERT INTO TableName (Column1, Column2, Column3)
VALUES (value1, value2, value3)
</source>
Insert one record (values are inserted in the order that the columns appear in the database):
<source lang=sql>
INSERT INTO TableName
VALUES (value1, value2, value3)
</source>
Insert two records:
<source lang=sql>
INSERT INTO TableName
VALUES (value1, value2, value3), (value4, value5, value6)
Ligne 15 ⟶ 20 :
INSERT INTO antiques VALUES (21, 01, 'Ottoman', 200.00);
INSERT INTO antiques (buyerid, sellerid, item) VALUES (01, 21, 'Ottoman');
</source>
 
You can also insert records 'selected' from other table.
<source lang=sql>
 
INSERT INTO table1(field1, field2)
SELECT field1, field2
Ligne 23 ⟶ 28 :
 
INSERT INTO World_Events SELECT * FROM National_Events
</source>
 
'''Performance tips:'''
 
Ligne 33 ⟶ 38 :
== UPDATE ==
The syntax is:
<source lang=sql>
 
UPDATE table SET field = newvalue WHERE criteria ORDER BY field LIMIT n
</source>
 
Examples are:
<source lang=sql>
 
UPDATE owner SET ownerfirstname = 'John'
WHERE ownerid = (SELECT buyerid FROM antiques WHERE item = 'Bookcase');
Ligne 60 ⟶ 65 :
UPDATE posts SET deleted=True
ORDER BY date LIMIT 1
</source>
 
With ORDER BY you can order the rows before updating them, and only update a given number of rows (LIMIT).
 
Ligne 84 ⟶ 89 :
 
== DELETE and TRUNCATE ==
<source lang=sql>
DELETE [QUICK] FROM `table1`
TRUNCATE [TABLE] `table1`
</source>
 
* If you don't use a WHERE clause with DELETE, all records will be deleted.
* It can be very slow in a large table, especially if the table has many indexes.
Ligne 96 ⟶ 102 :
* After DELETing many rows (about 30%), an OPTIMIZE TABLE command should make next statements faster.
* For a InnoDB table with FOREIGN KEYs constraints, TRUNCATE behaves like DELETE.
<source lang=sql>
 
DELETE FROM `antiques`
WHERE item = 'Ottoman'
ORDER BY `id`
LIMIT 1
</source>
 
You can order the rows before deleting them, and then delete only a given number of rows.