Introduction and use of Oracle Database DML statements

Source: Internet
Author: User

DML (Data Manipulation Language) is a Data Manipulation Language, one of the types of SQL, in addition to the Data Definition Language (DDL) Data Definition Language and Data Control Language (DCL) Data Control Language. DML includes INSERT, UPDATE, and DELETE. Note that the select statement belongs to DQL (Data Query Language ).

  1. DML (Data Manipulation Language-Data operation Language) can be executed under the following conditions:
  2. • Insert data into a table
  3. • Modify existing data
  4. • Delete existing data
  5. 1Insert statement
  6. Use the INSERT statement to INSERT data to the table.
  7. Insert into table [(column [, column...])]
  8. VALUES (value [, value...]);
  9. With this syntax, only one data entry can be inserted into the table at a time.
  10. // Insert a complete record to the users table
  11. SQL> insert into users (username, password, name, address, zip) values ('Rrmy','123','Honghong','Beijing',100089);
  12. NOTE: If it is a string, add ''. If it is a number, add'' or not ==> generally, do not add
  13. SQL> commit;
  14. Note: The Oracle database must be submitted manually when performing insert or modify operations in the DOS command. Otherwise, the data is not inserted successfully.
  15. When inserting a complete record, the field corresponding to the table in users () can be omitted without writing, but it must correspond to the structure of the table in the database.
  16. // Insert an incomplete record to the users table
  17. SQL> insert into users (username, password) values ('Mj','123');
  18. // Note: The table constraints must be met when inserting a part of the segment. Otherwise, the following exception occurs.
  19. Insert into users (username, password) values ('Mj','123')
  20. RA-01400: NULL cannot be inserted ("RED"."USERS"."NAME")
  21. SQL> insert into users (username, password, name) values ('Mj','123','Marj');
  22. 1Row inserted
  23. SQL> commit;// Manual submission required
  24. Commit complete
  25. The above summary is as follows:
  26. Add a new value to each column.
  27. The values of each column are listed by default.
  28. The column names and their values are randomly listed in the INSERT clause.
  29. Character and date data should be included in single quotes.
  30. Additional knowledge points:
  31. Implicit mode: omit the value of this column in the column name table.
  32. SQL> insert into users (username, password, name) values ('Mj','123','Marj');
  33. Display Mode: specify a null value in the VALUES clause.
  34. SQL> insert into users values ('Test','123','Test',Null,Null);
  35. Common interview questions:
  36. // Back up a table
  37. SQL> create table usess as select * from users;
  38. // Clear records in the table
  39. SQL> delete from usess;
  40. // Insert the records in the users table to usess using the insert statement
  41. SQL> insert into usess (username, password, name, address, zip) select username, password, name, address, zip from users;
  42. SQL> commit
  43. Note:
  44. You do not have to write the VALUES clause.
  45. The Value List in the subquery should correspond to the column name in the INSERT clause.
  46. 2, Update data
  47. Update data using the update Syntax:
  48. Syntax:
  49. UPDATE table
  50. SET column = value [, column = value,...]
  51. [WHERE condition];
  52. Note: Multiple records can be updated at a time.
  53. // Update data: update the users table and reset the passwrod condition to be unique (the primary key is unique.
  54. SQL> update users set password ='Redarmy'Where username ='Redarmy';
  55. SQL> commit
  56. Note: When updating multiple fields, you can use commas to differentiate them. For example:
  57. SQL> update users set password ='Redarmy', Name ='Chen hongjun'Where username ='Rrmy';
  58. SQL> commit
  59. Note: If the WHERE clause is omitted, all data in the table will be updated.
  60. SQL> update users set password ='Redarmy', Name ='Chen hongjun';
  61. 6Rows updated
  62. SQL> commit;
  63. Commit complete
  64. SQL> select * from users;
  65. USERNAME PASSWORD NAME ADDRESS ZIP
  66. ---------------------------------------------------------------------------------------
  67. Huxz redarmy Chen hongjun's home in Beibei100012
  68. Liucy redarmy Chen hongjun Tsinghua Park100084
  69. Redarmy Chen hongjun Beijing100089
  70. Rrmy redarmy Chen hongjun Beijing100089
  71. Mj redarmy Chen hongjun
  72. Test redarmy Chen hongjun
  73. 6Rows selected
  74. Supplement: Use the subquery in the update statement
  75. // Update huxz's zip code to be consistent with the user's liucy zip code.
  76. SQL> update users set zip = (select zip from users where username ='Liucy') Where username ='Huxz';
  77. // Note: subqueries can also be used in the where condition when updating data.
  78. 3, Delete data
  79. If some data in the table is not needed, you can use the delete statement to delete the data and release the storage space occupied by the data. The delete syntax is as follows:
  80. DELETE [FROM] table
  81. [WHERE condition];
  82. Note: The delete statement only deletes data from the table and does not delete the table structure. The drop statement is used to delete the table structure.
  83. Note: When deleting data in a table, you must consider the constraints of the table (otherwise, some exceptions may occur)
  84. // Clear the delete from table name | delete table name
  85. // The where condition for data deletion is consistent with the update condition. The where condition must be unique (the primary key is unique ).
  86. SQL> delete from users where username ='Test';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.