Database Theory
Database Theory
Data Models in DBMS: Data models specify how data is linked to one another, as well as how it is handled
and stored within the system.
Database Users
A Database User is defined as a person who interacts with data daily, updating,
reading, and modifying the given data. Database users can access and retrieve data
from the database through the Database Management System (DBMS) applications
and interfaces.
Database Administrators (DBAs) have full control of the database and they are sometimes
known as the super-users of the database.
BINARY(Size) It is equal to CHAR() but stores binary byte strings. Its size
parameter specifies the column length in the bytes. Default is 1.
VARBINARY(Size) It is equal to VARCHAR() but stores binary byte strings. Its size
parameter specifies the maximum column length in bytes.
INT(size) It is used for the integer value. Its signed range varies
from -2147483648 to 2147483647 and unsigned range
varies from 0 to 4294967295. The size parameter
specifies the max display width that is 255.
Creating Table: Tables are created using the CREATE TABLE command.
Create table in SQL
column1 datatype,
column2 datatype,
);
SQL Commands
Data Definition Language (DDL) Commands
What is DDL?
DDL, which stands for Data Definition Language, is a subset of SQL (Structured Query
Language) commands used to define and modify the database structure. These commands are
used to create, alter, and delete database objects like tables, indexes, and schemas. The primary
DDL commands in SQL include:
1. CREATE: This command is used to create a new database object. For example,
creating a new table, a view, or a database.
Syntax for creating a table: CREATE TABLE table_name (column1 datatype, column2
datatype, ...);
2. ALTER: This command is used to modify an existing database object, such as adding,
deleting, or modifying columns in an existing table.
Syntax for adding a column in a table: ALTER TABLE table_name ADD column_name
datatype;
Syntax for modifying a column in a table: ALTER TABLE table_name MODIFY
COLUMN column_name datatype;
3. DROP: This command is used to delete an existing database object like a table, a view,
or other objects.
Syntax for dropping a table: DROP TABLE table_name;
4. TRUNCATE: This command is used to delete all data from a table, but the structure of
the table remains. It’s a fast way to clear large data from a table.
Syntax: TRUNCATE TABLE table_name;
5. COMMENT: Used to add comments to the data dictionary.
Syntax: COMMENT ON TABLE table_name IS 'This is a comment.';
6. RENAME: Used to rename an existing database object.
Syntax: RENAME TABLE old_table_name TO new_table_name;
Data Manipulation Language (DML) Commands in SQL
Data Manipulation Language (DML) is a subset of SQL commands used for adding (inserting),
deleting, and modifying (updating) data in a database. DML commands are crucial for
managing the data within the tables of a database. The primary DML commands in SQL
include:
1. GRANT: This command is used to give users access privileges to the database. These
privileges can include the ability to select, insert, update, delete, and so on, over
database objects like tables and views.
Syntax: GRANT privilege_name ON object_name TO user_name;
For example, GRANT SELECT ON employees TO user123; gives user123 the permission to
read data from the employees table.
2. REVOKE: This command is used to remove previously granted access privileges from
a user.
Syntax: REVOKE privilege_name ON object_name FROM user_name;
For example, REVOKE SELECT ON employees FROM user123; would remove user123‘s
permission to read data from the employees table.