Author Archives: Febronei

100 Days of SQL

sql

Day 45 – SQL Injection

SQL injection is a type of security vulnerability that occurs when an attacker is able to inject malicious SQL code into an application’s database query, either through user input or by other means. This can allow the attacker to access or modify data in the database, or even execute arbitrary code on the server hosting the database.

The vulnerability typically arises when an application fails to properly validate user input or sanitize it before using it in a SQL query. For example, if an application accepts user input for a search query and constructs a SQL query based on that input without validating or sanitizing it, an attacker could insert their own SQL code as part of the input and execute it in the context of the query.

Some common techniques used by attackers to exploit SQL injection vulnerabilities include:

  • Commenting out parts of the original SQL query and adding their own code
  • Using UNION statements to combine the results of different queries
  • Using subqueries to retrieve or modify data in the database
  • Using time delays to obfuscate the attack and avoid detection

To prevent SQL injection vulnerabilities, it is important to use secure coding practices, such as:

  • Using parameterized queries, prepared statements, or stored procedures to sanitize user input
  • Validating input data to ensure it matches the expected format and does not contain unexpected characters or values
  • Limiting the privileges of the database user used by the application to only the necessary operations and data access
  • Regularly updating and patching the database software and application code to fix known vulnerabilities

In summary, SQL injection is a serious security vulnerability that can have significant consequences if exploited. It is important for developers to be aware of this vulnerability and take steps to prevent it in their applications.

100 Days of SQL

sql

Day 43 – SQL Dates

In SQL, dates are a common data type used to represent specific points in time. SQL supports several date-related data types, including DATE, TIME, DATETIME, TIMESTAMP, and INTERVAL.

The DATE data type represents a specific calendar date, without any time zone information. The format for a DATE value is ‘YYYY-MM-DD’, where YYYY is the year, MM is the month (1-12), and DD is the day of the month.

The TIME data type represents a specific time of day, without any date or time zone information. The format for a TIME value is ‘HH:MM:SS’, where HH is the hour (0-23), MM is the minute (0-59), and SS is the second (0-59).

The DATETIME data type represents a specific date and time, without any time zone information. The format for a DATETIME value is ‘YYYY-MM-DD HH:MM:SS’, where YYYY is the year, MM is the month (1-12), DD is the day of the month, HH is the hour (0-23), MM is the minute (0-59), and SS is the second (0-59).

The TIMESTAMP data type is similar to the DATETIME data type, but it also includes time zone information. The format for a TIMESTAMP value is ‘YYYY-MM-DD HH:MM:SS.SSSSSS +HH:MM’, where the first part is the same as a DATETIME value, and the second part represents the time zone offset.

The INTERVAL data type represents a duration of time, such as a number of years, months, days, hours, minutes, or seconds. The format for an INTERVAL value is ‘interval value unit’, where interval value is a number and unit is a time unit, such as YEAR, MONTH, DAY, HOUR, MINUTE, or SECOND.

For example, let’s say we have a table named “orders” with columns “order_id”, “customer_id”, “order_date”, and “order_total”. We want to insert a new row into the table with an order date of March 30, 2023. The INSERT statement for this row would be as follows:


INSERT INTO orders (order_id, customer_id, order_date, order_total)
VALUES (1, 123, '2023-03-30', 100.00);

In the above example, we have used the DATE data type to represent the order date as ‘2023-03-30’.

It is important to note that date and time values in SQL are subject to the rules of the underlying operating system and may vary depending on the database system being used. It is also important to ensure that date and time values are handled correctly when performing calculations or comparisons in SQL.

100 Days of SQL

sql

Day 42 – AUTO INCREMENT Field

In SQL, an AUTO INCREMENT field is a column that automatically generates a unique numeric value when a new row is inserted into a table. The AUTO INCREMENT field is often used as a primary key because it guarantees the uniqueness of each row in the table.

The syntax for defining an AUTO INCREMENT field varies depending on the database system being used. In MySQL, for example, the AUTO INCREMENT field is defined as follows:


CREATE TABLE table_name (
   id INT AUTO_INCREMENT PRIMARY KEY,
   column1 datatype,
   column2 datatype,
   .....
);

In the above syntax, id is the name of the AUTO INCREMENT field, INT is the data type of the field, AUTO_INCREMENT is the keyword that tells MySQL to generate a new value for the field automatically, and PRIMARY KEY specifies that the field is the primary key for the table.

When a new row is inserted into the table, the AUTO INCREMENT field is automatically assigned the next available integer value. Each subsequent row inserted into the table will have a unique value for the AUTO INCREMENT field.

For example, let’s say we have a table named “employees” with columns “id”, “name”, and “salary”. We want to define an AUTO INCREMENT field for the “id” column. The CREATE TABLE statement for this table would be as follows:


CREATE TABLE employees (
   id INT AUTO_INCREMENT PRIMARY KEY,
   name VARCHAR(50),
   salary DECIMAL(10,2)
);

In the above example, we have defined an AUTO INCREMENT field for the “id” column of the “employees” table. When a new row is inserted into the table, the “id” field will be automatically assigned the next available integer value.

It is important to note that not all database systems support the AUTO INCREMENT field syntax. In Microsoft SQL Server, for example, the equivalent functionality is provided by the IDENTITY property, and in Oracle, it is provided by the SEQUENCE object.

100 Days of SQL

sql

Day 41 – SQL CREATE INDEX Statement

In SQL, an index is a database object that improves the performance of SQL queries by allowing them to retrieve data more efficiently. An index is created on one or more columns of a table to help the database engine quickly find the rows that match a search condition. The CREATE INDEX statement is used to create an index on one or more columns of a table.

The syntax for creating an index on a table is as follows:


CREATE [UNIQUE] INDEX index_name
ON table_name (column1 [, column2, ...]);

In the above syntax, index_name is the name of the index, table_name is the name of the table on which the index is being created, column1, column2, etc. are the columns on which the index is being created.

If you specify UNIQUE keyword in the CREATE INDEX statement, the index will enforce uniqueness on the specified columns, i.e., it will not allow duplicate values in the indexed columns.

For example, let’s say we have a table named “employees” with columns “id”, “first_name”, “last_name”, and “department”. We want to create an index on the “last_name” column to improve the performance of queries that search for employees by last name. The CREATE INDEX statement for this index would be as follows:


CREATE INDEX idx_last_name
ON employees (last_name);

In the above example, we have created an index named “idx_last_name” on the “last_name” column of the “employees” table.

It is important to note that creating an index may improve the performance of queries that search for data in the indexed column(s), but it may also slow down the performance of queries that update or insert data into the table. Therefore, it is recommended to create indexes on columns that are frequently used in WHERE clauses of SELECT statements and not on columns that are frequently updated.

100 Days of SQL

sql

Day 40 – SQL DEFAULT Constraint

In SQL, a DEFAULT constraint is used to set a default value for a column when a new row is inserted into the table and the value for that column is not provided. The DEFAULT constraint is defined at the time of creating the table or added later using the ALTER TABLE statement.

The syntax for defining a DEFAULT constraint when creating a table is as follows:


CREATE TABLE table_name (
   column1 datatype DEFAULT default_value,
   column2 datatype,
   column3 datatype,
   .....
);

In the above syntax, column1 is the name of the column on which the DEFAULT constraint is defined, datatype is the data type of the column, and default_value is the value that will be assigned to the column if no value is specified during insertion.

The syntax for adding a DEFAULT constraint to an existing table is as follows:


ALTER TABLE table_name
ALTER COLUMN column_name SET DEFAULT default_value;

In the above syntax, column_name is the name of the column on which the DEFAULT constraint is being added, and default_value is the value that will be assigned to the column if no value is specified during insertion.

For example, let’s say we have a table named “employees” with a column “hire_date”, and we want to set the default value of hire date to the current date when a new row is inserted into the table. We can define a DEFAULT constraint on the hire_date column as follows:


CREATE TABLE employees (
   id INT PRIMARY KEY,
   name VARCHAR(50),
   hire_date DATE DEFAULT CURRENT_DATE,
   salary DECIMAL(10,2)
);

In the above example, we have defined a DEFAULT constraint on the hire_date column to set the default value to the current date if no value is provided during insertion.

When a DEFAULT constraint is defined, if no value is specified during insertion, the default value will be assigned to the column. If a value is provided during insertion, the specified value will be assigned to the column instead of the default value.

100 Days of SQL

sql

Day 39 – SQL CHECK Constraint

In SQL, a CHECK constraint is used to ensure that the values in a column meet a specified condition or set of conditions. The CHECK constraint is defined at the time of creating the table or added later using the ALTER TABLE statement.

The syntax for defining a CHECK constraint when creating a table is as follows:


CREATE TABLE table_name (
   column1 datatype CHECK (condition),
   column2 datatype,
   column3 datatype,
   .....
);

In the above syntax, column1 is the name of the column on which the CHECK constraint is defined, datatype is the data type of the column, and condition is the expression that must be evaluated to TRUE for the value to be inserted or updated in the column.

The syntax for adding a CHECK constraint to an existing table is as follows:


ALTER TABLE table_name
ADD CONSTRAINT constraint_name CHECK (condition);

In the above syntax, constraint_name is the name of the CHECK constraint, and condition is the expression that must be evaluated to TRUE for the value to be inserted or updated in the column.

For example, let’s say we have a table named “employees” with a column “age”, and we want to ensure that the age of an employee is not less than 18 and not greater than 60. We can define a CHECK constraint on the age column as follows:


CREATE TABLE employees (
   id INT PRIMARY KEY,
   name VARCHAR(50),
   age INT CHECK (age >= 18 AND age <= 60),
   salary DECIMAL(10,2)
);

In the above example, we have defined a CHECK constraint on the age column to ensure that the age is between 18 and 60 (inclusive).

When a CHECK constraint is violated, the database will not allow the insertion or update of data in the column that violates the constraint.

100 Days of SQL

sql

Day 38 – SQL FOREIGN KEY Constraint

In SQL, a FOREIGN KEY constraint is used to link two tables together based on a relationship between a column(s) in one table and the primary key column(s) in another table. The FOREIGN KEY constraint ensures that the values in the referencing column(s) in the child table exist in the referenced column(s) of the parent table or are null.

The syntax for defining a FOREIGN KEY constraint when creating a table is as follows:


CREATE TABLE table_name1 (
   column1 datatype PRIMARY KEY,
   column2 datatype,
   column3 datatype,
   .....
   FOREIGN KEY (column2) REFERENCES table_name2(column1)
);

In the above syntax, table_name1 is the child table, column2 is the referencing column in the child table, table_name2 is the parent table, and column1 is the referenced column in the parent table.

The syntax for adding a FOREIGN KEY constraint to an existing table is as follows:


ALTER TABLE table_name1
ADD CONSTRAINT fk_name FOREIGN KEY (column2) REFERENCES table_name2(column1);

In the above syntax, fk_name is the name of the FOREIGN KEY constraint.

It is important to maintain data integrity while creating FOREIGN KEY constraints. The referenced column(s) in the parent table must be defined as a primary key or a unique constraint. Also, the datatype and size of the referencing column(s) in the child table must match the datatype and size of the referenced column(s) in the parent table. If the FOREIGN KEY constraint is violated, the database will not allow the insertion, update, or deletion of data in the child table.

100 Days of SQL

sql

Day 37 – SQL PRIMARY KEY Constraint

In SQL, the PRIMARY KEY constraint is used to specify a column or a set of columns that uniquely identify each row in a table. The primary key constraint ensures that the values in the specified column(s) are unique and cannot be null.

A table can have only one primary key, and it can be defined at the time of creating the table or added later using the ALTER TABLE statement. When a primary key is defined on a column, the database automatically creates an index on that column for faster searching and sorting of data.

The syntax for defining a primary key constraint when creating a table is as follows:


CREATE TABLE table_name (
   column1 datatype PRIMARY KEY,
   column2 datatype,
   column3 datatype,
   .....
);

The syntax for adding a primary key constraint to an existing table is as follows:


ALTER TABLE table_name
ADD PRIMARY KEY (column1, column2, ...);

In the above syntax, column1, column2, etc. are the names of the columns that you want to include in the primary key.

It is important to choose the right column(s) to define as the primary key because the primary key will be used as a reference by other tables, and changing the primary key can have significant impacts on the database’s performance and data integrity.

100 Days of SQL

sql

Day 36 – SQL UNIQUE constraint

SQL UNIQUE constraint is used to ensure that each value in a column or a combination of columns is unique across all rows in a table. When you define a column with the UNIQUE constraint, it means that the values in that column must be unique for every row in the table.

Here’s an example of how to create a table with a UNIQUE constraint on a column:


CREATE TABLE students (
   id INT NOT NULL,
   name VARCHAR(50) NOT NULL,
   email VARCHAR(100) UNIQUE,
   age INT
);

In this example, the “email” column has the UNIQUE constraint, which means that every value in this column must be unique across all rows in the “students” table. The “id”, “name”, and “age” columns do not have a UNIQUE constraint and can contain duplicate values.

If you try to insert a row into the “students” table with a value that already exists in the “email” column, SQL Server will return an error. For example, the following statement would fail:


INSERT INTO students (id, name, email, age)
VALUES (1, 'John', 'john@example.com', 25);

This is because the value “john@example.com” already exists in the “email” column, and it has a UNIQUE constraint. To avoid this error, you need to provide a unique value for the “email” column in the INSERT statement or update the existing row with a different value.

100 Days of SQL

sql

Day 35 – SQL NOT NULL Constraint

SQL NOT NULL constraint is used to ensure that a column in a table cannot have a NULL value. When you define a column with the NOT NULL constraint, it means that the column must have a value for every row in the table.

Here’s an example of how to create a table with a NOT NULL constraint on a column:


CREATE TABLE employees (
   id INT NOT NULL,
   name VARCHAR(50) NOT NULL,
   age INT,
   salary DECIMAL(10, 2) NOT NULL
);

In this example, the “id”, “name”, and “salary” columns have the NOT NULL constraint. This means that a value must be provided for these columns for every row inserted into the “employees” table. The “age” column does not have a NOT NULL constraint, which means that it can have a NULL value.

If you try to insert a row into the “employees” table without providing a value for a column with the NOT NULL constraint, SQL Server will return an error. For example, the following statement would fail:


INSERT INTO employees (id, name, salary)
VALUES (1, 'John', 5000.00);

This is because the “age” column does not have a value specified, and it has a NOT NULL constraint. To avoid this error, you need to provide a value for the “age” column in the INSERT statement or remove the NOT NULL constraint on the “age” column.