Table Creation A table is a collection of related data organized in rows and columns. Each table should have a unique name and must be defined with columns that specify data types. CREATE TABLE: Command to create a table. INT, VARCHAR(50): Data types, where INT is for integers and VARCHAR(50) allows up to 50 characters. PRIMARY KEY: Ensures that the student_id is unique for each student and is not null.
Primary Key and Foreign Key Concepts Primary Key: A primary key is a unique identifier for each record in a table. It enforces uniqueness and ensures no NULL values. Foreign Key: A foreign key links two tables together. It is a field (or fields) that references the primary key of another table, maintaining referential integrity. The student_id in Enrollments is a foreign key referencing the primary key in the Students table. The course_id in Enrollments references the Courses table.
Inserting Data into a Table The INSERT INTO command is used to add data into a table. This adds a new student to the Students table and a new course to the Courses table.
Updating Data in a Table The UPDATE command is used to modify existing records in a table. This updates John Doe's email where student_id equals 1.
Deleting Data from a Table The DELETE command is used to remove records from a table. This deletes the record where student_id equals 1 from the Students table.
Selecting Data from a Table The SELECT statement is used to query data from a table. SELECT *: Retrieves all columns. SELECT column_name : Retrieves specific columns.
Deleting a Table To remove an entire table and its data, use the DROP statement. This deletes the Students table completely from the database.
Altering a Table To modify an existing table (such as adding a new column), use the ALTER statement. This adds a new column date_of_birth to the Students table.