++++Practice insert, select, null checks, update, delete, and alter operations on a small customer_info table.
4. DML Practice with customer_info
DML Practice with customer_info
Data Manipulation Language, or DML, is the part of SQL used to work with rows:
INSERTadds rows.SELECTreads rows.UPDATEchanges rows.DELETEremoves rows.
This practice uses the existing companydb database from the previous lesson.
Create the table
USE companydb;
CREATE TABLE customer_info (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(25),
last_name VARCHAR(25),
salary INT
);AUTO_INCREMENT lets MySQL generate the next id value for each inserted row.
Insert rows
Insert one customer:
INSERT INTO customer_info (first_name, last_name, salary)
VALUES
("Krish", "Naik", 50000);Insert multiple customers:
INSERT INTO customer_info (first_name, last_name, salary)
VALUES
("Darius", "N", 50000),
("Mahendra", "B", 50000),
("Ankit", "Sharma", NULL);Check the table:
SELECT *
FROM customer_info;Find missing and present salaries
Find customers whose salary is missing:
SELECT first_name, last_name
FROM customer_info
WHERE salary IS NULL;
SELECT *
FROM customer_info
WHERE salary IS NULL;Find customers whose salary is present:
SELECT first_name, last_name
FROM customer_info
WHERE salary IS NOT NULL;
SELECT *
FROM customer_info
WHERE salary IS NOT NULL;Replace a NULL value
Update the customer whose generated ID is 4.
UPDATE customer_info
SET salary = 60000
WHERE id = 4;Delete a row
DELETE FROM customer_info
WHERE id = 3;Then inspect the remaining data:
SELECT *
FROM customer_info;Insert another row correctly
The original practice script had a mismatched insert: it listed three target columns but supplied four values. Because id is auto-generated, the clean version is:
INSERT INTO customer_info (first_name, last_name, salary)
VALUES
("Darius", "N", 50000);If you want to set the ID yourself, include the id column and choose an unused value.
INSERT INTO customer_info (id, first_name, last_name, salary)
VALUES
(10, "Darius", "N", 50000);Alter the table
Add one column:
ALTER TABLE customer_info
ADD COLUMN dob DATE;Add multiple columns:
ALTER TABLE customer_info
ADD COLUMN email VARCHAR(25),
ADD COLUMN address VARCHAR(25);Inspect the schema:
DESC customer_info;Modify the column size:
ALTER TABLE customer_info
MODIFY COLUMN address VARCHAR(30);Drop a column:
ALTER TABLE customer_info
DROP COLUMN email;This exercise is small, but it covers the core row-level operations used in real data cleanup work.