SQL and DBMS
SQL and DBMS
@himanshu_shekhar16
What is a database?
A database is described as an organized way of collection of DATA. It is the collection of schemes,
tables, queries, reports, views and other objects.
Syntax: CREATEDATABASEDatabaseName
or you can Create Database through Design/ Wizard form by right clicking on DATABASE option-
New Database.
What is DBMS?
A Database Management System (DBMS) is a program that controls creation, maintenance and use
of a database. DBMS can be termed as File Manager that manages data in a database rather than
saving it in file systems.
What is SQL?
Structured Query Language, also known as SQL, is a programming language designed for
managing Relational Database Management Systems (RDBMSs). SQL is an International
Organization for Standardization (ISO) standard. In RDBMS all the data is stored in tables with each
table consisting of rows and columns. Example of Sql Server 2014 SQL format:
What is RDBMS?
RDBMS: It is referred as Relation Database Management Systems (RDBMS). RDBMS possesses a set
of the below given characteristics:
• Write-intensive operations: The RDBMS is frequently written to and is often used in transaction-
oriented applications.
• Data in flux or historical data: The RDBMS is designed to handle frequently changing data.
Alternatively, RDBMS can also store vast amounts of historical data, which can later be analyzed or
"mined".
• Complex data models. The relational nature of the RDBMS makes it suitable for handling
sophisticated, complex data models that require many tables, foreign key values, complex join
operations, and so on.
• Data integrity: The RDBMS features many components designed to ensure data integrity. This
includes rollback operations, referential integrity, and transaction-oriented operations.
Syntax:
What is a query?
A DB query is a code written in order to get the information back from the database. Query can be
designed in such a way that it matched with our expectation of the result set. Simply, a question to
the Database.
What is subquery?
A subquery is a query within another query. The outer query is called as main query, and inner
query is called subquery. SubQuery is always executed first, and the result of subquery is passed on
to the main query.
A correlated subquery cannot be considered as independent query, but it can refer the column in a
table listed in the FROM the list of the main query.
A Non-Correlated sub query can be considered as independent query and the output of subquery
are substituted in the main query.
create table Info (Name varchar(20), BirthDate date,Phone nvarchar(12), City varchar(20))
Syntax:
For a One to many relationships, a single column value in one table has one or more dependent
column values in another table. Look at the following diagram:
The third table acts as a bridge between the tables that want to establish a Many to Many
relationship. The bridge table stores the common information between Many to Many relationship
tables. Have a look at the following diagram:
A table column with this constraint is called the key column for the table. This constraint helps the
table to make sure that the value is not repeated and also that there are no null entries.
Now this column does not allow null values and duplicate values. You can try inserting values to
violate these conditions and see what happens. A table can have only one Primary key. Multiple
columns can participate on the primary key.
Example:
Example: To understand the foreign key clearly let's assume the following two tables:
CUSTOMER {Cust_ID, Cust_Name, Age, ContactNo, Gender, Address} VENDOR {Vend_ID,
Vend_Name, Cust_ID}
Syntax: CREATE TABLE table_name(Col1 datatype NOT NULL, Col2 datatype NOT NULL, Col3
datatype NOT NULL, CONSTRAINT FK_Column FOREIGN KEY(Col1, Col2, Col3) REFERENCES parent
_table(Col1, Col2, Col3) );
This should remove all the duplicate columns from the table. Creation of tables for the related data
and identification of unique columns.
Meeting all requirements of the first normal form. Placing the subsets of data in separate tables
and Creation of relationships between the tables using primary keys. Third Normal Form (3NF):
This should meet all requirements of 2NF. Removing the columns which are not dependent on
primary key constraints.
Meeting all the requirements of third normal form and it should not have multi- valued
dependencies.
What is Denormalization.
DeNormalization is a technique used to access the data from higher to lower normal forms of
database. It is also process of introducing redundancy into a table by incorporating data from the
related tables.
BEGIN
END
• Stored Procedures can be encrypted and that also prevents SQL Injection Attacks
Types of function
• Pre-Defined Function
• User-Defined Function
User-defined Function:
In a user-defined function we write our logic according to our needs. The main advantage of a
user-defined function is that we are not just limited to pre-defined functions. We can write our
own functions for our specific needs or to simplify complex SQL code. The return type of a SQL
function is either a scalar value or a table.
Creation of a function
Execution of a Function
Output:
• Functions can be used anywhere in SQL, such as AVG, COUNT, SUM, MIN, DATE and so on with
select statements.
Function Types:
• A view can combine data from multiple tables using adequate joins and while bringing it may
require complex filters and calculated data to form the required result set. From a user's point of
view, all these complexities are hidden data queried from a single table.
• Sometimes for security purposes, access to the table, table structures and table relationships are
not given to the database user. All they have is access to a view not knowing what tables actually
exist in the database.
• Using the view, you can restrict the user update only portions of the records.
Primary key will create clustered index on column but unique key will create non-clustered index
by default.
Every index increases the time takes to perform INSERTS, UPDATES, and DELETES, so the number
of indexes should not be too much. Try to use maximum 4-5 indexes on one table, not more. If
you have read-only table, then the number of indexes may be increased.
Keep your indexes as narrow as possible. This reduces the size of the index and reduces the
number of reads required to read the index.
Try to create indexes on columns that have integer values rather than character values.
If you create a composite (multi-column) index, the orders of the columns in the key are very
important. Try to order the columns in the key as to enhance selectivity, with the most selective
columns to the leftmost of the key.
If you want to join several tables, try to create surrogate integer keys for this purpose and create
indexes on their columns. Create surrogate integer primary key (identity for example) if your table
will not have many insert operations.
Clustered indexes are more preferable than nonclustered, if you need to select by a range of values
or you need to sort results set with GROUP BY or ORDER BY. If your application will be performing
the same query over and over on the same table, consider creating a covering index on the table.
You can use the SQL Server Profiler Create Trace Wizard with "Identify Scans of Large Tables" trace
to determine which tables in your database may need indexes. This trace will show which tables
are being scanned by queries instead of using an index.
For speed and reliability, it is better to have more disks. When these disks are arranged in certain
patterns and are use a specific controller, they are called a Redundant Array of Inexpensive Disks
(RAID) set. There are several numbers associated with RAID, but the most common are 1, 5 and 10.
I had mentioned that TRUNCATE table can not be rolled back while delete can be.
SQL injection attacks typically are easy to avoid by ensuring that a system has strong input
validation.
As name suggest we inject SQL which can be relatively dangerous for the database. Example this is
a simple SQL
SELECT email, passwd, login_id, full_name FROM members WHERE email = 'x'
Now somebody does not put “x” as the input but puts “x ; DROP TABLE members;”. So the actual
SQL which will execute is:-
SELECT email, passwd, login_id, full_name FROM members WHERE email = ‘x’; DROP TABLE
members;