Wednesday, April 23, 2025

MySQL INSERT Alternatives

This MySQL report is showing the result of the SELECT query executed on the "friends" table. It is displaying a table with four columns: id, Name, Lastname, and Telephone.

The table has two rows, where each row represents a record in the "friends" table. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    111333 |
|  2 | Anastasia | TheGreat |    555777 |
+----+-----------+----------+-----------+
2 rows in set (0.00 sec)

mysql>

For example, the first row shows that the person with id 1 is named "John", has a last name of "Smith", and has a telephone number of 111333.

Similarly, the second row shows that the person with id 2 is named "Anastasia", has a last name of "TheGreat", and has a telephone number of 555777. 

This INSERT statement adds a new row to the friends table with the specified values for the id, Name, Lastname, and Telephone columns. The id column is set to NULL, which means that MySQL automatically assigns a new value to it using the AUTO_INCREMENT feature.

The Name, Lastname, and Telephone columns are set to 'Samantha', 'Fox', and 456456, respectively.


mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
    -> VALUES (NULL, 'Samantha', 'Fox', 456456);
Query OK, 1 row affected (0.09 sec)

mysql>

After running the INSERT statement, MySQL returns a message saying that one row was affected. This means that one new row was added to the friends table. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    111333 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
+----+-----------+----------+-----------+
3 rows in set (0.00 sec)

mysql>

The id column was set to NULL, which means that MySQL automatically assigned a new value to it using the AUTO_INCREMENT feature.

When we ran the SELECT statement, all three rows were displayed in the result, including the newly added row for Samantha Fox. 


mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
    -> VALUES (10, 'Larry', 'TheFirst', 555888);
Query OK, 1 row affected (0.17 sec)

mysql>

The above MySQL code is inserting a new row into the "friends" table.

The "id" column is set to 10 explicitly in the INSERT statement, along with values for the "Name", "Lastname", and "Telephone" columns. The query returns "Query OK, 1 row affected", indicating that one row was successfully inserted into the table. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    111333 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
| 10 | Larry     | TheFirst |    555888 |
+----+-----------+----------+-----------+
4 rows in set (0.00 sec)

mysql>

We can see that a new row was added to the friends table using the INSERT statement.

In the first example, we used NULL for the id column, so MySQL assigned a new value to it using the AUTO_INCREMENT feature.

In the second example, we specified a value of 10 for the id column, which was inserted into the table along with the other values.

When we ran the SELECT statement after each INSERT statement, we could see that the table contained all the rows that were added. The number of rows in the result set is displayed at the bottom of the output, which is 3 in the first example and 4 in the second example. 


mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
    -> VALUES (NULL, 'Bill', 'Gates', 444888);
Query OK, 1 row affected (0.08 sec)

mysql>

This SQL statement is inserting a new row into the friends table. The id column is set to NULL, which means that MySQL will automatically assign a new value to it using the AUTO_INCREMENT feature. The Name, Lastname, and Telephone columns are set to the specified values.

The INSERT statement is successful, and MySQL returns a message saying that one row was affected. 


mysql> SELECT * FROM friends;
+----+-----------+----------+-----------+
| id | Name      | Lastname | Telephone |
+----+-----------+----------+-----------+
|  1 | John      | Smith    |    111333 |
|  2 | Anastasia | TheGreat |    555777 |
|  3 | Samantha  | Fox      |    456456 |
| 10 | Larry     | TheFirst |    555888 |
| 11 | Bill      | Gates    |    444888 |
+----+-----------+----------+-----------+
5 rows in set (0.00 sec)

mysql>

Note: It's generally recommended to avoid inserting explicit values into an auto-increment column.

Instead, simply omit the id column from the INSERT statement, and MySQL will assign a new value to it automatically.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...