Wednesday, April 23, 2025

MySQL INSERT Values Into Table

Our MySQL code is inserting a new row into the friends table.

The INSERT INTO statement is used to insert data into a table, and the friends (id, Name, Lastname, Telephone) specifies the table and columns where the data will be inserted.

The values to be inserted are specified in the VALUES clause, which is VALUES (NULL, 'John', 'Smith', 111333) in this case. 


mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
    -> VALUES (NULL, 'John', 'Smith', 111333);
Query OK, 1 row affected (0.27 sec)

mysql>

We are inserting a row with the following values:

  • id: NULL (the id column is an auto-increment column, so MySQL will generate a unique value for this column)
  • Name: 'John'
  • Lastname: 'Smith'
  • Telephone: 111333

The output Query OK, 1 row affected (0.27 sec) indicates that the query was executed successfully and one row was affected (i.e., inserted) in the friends table. 


mysql> SELECT * FROM friends;
+----+------+----------+-----------+
| id | Name | Lastname | Telephone |
+----+------+----------+-----------+
|  1 | John | Smith    |    111333 |
+----+------+----------+-----------+
1 row in set (0.02 sec)

mysql>

This code is performing a SELECT query on the "friends" table in the "test" database.

The asterisk (*) after the SELECT keyword indicates that all columns in the table should be returned in the result set.

The query returns one row of data that was inserted earlier using an INSERT statement. The row contains the values for the "id", "Name", "Lastname", and "Telephone" columns of the table. 


mysql> INSERT INTO friends (id, Name, Lastname, Telephone)
    -> VALUES (NULL, 'Anastasia', 'TheGreat', 555777);
Query OK, 1 row affected (0.14 sec)

mysql>

This MySQL code inserts a new row of data into the "friends" table.

The data includes the columns "id", "Name", "Lastname", and "Telephone". Since "id" is defined as an AUTO_INCREMENT field, the value for "id" is set to NULL, which will trigger MySQL to automatically assign the next available ID number.

The values for the "Name", "Lastname", and "Telephone" columns are specified in the VALUES clause of the INSERT statement.

The inserted data is: id=2, Name='Anastasia', Lastname='TheGreat', Telephone=555777. The query returns a message indicating that one row was affected. 


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

mysql>

As we can see, the INSERT statement added a new row to the friends table with the specified values for the id, Name, Lastname, and Telephone columns.

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, both rows were displayed in the result.

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 .  ...