MCS-201 · Unit 16

Data Access Using Python

Section 3 of 7

Block 4 · Unit 16Data Access Using Python10 min · Section 3 of 7

16.3SQL Commands and MySQL Data Types

Covers IGNOU MCS-201 §16.2

SQL (Structured Query Language) is the standard language for defining and manipulating relational data. Every major DBMS understands it, with small differences between products. This section uses MySQL syntax, the DBMS used in the rest of the unit.

Groups of SQL commands

GroupPurposeCommands
DDL (Data Definition Language)Define and change the structure of database objectsCREATE, ALTER, DROP, RENAME, TRUNCATE
DML (Data Manipulation Language)Add, read, change and remove dataINSERT, SELECT, UPDATE, DELETE
DCL (Data Control Language)Grant and remove permissionsGRANT, REVOKE
TCL (Transaction Control Language)Manage transactionsCOMMIT, ROLLBACK, SAVEPOINT, SET TRANSACTION

MySQL data types

TypeStoresNotes
INTWhole numbers from −2,147,483,648 to 2,147,483,647BIGINT for larger values
DECIMAL(p, s)Exact decimals, e.g. DECIMAL(10, 2) for moneyFLOAT/DOUBLE are approximate
CHAR(n)Fixed-length text, up to 255 charactersPadded to length n
VARCHAR(n)Variable-length text, n up to 65,535 (limited by the row size)Stores only the characters used
DATE'YYYY-MM-DD', from 1000-01-01 to 9999-12-31e.g. '2026-11-20'
TIME'HH:MM:SS'
DATETIME'YYYY-MM-DD HH:MM:SS'Years 1000 to 9999
TIMESTAMPDate and time, stored in UTC, from 1970 to 2038-01-19Often used for “last updated”

DDL statements

SQL · ddl_examples.sql
CREATE TABLE student (
    roll_no  INT PRIMARY KEY,
    name     VARCHAR(30) NOT NULL,
    course   VARCHAR(10),
    marks    INT CHECK (marks BETWEEN 0 AND 100),
    birth_date DATE
);

ALTER TABLE student ADD sports VARCHAR(20);         -- add a column
ALTER TABLE student MODIFY name VARCHAR(50) NOT NULL; -- change a column
ALTER TABLE student DROP COLUMN birth_date;         -- remove a column
-- DROP TABLE student;   would delete the table and all its data

Constraints such as PRIMARY KEY, NOT NULL, UNIQUE, CHECK and FOREIGN KEY … REFERENCES … let the DBMS enforce the rules of the design.

DML statements

StatementSyntax
INSERTINSERT INTO table (col1, col2) VALUES (v1, v2);
SELECTSELECT [DISTINCT] columns FROM table [WHERE condition] [GROUP BY cols] [HAVING condition] [ORDER BY cols [ASC | DESC]];
UPDATEUPDATE table SET col1 = v1, col2 = v2 WHERE condition;
DELETEDELETE FROM table WHERE condition;

Text and date values go in single quotes; numbers do not. Without a WHERE clause, UPDATE and DELETE affect every row.

DCL and TCL

SQL · dcl_tcl.sql
GRANT SELECT, INSERT ON college.student TO 'clerk'@'localhost';
REVOKE INSERT ON college.student FROM 'clerk'@'localhost';

START TRANSACTION;
UPDATE account SET balance = balance - 500 WHERE acc_no = 1;
UPDATE account SET balance = balance + 500 WHERE acc_no = 2;
COMMIT;            -- or ROLLBACK; to undo both updates

Key takeaways

  • DDL: CREATE, ALTER, DROP; DML: INSERT, SELECT, UPDATE, DELETE; DCL: GRANT, REVOKE; TCL: COMMIT, ROLLBACK, SAVEPOINT.
  • MySQL types: INT, DECIMAL, CHAR, VARCHAR, DATE ('YYYY-MM-DD'), DATETIME, TIMESTAMP.
  • Select only the columns needed; ORDER BY … DESC sorts descending.
  • Always use WHERE with UPDATE and DELETE.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Which group does ALTER TABLE belong to?

Show answer

Answer: A. DDL

It changes the structure of a table, which is data definition.

Question 2 · multiple choice

Which statement lists the names of students in descending order of marks?

Show answer

Answer: C. SELECT name FROM student ORDER BY marks DESC;

ORDER BY is two words and DESC gives descending order; the other forms are invalid SQL.

Question 3 · multiple choice

What does DELETE FROM student; do?

Show answer

Answer: D. Deletes every row, leaving an empty table

Without WHERE, every row matches; DROP TABLE would remove the table itself.

Question 4 · multiple choice

Which MySQL type is best for storing money amounts such as 1234.50?

Show answer

Answer: C. DECIMAL(10, 2)

DECIMAL stores exact decimal values; FLOAT is approximate and text types cannot be summed correctly.

Question 5 · multiple choice

Which pair of commands belongs to TCL?

Show answer

Answer: B. COMMIT and ROLLBACK

Transaction control keeps (COMMIT) or undoes (ROLLBACK) a group of changes.

Question 6 · short answer

Write SQL statements to create a table product(prod_id, prod_name, price) with prod_id as primary key, insert two products, increase every price by 10 %, and display products costing more than 500.

Show worked answer

CREATE TABLE product (prod_id INT PRIMARY KEY, prod_name VARCHAR(50) NOT NULL, price DECIMAL(10, 2)); then INSERT INTO product VALUES (1, 'Keyboard', 450.00), (2, 'Monitor', 9000.00); then UPDATE product SET price = price * 1.10; (no WHERE, because every price should change) and finally SELECT prod_name, price FROM product WHERE price > 500;, which returns only Monitor 9900.00 (the keyboard’s new price, 495.00, is not above 500).

↑ ↓ move↵ openTab switch type