DML(資料操縱語言)
它指的是一種用於在資料庫中插入、刪除和更新資料的語言。它用於資料庫中資料的操作。DCL 與 DML 一起使用,因為 DML 做出的更改不會永久儲存,並且有可能回滾。以下列出三個 DML 命令
插入命令
它用於在表中插入一行資料。
語法
INSERT INTO table_name(col1,col2,col3,..) VALUES (value1,value2,value3,...); or INSERT INTO table_name VALUES (value1,value2,value3,...);
示例
此示例將演示如何在表中使用插入命令。Insert 用於透過 insert 語句將值或實體提供到表中
演算法
步驟 1:建立表
步驟 2:使用 INSERT 將值插入表中。
步驟 3:選擇表以顯示輸出並檢視插入的值
CREATE TABLE student(id int,name char(50),roll_no. (50),branch char(50);#table created INSERT INTO student(id,name,roll_no,branch) VALUES (1,aman,20,cs), (2,naman,21,civil), (3,raman,22,bao);#rows are inserted SELECT * FROM student;#table selected to show output
輸出
id name roll_no. branch 1 aman 20 cs 2 naman 21 civil 3 raman 22 bao
更新命令
它用於更新資料庫表中已有的資料。可以根據需要一次更新單個或多個列。
語法
UPDATE table_name SET values_to_update(1_col=1_value,2_col=2_value,....) WHERE condition;
這裡,
table_name 是表名
1_col、2_col...是列
1_value、2_value...是列的更新值
條件用於選擇將更新值的行的。
演算法
步驟 1:使用 update 更新資料
步驟 2:提供表名
步驟 3:設定要更新的值
步驟 4:提供執行更新操作的位置條件
步驟 5:選擇表以顯示輸出
示例
UPDATE student #update operation is to be performed on student table SET name='monu',roll_no=25#set updated values WHERE id=1;#condition regarding where to update SELECT * FROM student;#Select table to show output
輸出
id name roll_no. branch 1 monu 25 cs 2 naman 21 civil 3 raman 22 bao
刪除命令
它用於從給定的表中刪除記錄。可以刪除單個或多個記錄,具體取決於 WHERE 子句中提供的條件。
語法
DELETE FROM table_name WHERE condition;
這裡,
table_name 是表名
table_name 是表名
示例
此示例將演示如何使用 delete 命令刪除或移除表中存在的現有資料。
演算法
步驟 1:使用 delete
步驟 2:提供表名
步驟 3:提供執行刪除操作的位置條件
步驟 4:選擇表以顯示輸出
輸入
id name roll_no. branch 1 monu 25 cs 2 naman 21 civil 3 raman 22 bao
示例
DELETE FROM student#table on which data deletion will occur WHERE id=1;# condition regarding where to delete SELECT * FROM student;#Select table to show output
輸出
id name roll_no. branch 2 naman 21 civil 3 raman 22 bao
結論
本文解釋了 DML,它是 SQL 語句的一部分,其中執行諸如插入、更新和刪除之類的命令。Insert 用於在表中插入一行資料。Update 用於更新資料庫中的資料。Delete 用於從資料庫中刪除資料。
廣告