CHAPTER 26
Designing and modifying database structure.
Creating our employees table from scratch
CREATE TABLE employees (
emp_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
department VARCHAR(50) NOT NULL,
salary DECIMAL(10,2) NOT NULL DEFAULT 0.00,
manager_id INT,
hire_date DATE NOT NULL,
CONSTRAINT chk_salary CHECK (salary >= 0),
FOREIGN KEY (manager_id) REFERENCES employees(emp_id)
);| Constraint | Purpose |
|---|---|
| PRIMARY KEY | Unique + NOT NULL identifier for every row |
| NOT NULL | Column cannot be omitted or set to NULL |
| UNIQUE | All values in this column must be different across rows |
| DEFAULT | Value used automatically when INSERT omits this column |
| CHECK(condition) | INSERT/UPDATE fails if condition evaluates to false |
| FOREIGN KEY | Value must exist in the referenced table (referential integrity) |
| AUTO_INCREMENT | Database automatically assigns the next integer value |
SQL
ALTER TABLE employees ADD COLUMN phone VARCHAR(15); ALTER TABLE employees MODIFY COLUMN salary DECIMAL(12,2) NOT NULL; ALTER TABLE employees DROP COLUMN phone; ALTER TABLE employees RENAME COLUMN department TO dept;