Sie sind auf Seite 1von 9

Basic SQL

Structured Query Language (SQL) - Basic Concepts


Having query about what a SQL is? It is Structured Query language. It is a non
procedurallanguage and it is a database language.
Categories in SQL commands
There are 3 broad categories in SQL commands. They are namely

Data Definition language (DDL)


Data Manipulation Language (DML)
Transaction Control Commands

Under each category there are many SQL commands. Let us see each in brief.

Commands under Data Definition language (DDL)


This category include creating, dropping and altering table commands
CREATE TABLE is used in creation of table(s).
SYNTAX: CREATE TABLE tablename (colname col definition, colname col
definition);
ALTER TABLE: If you want to modify a table definition we can use this
command. This command is used to alter column definition in table.
SYNTAX: ALTER TABLE tablename MODIFY (Column definition);
DROP TABLE: This command is used to remove an existing table permanently
from database.
SYNTAX: DROP TABLE tablename;

Commands under Data Manipulation Language (DML)

INSERT: This command is used to insert rows into the table.


SYNTAX: INSERT INTO tablename VALUES (value,value,.);

SELECT: This is used to select values or data from table


SYNTAX: SELECT column name1, column name2, . FROM tablename;
If we want to select all column values from a table then SQL command use is
SELECT * from tablename ;
DELETE: In order to delete rows from a table we use this command
SYNTAX: DELETE FROM tablename WHERE condition;
Based on the condition specified the rows gets fetched from the table and gets
deleted in table. Here the WHERE clause is optional.
UPDATE: This SQL command is used to modify the values in an existing table.
SYNTAX: UPDATE tablename SET columnname = value, columnname = value,
.. WHERE condition;
The rows which satisfies the WHERE condition are fetched and for these rows the
column values we placed in command above in SET statement gets updated.

Commands under Transaction Control Statements


ROLLBACK This SQL command is used to undo the current transaction
SYNTAX: ROLLBACK;
SAVEPOINT: This is used to identify a point in a transaction to which we can
later rollback.
SYNTAX: SAVEPOINT savepoint_identifier;
COMMIT: This command is used to make all changes permanent in database and
also marks the end of transaction.
SYNTAX: COMMIT;
Always it is better to commit changes to database at regular intervals since it will
help loss of data or loss of work done when computer shuts due to unavoidable
reasons.
Having known about the broad categories of SQL commands let us see some
more SQL important terminologies.

Constraints Handling in SQL:


Constraint Handling can be done in two levels namely:

Column Level
Table Level

Some of the Constraints used in SQL are:

The keyword NOT NULL marks a column that the particular column cannot contain
null. By default column contains null unless we define the constraint NOT NULL.
This is a column level constraint as it can be defined for columns only.
PRIMARY KEY
A column or a group of columns together can be defined as PRIMARY KERY. If a
column or a group of columns are defined as a PRIMARY KEYS then the value of
the primary key cannot appear more than once in the table. Also those columns
defined as primary keys cannot have null values in it.
REFERENTIAL INTEGRITY
The table on which the column or a combination of columns is defined by this
constraint is called the foreign key is called child table. When this constraint is
defined a relation is defined between this table which has the foreign key and a
table that has the primary key in relationship with this. This is called as
referenced key. The table which has the primary key or in other words the
referenced key is called as parent table.

SQL Functions to Handle Arithmetic Operations


All these SQL arithmetic functions described below operate on numerical values
only and are used to get data as per users required format.
Some of the functions are:

To find the absolute value of a number we can use the SQL function ABS
(number);
To round a number to a specified number of decimal places SQL command used is
ROUND (number, number of decimal places to be rounded);
To convert a char to a number SQL function used is TO_NUMBER (char value);

VIEW
This is an important concept in SQL. A View is nothing but a window of an
existing table. In other words it is a logical representation that is created from
one or existing base table. After creation of view it can be used just as a base
table. That is one can query or select values of datas from views and it also
possible to update a view.

Views are created by suing CREATE VIEW command in SQL

SYNTAX: CREATE VIEW viewname AS SELECT statement


One of the important points to consider while a user uses views is that we have
seen that vies are created from a base table and if suppose the base table is
deleted after which if a user tries to use or access the view error will occur.
SYNONYMS:
It is possible to refer a table with a different name and this done by using CREATE
SYNONYM. It is possible to create synonym for tables as well as views.
SYNTAX: CREATE synonym name FOR username.tablename;
GROUP Functions
We have seen before arithmetic functions which operated on numerical value and
returned a single value for each single row taken ass input. But there may be
occasions where one might require several rows to be grouped and the result of
the grouped rows might be needed. To handle such situations GROUP functions in
SQL helps. There are number of group functions available in SQL. Let us see
some of them:

SQL: Joins
A join is used to combine rows from multiple tables. A join is performed
whenever two or more tables is listed in the FROM clause of an SQL statement.
There are different kinds of joins. Let's take a look at a few examples.

Inner Join (simple join)


Chances are, you've already written an SQL statement that uses an inner join. It
is the most common type of join. Inner joins return all rows from multiple tables
where the join condition is met.
For example,

SELECT suppliers.supplier_id, suppliers.supplier_name,


orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;
This SQL statement would return all rows from the suppliers and orders tables
where there is a matching supplier_id value in both the suppliers and orders
tables.

Let's look at some data to explain how inner joins work:


We have a table called suppliers with two fields (supplier_id and supplier_
name).
It contains the following data:
supplier_id supplier_name
10000

IBM

10001

Hewlett Packard

10002

Microsoft

10003

NVIDIA

We have another table called orders with three fields (order_id, supplier_id, and
order_date).
It contains the following data:
order_id supplier_id order_date
500125

10000

2003/05/12

500126

10001

2003/05/13

If we run the SQL statement below:


SELECT suppliers.supplier_id, suppliers.supplier_name,
orders.order_date
FROM suppliers, orders
WHERE suppliers.supplier_id = orders.supplier_id;

Our result set would look like this:


supplier_id

name

order_date

10000

IBM

2003/05/12

10001

Hewlett Packard

2003/05/13

The rows for Microsoft and NVIDIA from the supplier table would be omitted,
since the supplier_id's 10002 and 10003 do not exist in both tables.

Outer Join
Another type of join is called an outer join. This type of join returns all rows from
one table and only those rows from a secondary table where the joined fields are
equal (join condition is met).
For example,
select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where suppliers.supplier_id = orders.supplier_id(+);
This SQL statement would return all rows from the suppliers table and only those
rows from the orders table where the joined fields are equal.
The (+) after the orders.supplier_id field indicates that, if a supplier_id value in
the suppliers table does not exist in the orders table, all fields in the orders table
will display as <null> in the result set.
The above SQL statement could also be written as follows:
select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where orders.supplier_id(+) = suppliers.supplier_id

Let's look at some data to explain how outer joins work:


We have a table called suppliers with two fields (supplier_id and name).
It contains the following data:
supplier_id supplier_name
10000

IBM

10001

Hewlett Packard

10002

Microsoft

10003

NVIDIA

We have a second table called orders with three fields (order_id, supplier_id, and
order_date).
It contains the following data:
order_id supplier_id order_date
500125

10000

2003/05/12

500126

10001

2003/05/13

If we run the SQL statement below:


select suppliers.supplier_id, suppliers.supplier_name, orders.order_date
from suppliers, orders
where suppliers.supplier_id = orders.supplier_id(+);

Our result set would look like this:


supplier_id

supplier_name

order_date

10000

IBM

2003/05/12

10001

Hewlett Packard

2003/05/13

10002

Microsoft

<null>

10003

NVIDIA

<null>

The rows for Microsoft and NVIDIA would be included because an outer join was
used. However, you will notice that the order_date field for those records contains
a <null> value.

RDBMS Basics
Basically RDBMS (Relational Database Management System) is nothing but a
standard. For many years database industry was not having any standard for storing
and retrieving information. As a result of this standard we came up the language
called SQL or Structured Query Language. We use SQL to communicate with
RDBMS databases including but not limited to MS Access, Oracle, MS SQL Server
etc. MS Access databases used to have file extension as .mdb but in 2007 release the
default extension is .accdb.
Using MS Access GUI (Graphical User Interface) we can now build very complex
SQL quires very fast visually just by Click, Drag & Drop operation. While we are
performing such tasks MS Access writes the SQL code for us in the background.
According to the RDBMS standard, a database should be comprised of tables and data
inside each of these tables should reside in the form horizontal rows called Records
and vertical columns called Fields.

In the above screenshot Last Name, First Name and Hire Date are the fields of
this table. If you move horizontally, you see records. We can say that the table has 3
Fields and 6 records. The standard also says that the each table may have relationship
with one or more tables. Relationships in between the tables are developed using
Primary/Foreign Key concept which we will discuss later in this book. The sole

purpose of developing such relationship amongst tables is to reduce the redundancy of


data inside the database. It also helps in increasing the data integrity of the system.
Where MS Access Stands?
You might have heard about different databases names like MS Access, Oracle, SQL
Server etc. and must be thinking which one to select for learning or implementation
purpose. Nearly all the good, famous databases are RDBMS including but not limited
to Oracle, DB2, MS SQL Server, MS Access, MySQL etc.

Since all these databases mentioned above follow RDBMS standard, learning and
mastering one will definitely reduce the time to master the other.

Interview Questions and Answers:


1. Difference between primary key and unique key ?
The column holding the primary key constraint cannot accept
null values.whereas colum holding the unique constraint can
accept null values assume that t3 is a table with two
columns t1 having primary key constraint and t2 having
unique constraint if u try to insert null into t2 it will
accept that values whereas column t1 will not accept null
Primary key doesnot allow null value but unique key allows
null value. We can declare only one primary key in a table
but a table can have multiple unique key(column assign).

2. What is the Difference between DBMS and RDBMS?


A DBMS has to be persistent, that is it should be accessible when the program
created the data ceases to exist or even the application that created the data
restarted. A DBMS also has to provide some uniform methods independent of a
specific application for accessing the information that is stored.
RDBMS is a Relational Data Base Management System Relational DBMS. This adds
the additional condition that the system supports a tabular structure for the data,
with enforced relationships between the tables. This excludes the databases that
don't support a tabular structure or don't enforce relationships between tables.
Many DBA's think that RDBMS is a Client Server Database system but thats not
the case with RDBMS.
Yes you can say DBMS does not impose any constraints or security with regard to
data manipulation it is user or the programmer responsibility to ensure the ACID
PROPERTY of the database whereas the rdbms is more with this regard bcz rdbms
define the integrity constraint for the purpose of holding ACID PROPERTY.

I have found many answers on many websites saying that


DBMS are for smaller organizations with small amount of data, where security of
the data is not of major concern and RDBMS are designed to take care of large
amounts of data and also the security of this data.
and this is completely wrong by definition of RDBMS and DBMS.
3. What is the difference between a "where" clause and a "having"
clause?
Where" is a kind of restiriction statement. You use where clause to restrict
all the data from DB.Where clause is using before result retrieving. But
Having clause is using after retrieving the data.Having clause is a kind of
filtering command.
4. What is the basic form of a SQL statement to read data out of a
table?
The basic form to read data out of table is SELECT * FROM table_name;
An answer: SELECT * FROM table_name WHERE xyz= whatever; cannot
be called basic form because of WHERE clause.
5. What is a "join"?
join used to connect two or more tables logically with or without
common field.
6. What is "normalization"? "Denormalization"? Why do you
sometimes want to denormalize?
Normalizing data means eliminating redundant information from a table
and organizing the data so that future changes to the table are easier.
Denormalization means allowing redundancy in a table. The main benefit
of denormalization is improved performance with simplified data retrieval
and manipulation. This is done by reduction in the number of joins needed
for data processing.
7. What is a "constraint"?
A constraint allows you to apply simple referential integrity checks to a
table. There are four primary types of constraints that are currently
supported by SQL Server: PRIMARY/UNIQUE - enforces uniqueness of a
particular table column. DEFAULT - specifies a default value for a column in
case an insert operation does not provide one. FOREIGN KEY - validates
that every value in a column exists in a column of another table. CHECK checks that every value stored in a column is in some specified list. Each
type of constraint performs a specific type of action. Default is not a
constraint. NOT NULL is one more constraint which does not allow values
in the specific column to be null. And also it the only constraint which is
not a table level constraint.
8. What is a "primary key"?

A PRIMARY INDEX or PRIMARY KEY is something which comes mainly


from
database theory. From its behavior is almost the same as an UNIQUE
INDEX, i.e. there may only be one of each value in this column. If you call
such an INDEX PRIMARY instead of UNIQUE, you say something about
your table design, which I am not able to explain in few words. Primary
Key is a type of a constraint enforcing uniqueness and data integrity for
each row of a table. All columns participating in a primary key constraint
must possess the NOT NULL property.
9. What is a "trigger"?
Triggers are stored procedures created in order to enforce integrity rules
in a database. A trigger is executed every time a data-modification
operation occurs (i.e., insert, update or delete). Triggers are executed
automatically on occurance of one of the data-modification operations. A
trigger is a database object directly associated with a particular table. It
fires whenever a specific statement/type of statement is issued against
that table. The types of statements are insert,update,delete and query
statements. Basically, trigger is a set of SQL statements A trigger is a
solution to the restrictions of a constraint. For instance: 1.A database
column cannot carry PSEUDO columns as criteria where a trigger can. 2. A
database constraint cannot refer old and new values for a row where a
trigger can.
10. Why can a "group by" or "order by" clause be expensive to
process?
Processing of "group by" or "order by" clause often requires creation of
Temporary tables to process the results of the query. Which depending of
the result set can be very expensive
11. What is a SQL view?
An output of a query can be stored as a view. View acts like small table
which meets our criterion. View is a precomplied SQL query which is used
to select data from one or more tables. A view is like a table but it doesnt
physically take any space. View is a good way to present data in a
particular format if you use that query quite often. View can also be used
to restrict users from accessing the tables directly.

Das könnte Ihnen auch gefallen