0% found this document useful (0 votes)
0 views6 pages

SQL Day_2

The document provides an overview of SQL data types, including numeric, character string, date and time, binary, boolean, and special data types, along with practical examples of defining tables and inserting data. It also categorizes SQL commands into Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL), and Data Query Language (DQL), detailing their functionalities and providing code examples. Understanding these concepts is essential for effective database design and management.

Uploaded by

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

SQL Day_2

The document provides an overview of SQL data types, including numeric, character string, date and time, binary, boolean, and special data types, along with practical examples of defining tables and inserting data. It also categorizes SQL commands into Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Transaction Control Language (TCL), and Data Query Language (DQL), detailing their functionalities and providing code examples. Understanding these concepts is essential for effective database design and management.

Uploaded by

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

SQL Data Types

In SQL, data types define the kind of data that can be stored in a column. Understanding data types is crucial for
designing a database that efficiently stores and processes data. Here’s an overview of the most commonly used
SQL data types:

1. Numeric Data Types


These data types are used to store numbers, both integers and floating-point numbers.

INT : Stores whole numbers (integers).


Example: INT can store values like 42 , -7 , 123456 .
Use Case: Storing counts, ages, or any data that doesn't require decimal points.

FLOAT : Stores floating-point numbers, which are numbers that have a decimal point.
Example: FLOAT can store values like 3.14 , -0.001 , 12345.6789 .
Use Case: Storing measurements, precise calculations, or data with fractional components.

DECIMAL(p, s) : Stores fixed-point numbers where p is the precision (total number of digits) and s is the
scale (number of digits to the right of the decimal point).
Example: DECIMAL(5, 2) can store values like 123.45 , -678.90 .
Use Case: Storing financial data where precision is crucial, like prices or monetary amounts.

2. Character String Data Types


These data types are used to store text data.

CHAR(n) : Stores fixed-length strings. The n specifies the exact length of the string.
Example: CHAR(10) stores exactly 10 characters. If the input is shorter, it’s padded with spaces.
Use Case: Storing data with a fixed format like product codes, phone numbers, or country codes.

VARCHAR(n) : Stores variable-length strings. The n specifies the maximum length.


Example: VARCHAR(255) can store any string up to 255 characters long.
Use Case: Storing names, emails, addresses, or any text that varies in length.

3. Date and Time Data Types


These data types are used to store dates, times, and timestamps.

DATE : Stores a date in the format YYYY-MM-DD .


Example: DATE can store values like 2024-08-27 , 1990-01-01 .
Use Case: Storing birthdates, event dates, or any data where only the date is required.

TIME : Stores a time in the format HH:MI:SS .


Example: TIME can store values like 14:30:00 , 08:15:45 .
Use Case: Storing time of day, like office hours or appointment times.

DATETIME : Stores both date and time in the format YYYY-MM-DD HH:MI:SS .
Example: DATETIME can store values like 2024-08-27 14:30:00 .
Use Case: Storing timestamps, such as when an order was placed or when a user logged in.
TIMESTAMP : Similar to DATETIME , but also includes the capability to store timezone information and often
used to automatically track changes in data.
Example: TIMESTAMP can store values like 2024-08-27 14:30:00 .
Use Case: Tracking the exact moment an entry was created or modified.

4. Binary Data Types


These data types are used to store binary data, such as files or images.

BLOB (Binary Large Object) : Stores large binary data, like images, audio, or video files.
Example: BLOB can store binary data of varying sizes.
Use Case: Storing multimedia content directly in the database.

5. Boolean Data Type


BOOLEAN : Stores a value of TRUE or FALSE . (In some databases, it is represented as TINYINT with values
1 for TRUE and 0 for FALSE ).
Example: BOOLEAN can store values like TRUE or FALSE .
Use Case: Storing flags, such as whether a user is active or an item is in stock.

6. Special Data Types


ENUM : Stores a list of predefined values. The user must select one value from the list.
Example: ENUM('Small', 'Medium', 'Large') can store one of these values.
Use Case: Storing data with limited, predefined options like clothing sizes, user roles, or statuses.

SET : Similar to ENUM , but allows multiple values to be selected from the predefined list.
Example: SET('A', 'B', 'C') can store any combination of these values.
Use Case: Storing multiple options that apply to a single field, like a product's available colors.

Practical Examples
1. Defining a Table with Various Data Types :

CREATE TABLE employees (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
birthdate DATE,
hire_date DATETIME,
salary DECIMAL(10, 2),
is_active BOOLEAN
);

2. Inserting Data into the Table :

INSERT INTO employees (name, birthdate, hire_date, salary, is_active)


VALUES ('John Doe', '1985-09-15', '2024-08-01 09:00:00', 55000.00, TRUE);

Data Types are Fundamental : They define the kind of data that can be stored in each column, influencing
how data is stored, retrieved, and processed.
Choosing the Right Data Type : Selecting appropriate data types improves database efficiency, accuracy,
and ensures data integrity.

Understanding and correctly using SQL data types is crucial for database design and management, ensuring
that your database can efficiently handle the data it needs to store.

Types of SQL Commands:


SQL commands are categorized into several types based on their functionality. Each category is designed to
handle specific tasks related to managing and interacting with a database. Below are the primary types of SQL
commands:

1. Data Definition Language (DDL)


DDL commands are used to define and manage database structures like tables, indexes, and schemas. These
commands directly affect the database schema and structure.

CREATE : Creates new database objects like tables, indexes, views, or databases.
Example: CREATE TABLE employees (id INT, name VARCHAR(100));

ALTER : Modifies an existing database object, such as adding a new column to a table or changing its
structure.
Example: ALTER TABLE employees ADD COLUMN birthdate DATE;

DROP : Deletes an existing database object like a table, view, or database.


Example: DROP TABLE employees;

TRUNCATE : Removes all records from a table but keeps the table structure intact.
Example: TRUNCATE TABLE employees;

2. Data Manipulation Language (DML)


DML commands are used to manipulate the data stored in the database. These commands are crucial for
managing the actual data in the database tables.

SELECT : Retrieves data from one or more tables.


Example: SELECT * FROM employees;

INSERT : Adds new records (rows) to a table.


Example: INSERT INTO employees (name, birthdate) VALUES ('John Doe', '1985-09-15');

UPDATE : Modifies existing records in a table.


Example: UPDATE employees SET name = 'Jane Doe' WHERE id = 1;

DELETE : Removes records from a table.


Example: DELETE FROM employees WHERE id = 1;

3. Data Control Language (DCL)


DCL commands are used to control access to data within the database. They manage permissions and security
for database objects.

GRANT : Grants specific privileges to users or roles.


Example: GRANT SELECT ON employees TO user_name;
REVOKE : Removes previously granted privileges from users or roles.
Example: REVOKE SELECT ON employees FROM user_name;

4. Transaction Control Language (TCL)


TCL commands are used to manage transactions within a database. They ensure that a series of SQL operations
are executed as a single unit, maintaining the integrity of the database.

COMMIT : Saves all changes made during the current transaction to the database.
Example: COMMIT;

ROLLBACK : Undoes all changes made during the current transaction.


Example: ROLLBACK;

SAVEPOINT : Sets a point within a transaction to which you can later roll back.
Example: SAVEPOINT savepoint_name;

SET TRANSACTION : Sets the properties of the current transaction, such as its isolation level.
Example: SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;

5. Data Query Language (DQL)


While technically part of DML, the SELECT command is often considered a separate category called DQL due
to its importance in querying the database.

SELECT : Retrieves data from the database based on specified criteria.


Example: SELECT name, birthdate FROM employees WHERE id = 1;

Summary
DDL: Focuses on defining and altering database structures.
DML: Deals with data manipulation, including inserting, updating, and deleting records.
DCL: Manages access control and security of database objects.
TCL: Controls transactions to maintain database consistency and integrity.
DQL: Primarily concerns data retrieval using the SELECT statement.

These commands form the core of SQL operations, allowing you to create, manage, and interact with a
relational database effectively.

Example with code:


Here are examples of SQL commands, categorized by their types, with corresponding SQL code snippets.

1. Data Definition Language (DDL)


CREATE : Creates a new table called employees .

CREATE TABLE employees (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
birthdate DATE,
salary DECIMAL(10, 2)
);

ALTER : Adds a new column email to the employees table.


ALTER : Adds a new column email to the employees table.

ALTER TABLE employees ADD COLUMN email VARCHAR(100);

DROP : Deletes the employees table from the database.

DROP TABLE employees;

TRUNCATE : Removes all records from the employees table but keeps the table structure.

TRUNCATE TABLE employees;

2. Data Manipulation Language (DML)


INSERT : Adds a new record to the employees table.

INSERT INTO employees (name, birthdate, salary, email)


VALUES ('John Doe', '1985-09-15', 55000.00, '[email protected]');

SELECT : Retrieves all records from the employees table.

SELECT * FROM employees;

UPDATE : Modifies the salary of an employee with id = 1 .

UPDATE employees SET salary = 60000.00 WHERE id = 1;

DELETE : Removes the record of the employee with id = 1 .

DELETE FROM employees WHERE id = 1;

3. Data Control Language (DCL)


GRANT : Grants SELECT permission on the employees table to a user named user_name .

GRANT SELECT ON employees TO user_name;

REVOKE : Revokes SELECT permission on the employees table from the user user_name .

REVOKE SELECT ON employees FROM user_name;

4. Transaction Control Language (TCL)


START TRANSACTION : Begins a new transaction.

START TRANSACTION;

SAVEPOINT : Sets a savepoint named sp1 within the transaction.

SAVEPOINT sp1;

ROLLBACK : Rolls back to the sp1 savepoint.

ROLLBACK TO sp1;

COMMIT : Commits the transaction, saving all changes.

COMMIT;

5. Data Query Language (DQL)


SELECT : Retrieves the names and salaries of all employees earning more than 50,000 .
SELECT : Retrieves the names and salaries of all employees earning more than 50,000 .

SELECT name, salary FROM employees WHERE salary > 50000;

BY Pranjal Gajbhiye(AIE)

Happy Learning...
In [ ]:

You might also like