SQL NOTES
SQL NOTES
language that can work in different computing environments. SQL has proved to be
the standard language as it allows programmers or developers to learn one set of
commands and use them to create, retrieve, alter, and or transfer data regardless of
whether they are working on the PC, a workstation, a mini, or a mainframe. In this
tutorial, you will learn about the introduction of the structured query language. or
handling database and database-related programming, programmers need to have
some medium or interface to particularize a set of commands or codes to deal with a
database or access the database's data. SQL(Structured Query Language) is a
standard database programming language for accessing and manipulating data in a
relational database.
SQL gives unique learning and database handling techniques on Structured Query
language and will help you make better command over the SQL queries and to deal
with these codes efficiently. Since SQL helps you to include database creation,
database or table deletion, fetching row data and modifying those data, etc., in
parallel, SQL makes things automatic and smooth for end-users to access and deal
with that application's data efficiently.
These are just a few examples of the many operations that can be performed using
SQL, and the specific set of commands and functionality may vary depending on the
specific relational database management system (RDBMS) being used.
QL syntax refers to the rules and guidelines defining how to correctly write SQL
statements. It includes the use of keywords, clauses, operators, and functions that
are used to query and manipulate data in a relational database.
SQL Clause
In SQL (Structured Query Language), a clause is a component of a SQL statement
that performs a specific function. For example, the SELECT clause specifies the
columns to be retrieved, the WHERE clause specifies a condition to filter rows, and
the ORDER BY clause sorts the result set.
On the other hand, an SQL statement is composed of several clauses, each with a
specific purpose and syntax. It is a complete command that is executed against a
database. An SQL statement can contain one or more clauses, depending on the
task performed. For example, a SELECT statement typically includes a SELECT
clause, a FROM clause, and possibly others such as WHERE, GROUP BY,
HAVING, and ORDER BY.
Some standard SQL clauses include:
SELECT Clause
The SELECT clause in SQL is used to specify the columns in a SELECT statement.
It is used to query a database and retrieve specific data from one or more tables in
the database. The syntax for the SELECT clause is as follows:
WHERE clause
The WHERE clause in SQL filters the rows returned by a SELECT, UPDATE, or
DELETE statement. The WHERE clause is used to specify a condition that must be
true for a row to be included in the result set or affected by the statement. The basic
syntax of a WHERE clause is:
Syntax:
AND/OR CLAUSE
The SQL AND and OR operators are used in the WHERE clause to combine multiple
conditions in a query. The AND operator returns rows that satisfy both conditions,
while the OR returns rows that satisfy either. The basic syntax of a WHERE clause
with AND and OR operators is:
Syntax:
ORDER BY Clause
The SELECT statement in SQL is used to retrieve data from one or more tables in a
database. The ORDER BY clause is used to sort the result set by one or more
columns. The basic syntax of a SELECT statement with an ORDER BY clause is:
Syntax:
Syntax:
UPDATE Clause
The UPDATE clause in SQL is used to modify existing data in a table. It is typically
used in conjunction with a SET clause to specify the new values for the columns and
a WHERE clause to identify the rows that should be updated. The basic syntax of
the UPDATE clause is:
Syntax:
The DELETE clause in SQL is used to delete existing rows of data from a table. It is
typically used in conjunction with a WHERE clause to specify the rows that should be
deleted. The basic syntax of the DELETE clause is:
Syntax:
SQL syntax has a set of rules that must be followed for a SQL statement to be valid.
Here are some of the most important rules to keep in mind when writing SQL
statements:
1. Numeric data types: It stores numeric values, such as integers and floating-
point numbers. Examples include INT, BIGINT, FLOAT, NUMERIC, and DECIMAL.
2. Character and string data types: It stores character strings, such as names
and addresses. Examples include CHAR, VARCHAR, and TEXT.
3. Date and time data types: It stores date and time values. Examples
include DATE, TIME, DATETIME, and TIMESTAMP.
4. Binary data types: It stores binary data, such as images and files. Examples
include BLOB, BINARY, and VARBINARY.
5. Boolean data types: It stores true/false values. Examples
include BOOL and BOOLEAN.
6. Enumerated data types: It stores a predefined set of values. Examples
include ENUM.
7. Array data types: It stores multiple values of the same data type. Examples
include ARRAY.
8. JSON data types: It stores JSON data. Examples include JSON and JSONB.
Here are some examples of common SQL data types and their uses:
DATABASE CONCEPT
Purpose of Database
A database should be a repository of data needed for an organization's data
processing. That data should be accurate, private, and protected from damage. It
should be accurate so that diverse applications with different data requirements can
employ the data. Different application programmers and various end-users have
different views upon data, which must be derived from a common overall data
structure. Their methods of searching and accessing data will be different.
In any given column of a table, all the items are of the same kind, whereas
items in different columns may not be of the same kind.
For a row, each column must have an atomic value, and also, for a row, a
column cannot have more than one value.
All rows of a relation are distinct.
The ordering of rows in a relationship is immaterial.
The column of a relation are assigned distinct names, and the ordering of
these columns is immaterial.
Degree: The number of attributes in a relation determines the degree of the relation.
A relation having three attributes is said to have a relation of degree 3.
Type Description
Data Definition Language (DDL) The SQL DDL offers commands for defining
relation schemas, deleting relations, creating
indexes, and modifying relation schemas.
Interactive data manipulation language DML includes query languages based on both
relational algebra and tuple relational calculus.
Embedded Data Manipulation The embedded form of SQL is designed for use
Language within general-purpose programs such as PL/1,
COBOL, Fortran, Pascal, or C.
View Definition The SQL DDL also includes commands for
defining views.
Authorization The SQL DDL includes commands for specifying
access rights to relations and views.
Integrity SQL provides some limited forms of integrity
checking. Future products and standards of SQL
will likely comprise enhanced features for integrity
checking.
Transaction Control SQL also includes commands for specifying the
beginning and end of transactions and commands
for controlling transaction processing.
Data Definition Language (DDL)
DDL is also used to define the length of data items. It may also specify the encoding
the program uses in the data items (binary, character, bits, string, etc.).
Although the DDL and DML are two distinct parts of the SQL language, in most SQL-
based DBMS products, the split is a conceptual one only. Usually, the DDL and DML
statements are submitted to the DBMS in the same way, and they can be freely
intermixed in both interactive SQL sessions and programmatic SQL applications. If a
program or user needs a table to store its temporary results, it can create the table,
populate it, manipulate the data, and then delete it. Again, this is a significant
advantage over earlier data models, in which the database structure was fixed when
the database was created.
After the database schema has been specified and the database has been created,
the data can be manipulated using a set of procedures that are expressed by a
particular language called a data manipulation language.
The basic syntax for creating a new database using SQL is:
Syntax
For example, to create a new database named "mydb", you would use the
following SQL statement:
Example:
Copy CodeCREATE DATABASE mydb;
When the above statement is executed, a new database with the specified
name is created. The new database will be empty and contain no tables. You
can then use other SQL statements to create tables, insert data, and perform
different operations on the new database.
Basic Syntax
The basic syntax for the ALTER DATABASE statement is as follows:
Basic Syntax:
Renaming a Database
One of the most common uses of the ALTER DATABASE statement is to change the
name of a database. The MODIFY NAME clause lets you easily change the database
name. For example, to rename a database from "old_db" to "new_db":
To change the properties of a database file, use the MODIFY FILE option:
The name specifies the name of the file, the size sets the size of the file in bytes, and
the FILE_ACCESS determines the access mode for the file (READ_ONLY,
READ_WRITE, or WRITE_ONLY). For example, to resize a file named "old_file" to
10MB, use the following statement:
Another common use for the ALTER DATABASE statement is setting a database to
single-user or restricted-user mode. You can accomplish this by using the SET
clause with the SINGLE_USER or RESTRICTED_USER option. Setting a database to
single-user mode limits access to one user at a time, while restricted-user mode
allows only members of db_owner or sysadmin roles. For example, to set a database
named "mydb" to single-user mode, use the following statement:
Using the COLLATE clause, you can also change the default collation of a database
with the ALTER DATABASE statement. Collation sets the rules for sorting and
comparing characters. For example, to change the collation of a database named
"mydb" to "utf8mb3_unicode_ci", use the following statement:
You can change the recovery model of a database with the ALTER
DATABASE statement. The recovery model sets the database's strategy for
recovering data in the event of a failure. The most commonly used recovery models
are "FULL" and "SIMPLE". The full recovery model maintains a complete set of
transaction logs, providing the most comprehensive data recovery. In contrast, the
simple recovery model keeps only the most recent transaction logs, providing less
comprehensive data recovery. To change the recovery model of a database named
"mydb" to "Simple," use the following statement:
Conclusion
The ALTER DATABASE statement is a powerful tool for modifying the properties of an
existing database. By understanding its basic syntax and some of the most common
options, you are well-equipped to manage your databases effectively.
This tutorial provides an overview of the most commonly used SQL commands to
create, alter, and manipulate tables and data in databases.
SQL CREATE TABLE is a command that creates a new table in the RDBMS
database, such as MySQL, SQL Server, Oracle, etc.
Syntax:
Example:
Learn how the ALTER TABLE statement in SQL streamlines your database
management by simplifying modifications to a table's structure without affecting its
data. This feature allows easy modification of table columns, constraints, and
indexes.
Syntax
The basic syntax for SQL ALTER TABLE is as follows:
You can use the DROP TABLE statement to delete a table from a database
permanently. This statement can be helpful if you no longer need the table's data or
want to begin fresh. This tutorial will guide you through how to use the DROP
TABLE statement, including its syntax and options.
The following SQL statements drop the tables tbl_logs and tbl_tmp:
Option Description
IF EXISTSIt checks table existence to prevent errors.
CASCADE It drops the table along with dependent objects, such as views, triggers, and stored procedures.
RESTRICT It prevents table deletion if dependent objects exist.
DROP TABLE with IF EXISTS
Before attempting to delete a table, use IF EXISTS to check if it exists first to prevent
errors.
Example:
The CASCADE option with the DROP TABLE statement will also drop any associated
objects like stored procedures, triggers, and views. This option can be helpful when
you want to delete the table and all its related objects.
Example:
The RESTRICT option prevents the DROP TABLE statement from dropping a table if any
dependent objects exist. This option is helpful if you are unsure whether there are
any dependent objects or do not want to delete them.
Example: