Creating and changing tables (DDL and DML)
SQL · 17 questions
Short notes
Key idea
DDL defines tables (CREATE, ALTER, DROP); DML changes the rows in them (INSERT, UPDATE, DELETE). Constraints stop bad data getting in.
Syntax
CREATE TABLE customers (
customer_id INT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE,
city TEXT DEFAULT 'Chennai'
);
INSERT INTO customers (customer_id, name) VALUES (1, 'Asha');
UPDATE customers SET city = 'Madurai' WHERE customer_id = 1;
DELETE FROM customers WHERE customer_id = 1;
ALTER TABLE customers ADD COLUMN phone TEXT;
Example
Raise every electronics price by 5%:
UPDATE products
SET price = price * 1.05
WHERE category = 'electronics';
Run the same WHERE as a SELECT first to see which rows will change.
Traps
UPDATEorDELETEwithoutWHEREchanges every row.DROP TABLEremoves the table;TRUNCATEempties it; neither asks twice.- A foreign key stops you deleting a parent row that child rows still point to.
- Use a transaction (
BEGIN…COMMIT) when several changes must succeed together.
Want the full lesson with step-by-step examples, a project and a certificate? SQL course, ₹120 →
- Question 1easy
Which statement removes the table itself, structure and all?
- Question 2easy
What does
UPDATE products SET price = 0do? - Question 3medium
What does a
PRIMARY KEYon a column guarantee, in databases that enforce it? - Question 4medium
The Books category has 4 products priced ₹650, ₹899, ₹450, and ₹249. Product 1 costs ₹1,999. What does the last line return?
CREATE TEMP TABLE picks AS SELECT * FROM products WHERE category = 'Books'; INSERT INTO picks SELECT * FROM products WHERE product_id = 1; DELETE FROM picks WHERE price < 500; SELECT COUNT(*) FROM picks;
- Question 5hard
Create a temporary table
new_productswith columnsproduct_id(integer),product_name(text), andprice(decimal). Insert two rows: (101, 'Mouse Pad', 299) and (102, 'Webcam', 2199). Then return all its rows.Tables
Creating and changing tables (DDL and DML)
Creating and changing tables (DDL and DML)
Creating and changing tables (DDL and DML)
Creating and changing tables (DDL and DML)
Creating and changing tables (DDL and DML)
12 more questions on this topic, with hints, and tests
Sign up free for 21 days of every question and test.