TECHNOLOGIESTieInn Technologies

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

  • UPDATE or DELETE without WHERE changes every row.
  • DROP TABLE removes the table; TRUNCATE empties 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 →

Questions

Open in Code Lab5 free questions
5 questions

    Creating and changing tables (DDL and DML)

  1. Question 1easy

    Which statement removes the table itself, structure and all?

  2. Creating and changing tables (DDL and DML)

  3. Question 2easy

    What does UPDATE products SET price = 0 do?

  4. Creating and changing tables (DDL and DML)

  5. Question 3medium

    What does a PRIMARY KEY on a column guarantee, in databases that enforce it?

  6. Creating and changing tables (DDL and DML)

  7. 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;
  8. Creating and changing tables (DDL and DML)

  9. Question 5hard

    Create a temporary table new_products with columns product_id (integer), product_name (text), and price (decimal). Insert two rows: (101, 'Mouse Pad', 299) and (102, 'Webcam', 2199). Then return all its rows.

    Tables

12 more questions on this topic, with hints, and tests

Sign up free for 21 days of every question and test.