Thursday, December 19, 2024

SQL VALUES Keyword

 The VALUES keyword in SQL is used to insert data into a table. It is typically used with the INSERT INTO statement to specify the data that you want to insert into a table.

Syntax:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Example:

Suppose you have a table called employees with the columns id, name, and age.

INSERT INTO employees (id, name, age)
VALUES (1, 'John Doe', 30);

You can also insert multiple rows in one query:

INSERT INTO employees (id, name, age)
VALUES 
    (2, 'Jane Smith', 28),
    (3, 'Samuel Adams', 35);

Important Notes:

  1. The number of values in the VALUES clause must match the number of columns specified in the INSERT INTO clause.

  2. You can omit the column names if you're providing values for all columns in the same order as they are defined in the table:

    INSERT INTO employees 
    VALUES (4, 'Alice Brown', 25);
    
  3. Some SQL databases allow you to use the DEFAULT keyword to insert default values for certain columns:

    INSERT INTO employees (id, name)
    VALUES (5, 'Bob Green');
    

In this case, if the age column has a default value or is nullable, it will be automatically handled by the database.

No comments:

Post a Comment