How to do insert update delete in MySql Workbench

preview_player
Показать описание
In this part of the MySQL tutorial, we will insert, update and delete data from MySQL tables. We will use the INSERT, DELETE and UPDATE statements. These statements are part of the SQL Data Manipulation Language, DML.
Inserting data
The INSERT statement is used to insert data into tables.
We will create a new table, where we will do our examples.
mysql CREATE TABLE Books(Id INTEGER PRIMARY KEY, Title VARCHAR(100),
- Author VARCHAR(60));
We create a new table Books, with Id, Title and Author columns.
mysql INSERT INTO Books(Id, Title, Author) VALUES(1, 'War and Peace',
- 'Leo Tolstoy');
This is the classic INSERT SQL statement. We have specified all column names after the table name and all values after the VALUES keyword. We add our first row into the table.
mysql SELECT * FROM Books;
+----+---------------+-------------+
| Id | Title | Author |
+----+---------------+-------------+
| 1 | War and Peace | Leo Tolstoy |
+----+---------------+-------------+
We have inserted our first row into the Books table.
mysql INSERT INTO Books(Title, Author) VALUES ('The Brothers Karamazov',
- 'Fyodor Dostoyevsky');
We add a new title into the Books table. We have omitted the Id column. The Id column has AUTO_INCREMENT attribute. This means that MySQL will increase the Id column automatically. The value by which the AUTO_INCREMENT column is increased is controlled by auto_increment_increment system variable. By default it is 1.
mysql SELECT * FROM Books;
+----+------------------------+--------------------+
| Id | Title | Author |
+----+------------------------+--------------------+
| 1 | War and Peace | Leo Tolstoy |
| 2 | The Brothers Karamazov | Fyodor Dostoyevsky |
+----+------------------------+--------------------+
Here is what we have in the Books table.
mysql INSERT INTO Books VALUES(3, 'Crime and Punishment',
- 'Fyodor Dostoyevsky');
In this SQL statement, we did not specify any column names after the table name. In such a case, we have to supply all values.
mysql REPLACE INTO Books VALUES(3, 'Paradise Lost', 'John Milton');
Query OK, 2 rows affected (0.00 sec)
The REPLACE statement is a MySQL extension to the SQL standard. It inserts a new row or replaces the old row if it collides with an existing row. In our table, there is a row with Id=3. So our previous statement replaces it with a new row. There is a message that two rows were affected. One row was deleted and one was inserted.
mysql SELECT * FROM Books WHERE Id=3;
+----+---------------+-------------+
| Id | Title | Author |
+----+---------------+-------------+
| 3 | Paradise Lost | John Milton |
+----+---------------+-------------+
This is what we have now in the third column.
We can use the INSERT and SELECT statements together in one statement.
mysql CREATE TABLE Books2(Id INTEGER PRIMARY KEY AUTO_INCREMENT,

#USA The Coding Bus #TheCodingBus
#TCB #unitedstates #us
Рекомендации по теме