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 ).
- DML (Data Manipulation Language-Data operation Language) can be executed under the following conditions:
- • Insert data into a table
- • Modify existing data
- • Delete existing data
- 1Insert statement
- Use the INSERT statement to INSERT data to the table.
- Insert into table [(column [, column...])]
- VALUES (value [, value...]);
- With this syntax, only one data entry can be inserted into the table at a time.
- // Insert a complete record to the users table
- SQL> insert into users (username, password, name, address, zip) values ('Rrmy','123','Honghong','Beijing',100089);
- NOTE: If it is a string, add ''. If it is a number, add'' or not ==> generally, do not add
- SQL> commit;
- 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.
- 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.
- // Insert an incomplete record to the users table
- SQL> insert into users (username, password) values ('Mj','123');
- // Note: The table constraints must be met when inserting a part of the segment. Otherwise, the following exception occurs.
- Insert into users (username, password) values ('Mj','123')
- RA-01400: NULL cannot be inserted ("RED"."USERS"."NAME")
- SQL> insert into users (username, password, name) values ('Mj','123','Marj');
- 1Row inserted
- SQL> commit;// Manual submission required
- Commit complete
- The above summary is as follows:
- Add a new value to each column.
- The values of each column are listed by default.
- The column names and their values are randomly listed in the INSERT clause.
- Character and date data should be included in single quotes.
- Additional knowledge points:
- Implicit mode: omit the value of this column in the column name table.
- SQL> insert into users (username, password, name) values ('Mj','123','Marj');
- Display Mode: specify a null value in the VALUES clause.
- SQL> insert into users values ('Test','123','Test',Null,Null);
- Common interview questions:
- // Back up a table
- SQL> create table usess as select * from users;
- // Clear records in the table
- SQL> delete from usess;
- // Insert the records in the users table to usess using the insert statement
- SQL> insert into usess (username, password, name, address, zip) select username, password, name, address, zip from users;
- SQL> commit
- Note:
- You do not have to write the VALUES clause.
- The Value List in the subquery should correspond to the column name in the INSERT clause.
- 2, Update data
- Update data using the update Syntax:
- Syntax:
- UPDATE table
- SET column = value [, column = value,...]
- [WHERE condition];
- Note: Multiple records can be updated at a time.
- // Update data: update the users table and reset the passwrod condition to be unique (the primary key is unique.
- SQL> update users set password ='Redarmy'Where username ='Redarmy';
- SQL> commit
- Note: When updating multiple fields, you can use commas to differentiate them. For example:
- SQL> update users set password ='Redarmy', Name ='Chen hongjun'Where username ='Rrmy';
- SQL> commit
- Note: If the WHERE clause is omitted, all data in the table will be updated.
- SQL> update users set password ='Redarmy', Name ='Chen hongjun';
- 6Rows updated
- SQL> commit;
- Commit complete
- SQL> select * from users;
- USERNAME PASSWORD NAME ADDRESS ZIP
- ---------------------------------------------------------------------------------------
- Huxz redarmy Chen hongjun's home in Beibei100012
- Liucy redarmy Chen hongjun Tsinghua Park100084
- Redarmy Chen hongjun Beijing100089
- Rrmy redarmy Chen hongjun Beijing100089
- Mj redarmy Chen hongjun
- Test redarmy Chen hongjun
- 6Rows selected
- Supplement: Use the subquery in the update statement
- // Update huxz's zip code to be consistent with the user's liucy zip code.
- SQL> update users set zip = (select zip from users where username ='Liucy') Where username ='Huxz';
- // Note: subqueries can also be used in the where condition when updating data.
- 3, Delete data
- 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:
- DELETE [FROM] table
- [WHERE condition];
- 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.
- Note: When deleting data in a table, you must consider the constraints of the table (otherwise, some exceptions may occur)
- // Clear the delete from table name | delete table name
- // The where condition for data deletion is consistent with the update condition. The where condition must be unique (the primary key is unique ).
- SQL> delete from users where username ='Test';