Published by Patrick Mutisya · 14 days ago
By the end of this lesson you should be able to:
| Command | Purpose | Typical Syntax (simplified) |
|---|---|---|
| CREATE TABLE | Define a new table and its columns. | CREATE TABLE table_name ( column1 datatype, column2 datatype, … ); |
| ALTER TABLE | Modify an existing table – add, drop or change columns. | ALTER TABLE table_name ADD column datatype; ALTER TABLE table_name DROP COLUMN column; |
| DROP TABLE | Delete a table and all its data permanently. | DROP TABLE table_name; |
| CREATE INDEX | Speed up searching on one or more columns. | CREATE INDEX idxname ON tablename(column); |
| DROP INDEX | Remove an index. | DROP INDEX idx_name; |
| Command | Purpose | Typical Syntax (simplified) |
|---|---|---|
| INSERT | Add new rows to a table. | INSERT INTO table_name (col1, col2) VALUES (val1, val2); |
| UPDATE | Modify existing rows. | UPDATE tablename SET col = newvalue WHERE condition; |
| DELETE | Remove rows. | DELETE FROM table_name WHERE condition; |
| SELECT | Retrieve data. | SELECT col1, col2 FROM table_name WHERE condition; |
Read each statement and answer the questions that follow.
CREATE TABLE Student (
StudentID INT PRIMARY KEY,
FirstName \cdot ARCHAR(30) NOT NULL,
LastName \cdot ARCHAR(30) NOT NULL,
DOB DATE,
EnrolDate DATE DEFAULT CURRENT_DATE
);
ALTER TABLE Student ADD Email \cdot ARCHAR(50) UNIQUE;
UNIQUE constraint.DROP TABLE TempResults;
Use the sub‑set of commands listed above to complete the following tasks. Write each statement on a separate line.
Course with the columns:RoomNumber (type VARCHAR(10)) to the Course table.idxcoursename on the CourseName column.idxcoursename.Course table.| Aspect | DDL | DML |
|---|---|---|
| Purpose | Define or modify database structure. | Manipulate the data stored in that structure. |
| Typical Commands | CREATE, ALTER, DROP, TRUNCATE, RENAME. | SELECT, INSERT, UPDATE, DELETE. |
| Transaction Control | Usually auto‑committed; cannot be rolled back in many DBMS. | Can be committed or rolled back as part of a transaction. |
| Effect on Schema | Changes the schema (metadata). | Does not change the schema. |
DROP TABLE accidentally – always double‑check the table name.NOT NULL without a default value on a populated table – will cause an error.