0% found this document useful (0 votes)
2 views3 pages

Lab 4

The document provides SQL commands for modifying database tables, including adding, dropping, modifying, and renaming columns. It also explains the use of the LIKE operator for pattern matching in SELECT statements, detailing the syntax and examples of wildcards. Additionally, it highlights variations in syntax for different SQL dialects, such as MySQL and PostgreSQL.

Uploaded by

66252azaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Lab 4

The document provides SQL commands for modifying database tables, including adding, dropping, modifying, and renaming columns. It also explains the use of the LIKE operator for pattern matching in SELECT statements, detailing the syntax and examples of wildcards. Additionally, it highlights variations in syntax for different SQL dialects, such as MySQL and PostgreSQL.

Uploaded by

66252azaz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Add Column
ALTER TABLE table_name
ADD column_name datatype;

Example:

ALTER TABLE employees


ADD birthdate DATE;

2. Drop Column
ALTER TABLE table_name
DROP COLUMN column_name;

Example:

ALTER TABLE employees


DROP COLUMN birthdate;

3. Modify Column Data Type


ALTER TABLE table_name
MODIFY COLUMN column_name new_datatype;

Example (MySQL):

ALTER TABLE employees


MODIFY COLUMN salary DECIMAL(10,2);

⚠️Note: In some SQL dialects like PostgreSQL, use ALTER COLUMN ... TYPE:

ALTER TABLE employees


ALTER COLUMN salary DECIMAL(10,2);

4. Rename Column
ALTER TABLE table_name
RENAME COLUMN old_name TO new_name;

Example (PostgreSQL / recent MySQL):

ALTER TABLE employees


RENAME COLUMN fullname TO full_name;

⚠️Note: For older MySQL versions, use CHANGE:


ALTER TABLE employees
CHANGE fullname full_name VARCHAR(255);

OR use this:

exec sp_rename 'employees.DOB' ,'dob','column';

5. LIKE is used in SQL for pattern matching, typically with the SELECT
statement.

Basic Syntax of LIKE

SELECT column_name
FROM table_name
WHERE column_name LIKE 'pattern';
Wildcards Used with LIKE
% → Matches any number of characters (including zero)

_ → Matches exactly one character

Examples:
1. Names that start with 'A':
SELECT * FROM employees
WHERE full_name LIKE 'A%';
2. Names that end with 'n':
SELECT * FROM employees
WHERE full_name LIKE '%n';
3. Names that contain 'ohn':
SELECT * FROM employees
WHERE full_name LIKE '%ohn%';
4. Names with exactly 5 letters:
SELECT * FROM employees
WHERE full_name LIKE '_____';
5. Email addresses from Gmail:
SELECT * FROM employees
WHERE email LIKE '%@gmail.com';

You might also like