0% found this document useful (0 votes)
3 views8 pages

Database Commands

The document provides a comprehensive overview of various SQL commands for managing databases, tables, indexes, views, constraints, stored routines, and data manipulation. It includes syntax and examples for commands such as CREATE, DROP, ALTER, INSERT, UPDATE, and DELETE. Additionally, it covers the use of primary keys, foreign keys, unique constraints, and check constraints in table management.

Uploaded by

ranatariq5044
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)
3 views8 pages

Database Commands

The document provides a comprehensive overview of various SQL commands for managing databases, tables, indexes, views, constraints, stored routines, and data manipulation. It includes syntax and examples for commands such as CREATE, DROP, ALTER, INSERT, UPDATE, and DELETE. Additionally, it covers the use of primary keys, foreign keys, unique constraints, and check constraints in table management.

Uploaded by

ranatariq5044
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/ 8

Database Commands

CREATE DATABASE

Use: Creates a new database


Syntax:

CREATE DATABASE [IF NOT EXISTS] database_name;

Example:

CREATE DATABASE IF NOT EXISTS my_database;

DROP DATABASE

Use: Deletes a database


Syntax:

DROP DATABASE [IF EXISTS] database_name;

Example:

DROP DATABASE IF EXISTS old_database;

Table Commands
CREATE TABLE

Use: Creates a new table


Syntax:

CREATE TABLE [IF NOT EXISTS] table_name (


column1 datatype constraints,
column2 datatype constraints,
...
[PRIMARY KEY (column_name)]
);

Example:

CREATE TABLE employees (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
salary DECIMAL(10,2)
);

ALTER TABLE

Use: Modifies an existing table structure

Add column:
ALTER TABLE table_name ADD COLUMN column_name datatype;

Example:

ALTER TABLE employees ADD COLUMN email VARCHAR(100);

Modify column:

ALTER TABLE table_name MODIFY COLUMN column_name new_datatype;

Example:

ALTER TABLE employees MODIFY COLUMN salary DECIMAL(12,2);

Drop column:

ALTER TABLE table_name DROP COLUMN column_name;

Example:

ALTER TABLE employees DROP COLUMN email;

Rename table:

RENAME TABLE old_name TO new_name;

Example:

RENAME TABLE employees TO staff;

DROP TABLE
Use: Deletes a table
Syntax:

DROP TABLE [IF EXISTS] table_name;

Example:

DROP TABLE IF EXISTS temp_employees;

Index Commands
CREATE INDEX

Use: Improves query performance on frequently queried columns


Syntax:

CREATE INDEX index_name ON table_name(column_name);

Example:

CREATE INDEX idx_email ON staff(email);

DROP INDEX

Use: Removes an index


Syntax:

DROP INDEX index_name ON table_name;

Example:

DROP INDEX idx_email ON staff;

View Commands
CREATE VIEW

Use: Creates a virtual table based on a query


Syntax:

CREATE VIEW view_name AS


SELECT columns
FROM table
WHERE condition;

Example:

CREATE VIEW high_earners AS


SELECT name, salary
FROM staff
WHERE salary > 100000;

DROP VIEW

Use: Deletes a view


Syntax:

DROP VIEW [IF EXISTS] view_name;

Example:

DROP VIEW IF EXISTS high_earners;

Constraint Commands
PRIMARY KEY

Use: Uniquely identifies each record


Syntax:

-- During table creation


CREATE TABLE table_name (
id INT PRIMARY KEY,
...
);

-- After table creation


ALTER TABLE table_name
ADD CONSTRAINT pk_name PRIMARY KEY (column);

Example:

ALTER TABLE staff


ADD CONSTRAINT pk_emp_id PRIMARY KEY (employee_id);
FOREIGN KEY

Use: Links tables together


Syntax:

ALTER TABLE child_table


ADD CONSTRAINT fk_name
FOREIGN KEY (child_column)
REFERENCES parent_table(parent_column)
[ON DELETE action] [ON UPDATE action];

Example:

ALTER TABLE staff


ADD CONSTRAINT fk_dept
FOREIGN KEY (department_id)
REFERENCES departments(id)
ON DELETE SET NULL;

UNIQUE

Use: Ensures all values in a column are different


Syntax:

ALTER TABLE table_name


ADD CONSTRAINT constraint_name UNIQUE (column);

Example:

ALTER TABLE staff


ADD CONSTRAINT unique_email UNIQUE (email);

CHECK

Use: Limits the value range in a column


Syntax:

ALTER TABLE table_name


ADD CONSTRAINT constraint_name CHECK (condition);
Example:

ALTER TABLE staff


ADD CONSTRAINT chk_salary CHECK (salary >= 0);

Stored Routines
Stored Procedure

Use: Reusable SQL code block


Syntax:

DELIMITER //
CREATE PROCEDURE procedure_name(parameters)
BEGIN
-- SQL statements
END //
DELIMITER ;

Example:

DELIMITER //
CREATE PROCEDURE GetEmployees(IN dept_id INT)
BEGIN
SELECT * FROM staff WHERE department_id = dept_id;
END //
DELIMITER ;

Stored Function

Use: Returns a single value


Syntax:

DELIMITER //
CREATE FUNCTION function_name(parameters)
RETURNS datatype
BEGIN
-- SQL statements
RETURN value;
END //
DELIMITER ;
Example:

DELIMITER //
CREATE FUNCTION GetFullName(emp_id INT)
RETURNS VARCHAR(100)
BEGIN
DECLARE full_name VARCHAR(100);
SELECT CONCAT(first_name, ' ', last_name) INTO full_name
FROM staff WHERE employee_id = emp_id;
RETURN full_name;
END //
DELIMITER ;

Data Manipulation
INSERT

Use: Adds new records to a table

Single row:

INSERT INTO table_name (column1, column2, ...)


VALUES (value1, value2, ...);

Example:

INSERT INTO staff (name, salary) VALUES ('John Doe', 50000);

Multiple rows:

INSERT INTO table_name (columns)


VALUES (values1), (values2), ...;

Example:

INSERT INTO staff (name, salary)


VALUES ('Jane Smith', 60000), ('Bob Johnson', 55000);

UPDATE

Use: Modifies existing records

Single record:
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

Example:

UPDATE staff SET salary = 65000 WHERE id = 5;

Multiple records:

UPDATE table_name
SET column = value
WHERE condition;

Example:

UPDATE staff SET department = 2 WHERE department = 3;

DELETE

Use: Removes records from a table

Specific records:

DELETE FROM table_name WHERE condition;

Example:

DELETE FROM staff WHERE id = 10;

All records:

DELETE FROM table_name;


-- OR (faster but cannot be rolled back)
TRUNCATE TABLE table_name;

Example:

TRUNCATE TABLE temp_staff;

You might also like