Dml in Mysql

Embed Size (px)

Citation preview

  • 7/27/2019 Dml in Mysql

    1/3

    DML Statement in MySQL

    Data Manipulation Language (DML) statements are used for managing data within

    schema objects (e.g. table). It includes Inserting, Updating and deleting of data.

    Inserting Data into Tables:

    INSERT statement allows you to insert one or more rows to the table.

    INTO clause in the INSERT statement is optional.

    It is possible to write the INSERT INTO statement in two forms.

    The first form doesn't specify the column names where the data will be inserted, only their

    values. It is used where users need to insert values to all available columns of a table.

    Syntax:

    mysql> INSERT INTO table_name

    VALUES (value1, value2, value3,...);

    Example:

    mysql> INSERT INTO student

    VALUES (1,'Ajay','Hyderabad');

    The second form specifies both the column names and the values to be inserted. It is used

    where users need to insert value to specific column(s) of a given table.

    Syntax:

    mysql> INSERT INTO table_name (column1, column2,

    column3,...) VALUES (value1, value2, value3,...)

    Example:

    mysql> INSERT INTO student (Rollno, Name)

    VALUES (2, 'Rahul');

    In another form, instead of providing explicit data, you select it from other table by using

    SELECT statement. This form allows you to copy all or some part of data from other table to the

    inserted table.

  • 7/27/2019 Dml in Mysql

    2/3

    As an example, we can create a temporary table and insert all offices which locate in US

    into that one by using this query:

    mysql> INSERT INTO temp_table SELECT * FROM offices WHERE

    country = US;

    Updating Data in Tables:

    SQL UPDATE statement is used to update existing data in database tables. It can be used

    to change values of single row, group of rows or even all rows in a table.

    Followed by the UPDATE keyword is the name of a table you want to change the data. If

    an UPDATE command violates any integrity constraint, MySQL does not perform the update

    and it will issue an error message.

    The SET clause determines the column(s) name and the changed value(s). The changed

    values could be a constant value, expression or even a subquery.

    WHERE clause determines which rows of the tables will be updated. It is an optional part

    of SQL UPDATE statement. If WHERE clause is ignored, all rows in the tables will be updated.

    In employees table, if you want to update the email of Mary with employeeid 1 with the

    new email as [email protected], you can execute the following query.

    mysql> UPDATE employees SET email [email protected]

    WHERE employeeid = 1;

    The following syntax will update the email of all employees with the new email as

    [email protected].

    mysql> UPDATE employees SET email [email protected];

    Deleting Records from Tables:

    The DELETE FROM statement is used to delete records from a database table. TheWHERE clause in DELETE statement specifies condition to limit which rows you want to

    remove.If a record meets WHERE condition, it will be removed from the database table

    permanently. If the WHERE clause is ignored in the MySQL DELETE statement, all rows of thetable are deleted.

  • 7/27/2019 Dml in Mysql

    3/3

    Suppose you want to delete all employees in an office with officeNumber is 4, just execute

    the following query:

    mysql> DELETE FROM employees WHERE officeCode = 4;

    To delete all employees from all offices, just remove the WHERE condition as follows:

    mysql> DELETE FROM employees;