close[x]


MySQL

MySQL-Home MySQL-Environment setup MySQL- Workbench MySQL-Basic syntax MySQL-Operator MySQL-Data type MySQL-Comments MySQL-Create DB MySQL-Drop DB MySQL-Select DB MySQL-Create Table MySQL-Drop table MySQL-Truncate MySQL-Primary Key MySQL-Foreign Key MySQL-Null MySQL-Increment MySQL-Having MySQL-Top MySQL-Insert Statement MySQL-Select Statement MySQL-Alter Statement MySQL-Where MySQL-And & Or MySQL-Default values MySQL-Exists MySQL-Order by MySQL-View MySQL-Update Statement MySQL-Delete Statement MySQL-Like MySQL-Sort MySQL-Limit MySQL-Min MySQL-Max MySQL-Group MySQL-In MySQL-Between MySQL-Union MySQL-Count MySQL-Average MySQL-Sum MySQL-Date & Time MySQL-Import MySQL-Export MySQL-Index MySQL-Temporary MySQL-Join MySQL-Full Join MySQL-Inner Join MySQL-Left Join MySQL-Right Join MySQL-Store Procedure MySQL-Injection MySQL-PHP connection



learncodehere.com




MySQL - INSERT Statement

The INSERT INTO statement is used to insert new records in a table.

It is used to insert a single or a multiple records in a table.

There are two ways to insert data in a table.

  • Only values
  • Column names and values

  • Only Values

    This method is to specify only the value of data to be inserted without the column names.

    Syntax : Insert Values

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

    Column names and values

    In this method we will specify both the columns which we want to fill and their corresponding values.


    Syntax : Insert Values with Columns name

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

    column1, column2, column3,... are the names of the columns in the table into which you want to insert the data.

    First we already create table name "students"


    Example : Create Table

    
     CREATE TABLE Students
     (
     ROLL_NO int(3),
     NAME varchar(20),
     SUBJECT varchar(20),
     );
    

    Below is a selection from the "students" table

    Students table available in your database which you can use to store the required information related to Students.

    The empty "Students" table will now look like this:


    db table


    Insert Into Table

    The following MySQL statement inserts a new record in the "students" table.


    Example : Insert Values

    
     INSERT INTO students VALUES (‘1′,’Will’,’C++’);
     INSERT INTO students VALUES (‘2′,’Sam’,’Python’);
     INSERT INTO students VALUES (‘3′,’Sara’,’HTML’);
    

    The table students will now look like:

    ROLL_NO NAME SUBJECT
    1 Will C++
    2 SAM Python
    3 Sara HTML

    Now we will specify both the columns which we want to fill and their corresponding values.

    The following SQL statement inserts a new record in the "students" table.


    Example : Insert Values with Columns name

    
     INSERT INTO students (ROLL_NO, NAME, SUBJECT) 
     VALUES (‘1′,’Will’,’C++’),(‘2′,’Sam’,’Python’);
       
     INSERT INTO STUDENTS (ROLL_NO, NAME, SUBJECT)  
     VALUES (‘4′,’Rim’,’Java’ );  
     INSERT INTO STUDENTS (ROLL_NO, NAME, SUBJECT)  
     VALUES (‘5′,’Micheal’,’SQL’);   
     INSERT INTO STUDENTS (ROLL_NO, NAME)  
     VALUES (‘6′,’Lara’,);       
    

    The table students will now look like:

    ROLL_NO NAME SUBJECT
    1 Will C++
    2 SAM Python
    3 Sara HTML
    4 Rim Java
    5 Micheal SQL
    6 Lara