In our continuing journey, we now learn to wield the powerful spells of SQL (Structured Query Language) to manipulate data within our databases. These commands are like magic incantations that allow you to insert, select, update, and delete data. Let’s dive into each of these essential queries.
Imagine you’re adding new books to a shelf. The INSERT command lets you place new records into your table.
Syntax:
SQL
Copy code
INSERT INTO table_name (column1, column2, column3, …)
VALUES (value1, value2, value, …);
Example: Let’s say we have a table named students with columns id, name, and age. To add a new student, we would use:
SQL
Copy code
INSERT INTO students (name, age) VALUES (‘Alice’, 23);
When you want to find specific books on a shelf, you use the SELECT command to retrieve data from the table.
Syntax:
SQL
Copy code
SELECT column1, column2, …
FROM table_name
WHERE condition;
Example: To retrieve all students from the students table:
SQL
Copy code
SELECT * FROM students;
To retrieve only the names of students who are older than 20:
SQL
Copy code
SELECT name FROM students WHERE age > 20;
Updating is like editing the information in an existing book. The UPDATE command allows you to modify existing records in your table.
Syntax:
SQL
Copy code
UPDATE table_name
SET column1 = value1, column2 = value2, …
WHERE condition;
Example: To change the age of a student named Alice to 24:
SQL
Copy code
UPDATE students SET age = 24 WHERE name = ‘Alice’;
Deleting is like removing books from a shelf. The DELETE command lets you remove records from your table.
Syntax:
SQL
Copy code
DELETE FROM table_name
WHERE condition;
Example: To delete a student named Alice from the students table:
SQL
Copy code
DELETE FROM students WHERE name = ‘Alice’;
Insert Data
1. Task: Add a new student named Bob, aged 25, to the students table.
SQL
Copy code
INSERT INTO students (name, age) VALUES (‘Bob’, 25);
2. Task: Add a new student named Carol, aged 22, to the students table.
SQL
Copy code
INSERT INTO students (name, age) VALUES (‘Carol’, 22);
Select Data
1. Task: Retrieve all students from the students table.
SQL
Copy code
SELECT * FROM students;
2. Task: Retrieve the names and ages of students aged over 21.
SQL
Copy code
SELECT name, age FROM students WHERE age > 21;
Update Data
1. Task: Update the age of Bob to 26.
SQL
Copy code
UPDATE students SET age = 26 WHERE name = ‘Bob’;
2. Task: Change Carol’s name to Caroline.
SQL
Copy code
UPDATE students SET name = ‘Caroline’ WHERE name = ‘Carol’;
Delete Data
1. Task: Remove the student named Caroline.
SQL
Copy code
DELETE FROM students WHERE name = ‘Caroline’;
2. Task: Delete all students aged below 24.
SQL
Copy code
DELETE FROM students WHERE age < 24;
Mastering these basic SQL queries allows you to efficiently interact with your databases using phpMyAdmin. With the power to insert, select, update, and delete data, you’re well-equipped to manage and manipulate data, much like a skilled librarian organizing an ever-growing collection of books. Keep practicing, and soon these commands will become second nature, enhancing your ability to handle any data-related task with ease. Happy querying!