SQL Day_2
SQL Day_2
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:
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.
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.
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.
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.
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 :
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.
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;
TRUNCATE : Removes all records from a table but keeps the table structure intact.
Example: TRUNCATE TABLE employees;
COMMIT : Saves all changes made during the current transaction to the database.
Example: COMMIT;
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;
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.
TRUNCATE : Removes all records from the employees table but keeps the table structure.
REVOKE : Revokes SELECT permission on the employees table from the user user_name .
START TRANSACTION;
SAVEPOINT sp1;
ROLLBACK TO sp1;
COMMIT;
BY Pranjal Gajbhiye(AIE)
Happy Learning...
In [ ]: