DatabaseInterview
DatabaseInterview
SQL stands for Structured Query Language. It is a language used to interact with the database, i.e to
create a database, to create a table in the database, to retrieve data or update a table in the
database
What is a database?
A database is an organized collection of data, so that it can be easily accessed and managed.
yes It is true that SQL is a language, but it is not support the programming features like conditional
statements, for loops or if..else, it is command based language nd used to create delete the
database.
What is the difference between CHAR and VARCHAR2 data types in SQL?
Database:
A database is a collection of data that is organized, which is also called structured data.
Databases can contain any type of data, such as numbers, words, images, videos, and files.
2. ALTER
o Used to modify the structure of an existing object, such as adding or dropping
columns, renaming columns, or altering constraints.
Syntax:
sql
Copy code
ALTER TABLE table_name
ADD column_name datatype;
Example:
sql
Copy code
ALTER TABLE Employees
ADD age INT;
3. DROP
o Used to delete database objects, such as tables, views, or schemas. Once
dropped, the object and its data are permanently removed.
Syntax:
sql
Copy code
DROP TABLE table_name;
Example:
sql
Copy code
DROP TABLE Employees;
4. TRUNCATE
o Removes all rows from a table without logging individual row deletions. The
table structure remains intact.
Syntax:
sql
Copy code
TRUNCATE TABLE table_name;
Example:
sql
Copy code
TRUNCATE TABLE Employees;
5. RENAME
o Changes the name of an existing database object.
Syntax:
sql
Copy code
ALTER TABLE old_table_name
RENAME TO new_table_name;
Example:
sql
Copy code
ALTER TABLE Employees
RENAME TO Staff;
DML Commands in SQL
DML (Data Manipulation Language) is a subset of SQL commands used to manipulate the
data stored in database tables. These commands are focused on retrieving, inserting,
updating, and deleting data from tables.
2. INSERT
o Adds new rows (records) to a table.
Syntax:
sql
Copy code
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Example:
sql
Copy code
INSERT INTO Employees (id, name, salary)
VALUES (1, 'John Doe', 50000);
Effect: A new row is added to the Employees table.
3. UPDATE
o Modifies existing records in a table.
Syntax:
sql
Copy code
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Example:
sql
Copy code
UPDATE Employees
SET salary = 55000
WHERE id = 1;
Effect: The salary of the employee with id = 1 is updated to 55,000.
4. DELETE
o Removes rows from a table based on a condition.
Syntax:
sql
Copy code
DELETE FROM table_name
WHERE condition;
Example:
sql
Copy code
DELETE FROM Employees
WHERE id = 1;
Effect: The row with id = 1 is removed from the Employees table.
1. COMMIT
o Saves all the changes made in the current transaction to the database
permanently.
Syntax:
sql
Copy code
COMMIT;
Example:
sql
Copy code
BEGIN TRANSACTION;
INSERT INTO Employees (id, name, salary) VALUES (1, 'John Doe', 50000);
COMMIT;
Effect: The changes made by the INSERT statement are saved permanently.
2. ROLLBACK
o Undoes all the changes made in the current transaction. This is used to revert
changes in case of an error or when the operation needs to be canceled.
Syntax:
sql
Copy code
ROLLBACK;
Example:
sql
Copy code
BEGIN TRANSACTION;
UPDATE Employees SET salary = 60000 WHERE id = 1;
ROLLBACK;
Effect: The UPDATE operation is canceled, and the original salary remains unchanged.
1. Data Definition Language
DDL is the short name for Data Definition Language, which deals with database schemas and
descriptions, of how the data should reside in the database.
SQL statements:
CREATE: to create a database and its objects like (table, index, views, store
procedure, function, and triggers)
ALTER: alters the structure of the existing database
DROP: delete objects from the database
TRUNCATE: remove all records from a table, including all spaces allocated for the
records are removed
COMMENT: add comments to the data dictionary
RENAME: rename an object