Crud
Crud
• Insert Command
• Update Command
• Delete Command
• Select Command
• Advanced SQL command
CRUD
• Create – Create data with INSERT command
• Retrieve – Retrieve data with SELECT command
• Update – Update data with UPDATE command
• Delete - Delete data with DELETE command
Manage Data
• Data in any database is constantly changing. We have to add new information to a database, modify
information in a database, and remove information from a database.
Table: User
Columns: (id, name, age)
Modify/Update Data
• To update data to a table, we use the UPDATE command.
Case1: (Modify Single Column)
• Syntax:
UPDATE <table_name> SET col1=value WHERE condition;
• Example:
(OR)
SET SQL_SAFE_UPDATES=0;
Case2: (Modify Multiple Column)
• Syntax:
UPDATE <table_name> SET col1=value,col2=value WHERE condition;
• Example:
-- SOFT DELETE
-- add new column - flag [0 (deleted)/1]
ALTER TABLE user ADD flag int;
UPDATE user SET flag = 1;
SELECT * FROM user WHERE flag=1;
UPDATE user SET flag=0 WHERE id=1;
Retrieve Data
• To retrieve data from a table, we use the SELECT command.
Appraoch1: (All Rows + All Columns)
• Syntax:
SELECT * FROM <table_name>;
SELECT colname1,colname2,colname3 FROM <table_name>;
• Example ----
Question2:
A group of students weren't scored on an oral exam, so the exam was canceled. The results from this exam have to
be removed. Write a query to remove these records.
AND, OR Operator
Question3:
Assign an oral exam score of 3 points to all students who do not have an oral English exam score date.
Question4:
Our university is not accredited to conduct exams in Spanish and Mathematics. Delete all records for these exams.
Question5:
The oral exams with no date assigned took place on same day of the written_exam_date. Update the data.