Database Design/Construction Commands Flashcards
(32 cards)
What is the goal of SQL design-construction commands?
To define and modify the database schema.
What SQL command creates a new database?
CREATE DATABASE database_name;
What SQL command deletes a database?
DROP DATABASE database_name;
What is the basic syntax to create a table?
CREATE TABLE table_name (column1 datatype constraint, …);
What is the purpose of constraints in table creation?
To enforce data validity rules like uniqueness, non-null values, or references.
How do you define a primary key in CREATE TABLE?
CONSTRAINT pk_name PRIMARY KEY (column_name)
How do you define a foreign key in CREATE TABLE?
CONSTRAINT fk_name FOREIGN KEY (column_name) REFERENCES parent_table(parent_column)
List four major categories of SQL data types.
Numerical, String, Date-Time, Boolean.
Give examples of SQL string data types.
VARCHAR(n), TEXT
What is the BOOLEAN type used for?
To store TRUE or FALSE values.
What does the PRIMARY KEY constraint enforce?
That column values are unique and not null.
What does the FOREIGN KEY constraint do?
It ensures values in the column exist in the referenced table’s primary key.
What does NOT NULL constraint enforce?
That a column cannot have NULL values.
What does the UNIQUE constraint enforce?
That no duplicate values exist in the column.
What does the DEFAULT constraint do?
Assigns a default value if no value is provided.
What does the CHECK constraint do?
Ensures a column value satisfies a given condition.
Give a syntax example using DEFAULT.
column_name datatype DEFAULT value
Give a syntax example using CHECK.
column_name datatype CHECK (condition)
What command shows the structure of a table?
DESC table_name;
What command adds a column to a table?
ALTER TABLE table_name ADD column_name datatype constraint;
How do you add a column at a specific position?
Use AFTER keyword in ADD COLUMN: … ADD COLUMN name AFTER existing_column;
What command deletes a column?
ALTER TABLE table_name DROP column_name;
What command modifies a column’s type or constraint?
ALTER TABLE table_name MODIFY COLUMN column_name new_type [constraint];
What command renames a column?
ALTER TABLE table_name RENAME COLUMN old_name TO new_name;