0% found this document useful (0 votes)
1 views5 pages

Crud

CRUD method in SQL

Uploaded by

maneabhishek96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views5 pages

Crud

CRUD method in SQL

Uploaded by

maneabhishek96
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Contents

• 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)

Adding Data in to Table


• To add data to a table, we use the INSERT command.
Case1: (Insert Data to All Columns)
• Syntax:
INSERT INTO <table_name> values(value1,value2....valuen);
Explanation
o After INSERT INTO, we put the name of the table.
o We add the keyword VALUES followed by the actual values.
o We need to put the values in the exact same order in which the columns appear in the database
• Example:

Case2: (Insert Partial Data)


• Syntax:
INSERT INTO <table_name>(colname1,colname2,..) values(value1,value2...)
• Example:

Case3: (Insert Multiple Rows)


• Syntax:
INSERT INTO <table_name> values(value1,value2..),(value1,value2..),(valuen,valuen..);
• Example:

Case4: (Insert NULL values)

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:

• By default SQL safe mode is enabled in MySQL work bench


Go to edit → preference → SQL editor → Unchecked Safe mode checkbox → close and start MySQL
workbench.

(OR)
SET SQL_SAFE_UPDATES=0;
Case2: (Modify Multiple Column)
• Syntax:
UPDATE <table_name> SET col1=value,col2=value WHERE condition;
• Example:

Case3- (Modify data – arithmetic operations)

-- update record without condition [not recomended]


UPDATE user SET age=30;
Delete Data
• To delete data from a table, we use the DELETE command.
Case1: (Delete Specific Rows)
• Syntax:
DELETE FROM <table_name> WHERE condition;
• Example:

Case2: (Delete All Rows)


• Syntax:
DELETE FROM <table_name>;
• 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 ----

Appraoch2: (All Rows + Specific Column)


• Syntax:
SELECT colname1 FROM <table_name>;
• Example:

Appraoch3: (Specific Rows + All Columns)


• Syntax:
SELECT * FROM <table_name> WHERE condition;
• Example:

Appraoch4: (Specific Rows + Specific Columns)


• Syntax:
SELECT colname1, colname2 FROM <table_name> WHERE condition;
• Example:

Advanced features of Insert, Update and Delete


Load Data from CSV file into Database Table
• Go to MySQL Workbench → Right click on database → Click on Table Data Import Wizard
IS NULL and IS NOT NULL
• This can be used with update, delete and select statement.
Question1:
exam table
Some students didn't receive any scores on a written exam – the teacher forgot to enter their scores. Correct this
mistake by assigning all missing written exam values a score of 43.

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.

Swapping Column Values


It's time to fix another mistake in our database. We have to swap oral_exam_date with written_exam_date.

Create Table from another existing Table


Approach1: (Create Table with Data)

Approach2: (Create Table without Data)

Copy data with INSERT INTO … SELECT Command


• We can copy data from one to another using INSERT INTO … SELECT command.

You might also like