Database Design-Construction Flashcards
(14 cards)
What are the SQL commands to create and delete a database?
CREATE DATABASE database_name; and DROP DATABASE database_name;
Write the basic syntax for creating a table with primary and foreign key constraints.
CREATE TABLE table_name (column1 datatype constraint, column2 datatype constraint, CONSTRAINT pk_table_name PRIMARY KEY (column1), CONSTRAINT fk_column2 FOREIGN KEY (column2) REFERENCES parent_table(parent_column));
What are the 4 main categories of SQL data types and give 2 examples of each?
- Numerical: Int, Float, Double 2. String: Varchar(n), Text 3. Date-Time: Date, Time, Datetime, Timestamp 4. Booleans: Boolean
When would you use VARCHAR(n) vs TEXT for string data?
VARCHAR(n) for fixed-length strings with known maximum length (more efficient, allows indexing). TEXT for variable-length content with unknown size (emails, descriptions, articles)
What does the PRIMARY KEY constraint enforce and what are its two key requirements?
Ensures the column can only hold Unique and non-null values. Requirements: 1. Values must be unique across all rows, 2. Cannot contain NULL values
What does a FOREIGN KEY constraint do and how does it maintain data integrity?
Links tables together and ensures the column can only contain values that exist in the referenced column of the linked table, preventing orphaned records and maintaining referential integrity
Compare these constraints: NOT NULL, UNIQUE, DEFAULT, CHECK. What does each enforce?
NOT NULL: No empty values. UNIQUE: No duplicate values (but allows NULL). DEFAULT: Sets automatic value if none provided. CHECK: Enforces custom rules/conditions on column values
Write a CREATE TABLE statement with a CHECK constraint that ensures age is between 18 and 65.
CREATE TABLE IF NOT EXISTS employees (age INT CHECK (age >= 18 AND age <= 65), name VARCHAR(50));
What command shows the structure of an existing table?
DESC table_name
Write the SQL to add an email column (VARCHAR 100, NOT NULL) to an existing ‘users’ table after the ‘name’ column.
ALTER TABLE users ADD COLUMN email VARCHAR(100) NOT NULL AFTER name;
What’s the advantage of combining multiple ALTER operations? Write an example.
More efficient than separate commands. Example: ALTER TABLE Employees DROP COLUMN MiddleName, ADD COLUMN Email VARCHAR(100) NOT NULL, MODIFY COLUMN Department VARCHAR(50) DEFAULT ‘General’;
What are the 4 main ALTER TABLE operations and their basic syntax?
- ADD: ADD COLUMN name datatype 2. DROP: DROP COLUMN name 3. MODIFY: MODIFY COLUMN name new_datatype 4. RENAME: RENAME COLUMN old_name TO new_name
You’re creating a ‘products’ table with: product_id, name, price, category. Which constraints would you apply and why?
product_id: PRIMARY KEY (unique identifier), name: NOT NULL + UNIQUE (must exist, no duplicates), price: NOT NULL + CHECK (price > 0), category: DEFAULT ‘General’ (fallback value)
Design tables for a library system: books and authors (many-to-many relationship). Show the table structures with appropriate constraints.
CREATE TABLE authors (author_id INT PRIMARY KEY, name VARCHAR(100) NOT NULL); CREATE TABLE books (book_id INT PRIMARY KEY, title VARCHAR(200) NOT NULL); CREATE TABLE book_authors (book_id INT, author_id INT, FOREIGN KEY (book_id) REFERENCES books(book_id), FOREIGN KEY (author_id) REFERENCES authors(author_id));