MySQL INSERT INTO

The INSERT INTO statement is used to add new records to a table. It can be used to insert a single record or multiple records in one query.

Example with Tamil Kings

1. Inserting a Single Record

INSERT INTO tamil_kings_auto_increment (king_name, reign_period)
VALUES ('Raja Raja Chola', '985–1014 CE');

Code Explanation: This command inserts a new record with the king's name 'Raja Raja Chola' and the reign period '985–1014 CE' into the tamil_kings_auto_increment table.

2. Inserting Multiple Records

INSERT INTO tamil_kings_auto_increment (king_name, reign_period)
VALUES ('Rajendra Chola I', '1014–1044 CE'),
       ('Karikala Chola', '100–125 CE');

Code Explanation: This command inserts two records into the tamil_kings_auto_increment table in a single query.

Best Practices

  • Always specify the column names to ensure data is inserted correctly.
  • Use transactions if inserting multiple records that must be handled together.

Key Takeaways

  • The INSERT INTO statement adds new records to a table.
  • You can insert one or multiple records in a single query.