0% found this document useful (0 votes)
5 views1 page

SQL Table Creation and Alteration Guide

The document outlines SQL commands for creating and modifying three tables: students, customers, and items. It includes operations such as adding columns, modifying constraints, and establishing foreign key relationships. Key modifications include setting unique constraints and renaming columns to maintain data integrity and structure.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

SQL Table Creation and Alteration Guide

The document outlines SQL commands for creating and modifying three tables: students, customers, and items. It includes operations such as adding columns, modifying constraints, and establishing foreign key relationships. Key modifications include setting unique constraints and renaming columns to maintain data integrity and structure.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

CREATE TABLE students(

id INT,
name VARCHAR(20)
);
ALTER TABLE students
ADD age INT;
ALTER TABLE students
ADD (collage VARCHAR(50), branch VARCHAR(50));
ALTER TABLE students
MODIFY (name NOT NULL);
ALTER TABLE students
DROP COLUMN collage;
ALTER TABLE students
MODIFY CHECK(age>18);
ALTER TABLE students
RENAME COLUMN branch TO deparment;

CREATE TABLE customers(


id INT,
name VARCHAR(20),
email VARCHAR(50)
);
ALTER TABLE customers
MODIFY (name VARCHAR(50));
ALTER TABLE customers
ADD (digi_wallet INT DEFAULT 0);
ALTER TABLE customers
ADD CONSTRAINT uq_email UNIQUE(email);

CREATE TABLE items(


id INT,
name VARCHAR(50),
price INT
);
ALTER TABLE items
ADD(customer_id INT);
ALTER TABLE customers
ADD CONSTRAINT unique_id UNIQUE (id);
ALTER TABLE items
ADD CONSTRAINT customer_fk FOREIGN KEY (customer_id) REFERENCES customers(id);

Common questions

Powered by AI

Introducing a FOREIGN KEY constraint on 'items(customer_id)' referencing 'customers(id)' enforces referential integrity between the tables. It ensures that any 'customer_id' in the 'items' table must match an existing 'id' in the 'customers' table or be NULL. This setup maintains consistent and valid relationships between orders (items) and the customers linked to them, preventing orphan records in 'items' .

The statement 'ALTER TABLE customers ADD (digi_wallet INT DEFAULT 0)' adds a new column 'digi_wallet' to the 'customers' table, setting the default value to 0. This means that any new customer record added without specifying a value for 'digi_wallet' will automatically have a value of 0. It simplifies data entry by providing a default state for digital wallet balances .

Changing the 'name' column from 'VARCHAR(20)' to 'VARCHAR(50)' in the 'customers' table increases the maximum allowable length for entries, accommodating longer names and potentially improving data accuracy. However, it may increase storage requirements and affect the performance of data retrieval if larger data sizes slow down operations, particularly in large datasets .

The statement 'ALTER TABLE students MODIFY (name NOT NULL)' changes the column 'name' in the 'students' table to not allow NULL values. This constraint ensures that every row must have a name value, preventing any operations that would result in a NULL name field. For existing data, any row with a NULL name would prevent the alteration, requiring cleanup or default values beforehand .

The CHECK constraint 'ALTER TABLE students MODIFY CHECK(age>18)' ensures that only students older than 18 can be entered into the table, enforcing business rules directly at the database level. This helps in maintaining compliance with policies or legal standards, automatically rejecting data entries that do not meet the specified condition .

Implementing a unique email constraint on the 'customers' table directly supports database normalization by removing data redundancies and promoting the single responsibility principle. It also facilitates user management by ensuring each user is uniquely identifiable by their email, simplifying authentication and communication while avoiding duplicate records and potential data inconsistencies .

Dropping the 'collage' column from the 'students' table results in the permanent loss of any data stored under that column, affecting both the historical data integrity and potentially any system functionalities dependent on that attribute. This decision should be preceded by a thorough data backup and analysis to mitigate any adverse effects on current systems utilizing that data .

Renaming a column from 'branch' to 'deparment' primarily affects the schema of the 'students' table by changing how that column is referenced in queries. Users must update any SQL queries or applications that interact with this table to use the new column name. Careful consideration is needed to ensure backward compatibility and avoid errors due to missing updates in dependent systems .

The addition of the 'unique_id' constraint as a UNIQUE key on the 'id' column in the 'customers' table ensures that each 'id' value is distinct across all records, thus maintaining entity integrity. This constraint prevents duplicate 'id' entries, which is essential for accurate identification and retrieval of customer records .

The constraint 'ALTER TABLE customers ADD CONSTRAINT uq_email UNIQUE(email)' ensures that no two rows in the 'customers' table can have the same email address. This uniqueness constraint is critical for maintaining accurate customer data, as it prevents duplicate entries based on email, thereby enhancing the integrity and usability of the table for operations like email-based communications .

You might also like