SQL Programming For Beginners The Ultimate Beginners Guide To Analyze and Manipulate Data With SQL (2020)
SQL Programming For Beginners The Ultimate Beginners Guide To Analyze and Manipulate Data With SQL (2020)
BEGINNERS:
THE ULTIMATE BEGINNERS
GUIDE TO ANALYZE AND
MANIPULATE DATA WITH SQL
Eugene Gates
Table of Contents
Introduction
Chapter 1: SQL Basics
Chapter 2: Relational Database
Chapter 3: Data Types
Chapter 4: Exploring a Database
Chapter 5: Creating a Database
Chapter 6: Getting Started with Queries
Chapter 7: Turning Data into Information
Chapter 8: Working with Multiple Tables
Chapter 9: Using Functions
Chapter 10 : Subqueries
Chapter 11 : SQL Views and Transactions
Conclusion
Introduction
Network programs have become larger and more flexible.
In many cases, the fundamental scheme of operations is mainly a mix of
scripts that handle the command of a database.
When you look at the structure of any business, you will see that it
generates, holds, and then uses data.
Because of the different ways that the company will need to handle this
data, they will need to find some method of storing the information.
In the traditional methods, known as Database Management System or
DBMS, business organizations would have all the data in one place to help
them out.
These systems are pretty simple to use, but modern technology has forced
about some changes.
Even the most essential or basic data management systems have changed,
and now they are more powerful than before.
This can be an advantage to some companies that have a large amount of
data to keep track of or who may need to be careful with some sensitive
information.
Out of all this, there was a new breed of data management that has been
implemented known as the Relational Database Management System or
RDBMS.
This was derived from the renown traditional DBMS, but it is going to have
some more to do with the web as well as server and client technologies.
This basically means that they are going to help various companies with the
management of data.
One of these new relational databases that will help to store the data in an
easy and simple to use a method that also keeps it all safe is SQL.
What is SQL?
It is best to start at the beginning. SQL stands for ‘Structured Query
Language,’ and it is a simple language to learn, considering it will allow
interaction to occur between various databases found in a particular
system.
The original version was established in the 1970s.
This continued to progress in 1979 until IBM released a new prototype, the
Relational Software Inc. that published one of the first SQL tools in the
world.
This tool was at first called ORACLE, and it gained so much success that
the company was able to split off from IBM and create the Oracle
Corporation.
Even today, ORACLE is one of the leaders thanks to being able to use the
SQL language.
The SQL is a set of instructions that you can use to interact with your
relational database.
While there are a lot of languages that you can use to do this, SQL is the
only language that most databases can understand.
Whenever you are ready to interact with one of these databases, the
software can go in and translate the commands that you are given, whether
you are giving them in form entries or mouse clicks.
These will be translated into SQL statements that the database will already
be able to interpret.
If you have ever worked with a software program that is database-driven,
then it is likely that you have used some form of SQL in the past.
It is likely that you didn't even know that you were doing this, though.
For example, there are a lot of dynamic web pages that are database driven.
These will take some user input from the forms and clicks that you are
making and then will use this information to compose a SQL query.
This query will then go through and retrieve the information from the
database to perform the action, such as switch over to a new page.
To illustrate how this functions, think about a simple online catalog that
allows you to search.
The search page will often contain a form that will just have a text box.
You can enter the name of the item that you would like to search using the
form, and then you would simply need to click on the search button.
As soon as you click on the search button, the web server will go through
and search through the database to find anything related to that search
term.
It will bring those back to create a new web page that will go along with
your specific request.
For those who have not spent that much time at all learning a programming
language and who would not consider themselves programmers, the
commands that you would use in SQL are not too hard to learn.
Take your time in reading this book for this will be very helpful in guiding
you as a beginner to SQL Programming.
Chapter 1: SQL Basics
The SQL (the Structured Query Language, Structured Query Language) is a
special language used to define data, provide access to data and their
processing. The SQL language refers to nonprocedural languages - it only
describes the necessary components (for example, tables) and the desired
results, without specifying how these results should be
obtained. Each SQL implementation is an add-on on the database engine,
which interprets SQL statements and determines the order of accessing the
database structures for the correct and effective formation of the desired
result.
SQL to Work with Databases?
To process the request, the database server translates SQL commands into
internal procedures. Due to the fact that SQL hides the details of data
processing, it is easy to use.
You can use SQL to help out in the following ways:
SQL helps when you want to create tables based on the data you
have.
SQL can store the data that you collect.
SQL can look at your database and retrieves the information on
there.
SQL allows you to modify data.
SQL can take some of the structures in your database and change
them up.
SQL allows you to combine data.
SQL allows you to perform calculations.
SQL allows data protection.
Traditionally, many companies would choose to work with the ‘Database
Management System,’ or the DBMS to help them to keep organized and to
keep track of their customers and their products. This was the first option
that was on the market for this kind of organization, and it does work well.
But over the years there have been some newer methods that have changed
the way that companies can sort and hold their information. Even when it
comes to the most basic management system for data that you can choose,
you will see that there is a ton more power and security than you would
have found in the past.
Big companies will be responsible for holding onto a lot of data, and some
of this data will include personal information about their customers like
address, names, and credit card information. Because of the more complex
sort of information that these businesses need to store, a new ‘Relational
Database Management System’ has been created to help keep this
information safe in a way that the DBMS has not been able to.
Now, as a business owner, there are some different options that you can
pick from when you want to get a good database management system. Most
business owners like to go with SQL because it is one of the best options
out there. The SQL language is easy to use, was designed to work well with
businesses, and it will give you all the tools that you need to make sure that
your information is safe. Let’s take some more time to look at this SQL and
learn how to make it work for your business.
How This Works with Your Database
If you decide that SQL is the language that you will work on for managing
your database, you can take a look at the database. You will notice that
when you look at this, you are basically just looking at groups of
information. Some people will consider these to be organizational
mechanisms that will be used to store information that you, as the user, can
look at later on, and it can do this as effectively as possible. There are a ton
of things that SQL can help you with when it comes to managing your
database, and you will see some great results.
There are times when you are working on a project with your company, and
you may be working with some kind of database that is very similar to
SQL, and you may not even realize that you are doing this. For example,
one database that you commonly use is the phone book. This will contain a
ton of information about people in your area including their name, what
business they are in, their address, and their phone numbers. And all this
information is found in one place so you won't have to search all over to
find it.
This is kind of how the SQL database works as well. It will do this by
looking through the information that you have available through your
company database. It will sort through that information so that you are
better able to find what you need the most without making a mess or
wasting time.
Relational Databases
First, we need to take a look at the relational databases. This database is the
one that you will want to use when you want to work with databases that
are aggregated into logical units or other types of tables, and then these
tables have the ability to be interconnected inside of your database in a way
that will make sense depending on what you are looking for at the time.
These databases can also be good to use if you want to take in some
complex information, and then get the program to break it down into some
smaller pieces so that you can manage it a little bit better.
The relational databases are good ones to work with because they allow you
to grab on to all the information that you have stored for your business, and
then manipulate it in a way that makes it easier to use. You can take that
complex information and then break it up into a way that you and others are
more likely to understand. While you might be confused by all the
information and how to break it all up, the system would be able to go
through this and sort it the way that you need in no time. You are also able
to get some more security so that if you place personal information about
the customer into that database, you can keep it away from others, in other
words, it will be kept completely safe from people who would want to steal
it.
Client and Server Technology
In the past, if you were working with a computer for your business, you
were most likely using a mainframe computer. What this means is that the
machines were able to hold onto a large system, and this system would be
good at storing all the information that you need and for processing
options.
Now, these systems were able to work, and they got the job done for a very
long time. If your company uses these and this is what you are most
comfortable with using, it does get the work done. But there are some
options on the market that will do a better job. These options can be found
in the client-server system.
These systems will use some different processes to help you to get the
results that are needed. With this one, the main computer that you are using,
which would be called the ‘server,’ will be accessible to any user who is on
the network. Now, these users must have the right credentials to do this,
which helps to keep the system safe and secure. But if the user has the right
information and is on your network, they can reach the information without
a lot of trouble and barely any effort. The user can get the server from other
servers or from their desktop computer, and the user will then be known as
the ‘client’ so that the client and server are easily able to interact through
this database.
How to Work With Databases That Are Online
There are a lot of business owners who will find that the client and server
technology is the one that works for them. This system is great for many
companies, but there are some things that you will need to add or take away
at times because of how technology has been changing lately. There are
some companies that like the idea that their database will do better with the
internet so that they can work on this database anywhere they are located,
whether they are at home or at the office. There are even times when a
customer will have an account with the company, and they will need to be
able to access the database online as well. For example, if you have an
account with Amazon, you are a part of their database, and you can gain
access to certain parts through this.
As the trend continues for companies to move online, it is more common to
see that databases are moving online as well and that you must have a
website and a good web browser so that the customer can come in and
check them out. You can always add in usernames and passwords to make it
more secure and to ensure that only the right user can gain access to their
information. This is a great idea to help protect personal and payment
information of your customers. Most companies will require that their users
pick out security credentials to get on the account, but they will offer the
account for free.
Of course, this is a system that is pretty easy to work with, but there will be
a number of things going on behind the scenes to make sure that the
program will work properly. The customer can simply go onto the system
and check the information with ease, but there will be a lot of work for the
server to do to make sure that the information is showing up on the screen
in the right way, and to ensure that the user will have a good experience and
actually see their own account information on the screen.
For example, you may be able to see that the web browser that you are
using uses SQL or a program that is similar to it, to figure out the user that
your data is hoping to see.
Why is SQL So Great?
Now that we have spent some time talking about the various types of
database management systems that you can work with; it is time to discuss
why you would want to choose SQL over some of the other options that are
out there. You not only have the option of working with other databases but
also with other coding languages, and there are benefits to choosing each
one. So, why would you want to work with SQL in particular? Some of the
great benefits that you can get from using SQL as your database
management system includes:
Incredibly Fast
If you would like to pick out a management system that can sort through the
information quickly and will get the results back in no time, then SQL is
one of the best programs to use for this. Just give it a try, and you will be
surprised at how much information you can get back, and how quickly it
will come back to you. In fact, out of all the options, this is the most
efficient one that you can go with.
Well-Defined Standards
The database that comes with SQL is one that has been working well for a
long time. In addition, it has been able to develop some good standards that
ensure the database is strong and works the way that you want. Some of the
other databases that you may want to work with will miss out on these
standards, and this can be frustrating when you use them.
Chapter 2: Relational Database
A relational database management system is a management system based on the
relational database model. Its most common representation is as a table that
contains rows and columns.
First introduced by E.F Codd, A RDBMS has several key components.
They include:
Table
Record/Tuple
Field/ Column
Instance
Schema
Keys
We will cover some of these components as we progress further into the SQL
lessons covered in the book. For now, let us look at tables and columns.
SQL Tables
Tables are objects used to store data in a Relational Database Management
System. Tables contain a group of related data entries with numeric columns and
rows.
Tables are the simplest form of data storage for relational databases. They are also
a convenient representation of relations. However, a table can contain duplicate
rows while a relation cannot contain any duplicate. The following is an example
of a DVD rental database table:
The table shows information about customers in a DVD rental database.
SQL Fields
Tables break down further into smaller entities referred to as fields. Fields are
columns in tables designed to hold information about records in the specified
table. For example, in the table above, we can see fields that contain Customer_id,
store_id, first_name and last_name.
SQL Records
In a database, we use Records or Rows to refer to a single entry in a database
table. It represents a collection or related data. We can also regard it as the
horizontal entity in a database table. For example, in the above table, we have five
records or tuples.
SQL
Columns
In a database table, we use a column to refer to the vertical entity containing data
related to specific fields in a table. For example, in the above table, a column
could be first_name or last_name.
NULL Values
A null value is a value assigned to a blank field. This means that a field with a
value NULL has no value. In SQL, a zero or a space is not a NULL value. NULL
value mainly refers to a field that is blank in a record.
SQL Constraints
Constraints are rules applied to columns in a database table. We mainly use them
to govern the type of data stored within the table. This ensures that the database is
accurate and reliable thus minimizing errors.
We can set constraints on columnar or tabular level. Columnar level rules apply
only to the specified column while tabular level rules apply to the entire database
level.
SQL contains various rules applied to the stored data.
They include:
NOT NULL: This rule ensures that a table or column does not contain
null value, which means during record creation, you must enter either a
zero, Space, or another value
UNIQUE: Ensures each value is unique and no duplicates are available
DEFAULT: We use this constraint rule to set a default value for a
column where we have an unspecified value
PRIMARY KEY: Identifies each record distinctively in a table. We
create this rule by combining NOT NULL and UNIQUE constraints
FOREIGN KEY: We use this constraint to identify (distinctively) a
record in another database table
CHECK constraint: We use this constraint to confirm if the values in a
column fulfill defined conditions
INDEX constraint: We use this to create and retrieve data from a
database
Data Integrity
Data integrity refers to the consistency and accuracy of the data. When creating
databases, you MUST pay attention to data integrity. A good database must ensure
reinforcement of data integrity as much as possible. Data integrity must also
remain maintained during manipulation and update of the database.
Various factors can lead to compromised data integrity within a database. These
factors include:
Unnormalized Form
First Normal Form
Second Normal Form
Third Normal Form
Elementary-Key Normal Form
Boyce-Codd Normal Form
DBMS VS RDBMS
We have already covered RDBMS. DBMS is not different either. DBMS is a
software package we use to create and manipulate databases.
DBMS and RDBMS give programmers and users an organized way of working
with databases. DBMS and RDBMS are not very different since both provide a
physical database storage.
With the above noted, some relevant difference between them are worth
mentioning.
They include:
RDBMS DBMS
Supports Normalization Normalization is not available
in a DBMS
Supports Distributed Database Does not support Distributed
System Database System
Stores data in a table format Stores data is a normal
computer form
Integrity constraints are defined Security for data
for the purpose of ACID manipulation is not applied
property by DBMS
Can handle large amounts of Designed to small scale data
data such as a Company and personal use
database
It supports multiple users Supports a single user
Cases of data redundancy are Data redundancy is a very
close to none common scenario
Stores data in tabular form and
Mainly stores data in
utilizes primary keys hierarchical or navigational
format
Requires complex software and Low software and hardware
high-performance hardware to requirements for
implement implementation
Supports client-server Client-side architectures is
architecture not supported
They include PostgreSQL, Include file system files, xml
MySQL and Oracle etc. files, and windows registry.
At this point in the guide, you know enough to start working with SQL and
databases.
Chapter 3: Data Types
The Basic Types of Data
Data types are attributes of the information itself, whose characteristics are
placed inside a table. For instance, you may require that a field should hold
numeric values only, stopping any user from entering alphanumeric data.
By assigning data types to certain fields in a database, you can minimize the
possibility of errors in data entry.
❖ Important Note: Each version of SQL has its own array of data
types. Nowadays, you should use version-specific data types if you want to
manage your database properly. The basics, however, are the same for all
SQL versions.
Here are the basic types of data:
Numeric strings
Character strings
Time and date values
The output shows that the database was created successfully. However, it
will be good for you to confirm whether or not the database was created. To
do this, use the SHOW command as shown below:
SHOW databases;
The above output shows that the school database was created successfully.
The above command returns the list of databases you have in your system.
An attempt to create a database that already exists generates an error. To
confirm this, try to recreate the school database by running the following
command:
CREATE DATABASE school;
The above output shows an error because the database already exists. To
avoid this error, we can use the optional clause IF NOT EXISTS. This is
showed below:
RENAME TABLE
Sometimes, you may need to change the name of your database table. This
could be after you realize that the current name of the table is not
meaningful. It then becomes necessary for you to change the name of the
table. This is possible by the use of the RENAME TABLE command. The
command takes the following syntax:
ALTER TABLE old_table_name
RENAME TO new_table_name;
;
For example, to change a table named employees to workers, we run the
following command:
ALTER TABLE employees
RENAME TO workers;
The table will be renamed successfully.
ALTER TABLE
This is the statement we use any time we need to change the columns of a
table. The modification in this can mean the need to add, change or delete a
table column. This command can also help you add or drop the various
constraints that may have been imposed on an existing table.
Adding a Column
To add a new column to a table, use the ALTER TABLE command with the
following syntax:
ALTER TABLE tableName ADD columnName column-definition;
However, you may sometimes to add multiple columns to the table. In such
a case, use the command with the following syntax:
ALTER TABLE tableName
ADD (column1 column-definition,
column2 column-definition,
.....
column_n column-definition);
Let us show this with an example. Suppose we have the workers table with
the following columns:
We need to add a column named home_town in which we will add the
hometown for each worker. We can do this by running the following
command:
We can describe the table to see whether or not the change was reflected:
Dropping Columns
Sometimes, you may need to reduce the number of columns you have in a
certain table. In such a case, it will be necessary for you to drop or delete
some table columns. It is possible with SQL.
To delete a table column, we combine the ALTER TABLE and the DROP
statements. This is done using the syntax given below:
ALTER TABLE tableName DROP COLUMN column_name;
Again, we will use the workers table to show how to use the above
command. The table has the following set of columns:
The above figure shows that the column deleted from the table successfully.
It is important to know the various data types supported in SQL. SQL data
types specify the data for every object. Every variable, column and
expression in SQL has an associated data type.
Chapter 6: Getting Started with Queries
How Do Queries Work?
Queries are a principal working flow for SQL servers. Without querying
operations, it is technically impossible to maintain or regulate database
operations.
For successful querying database checks the following essentials:
1) Does the authorized user have permissions to execute this statement? If
not terminate the statement and show Authorization error.
2) If the user is authorized then the SQL server will check whether the
authorized user has permission to retrieve the data he is asking for? If not
terminate the procedure and display data retrieval errors for the user.
3) If the author is authorized and has permission to retrieve the data then the
SQL server will check the SQL syntax that the user has entered. If SQL
syntax is correct then the querying process will start and display results to
the end-user. If the SQL syntax is not right then syntax error will be shown.
If the statement entered satisfies these three complexities then your
statement will be sent to a query optimizer to analyze the syntax in detail.
What is a Query optimizer?
The query optimizer is a system that helps you to analyze the syntactical
structure of the syntax given. The optimizer will first check at all the from
clauses that are present and then will look at all of the indexes that are
supported. After analyzing all the clauses optimizer will choose the best
way to query the operation provided. According to the resources provided it
will proceed.
What Happens Next?
After an execution procedure is selected by the Query optimizer it is then
sent to the tool that you are using. This tool is the SQL application itself.
Then the SQL application will conduct the procedure and display results for
the query operation. If the operation succeeds the result will be shown if not
then an error will appear. There is also a chance for the query process with
no results.
Before knowing about the SELECT function it is necessary to know about
the importance of the FROM clause.
FROM
FROM is a simple SQL clause that helps you to point out an instance.
When using the SELECT statement it is mandatory to look out at an
instance for selecting the columns. This is where the FROM clause helps
you to select the columns from the databases.
An SQL statement is here:
FROM {instance name}
Here the instance name belongs to either database or table name. You can
also individually enter the column or primary key values to extract
information.
As we have sufficient knowledge about the FROM statement now, we will
discuss the SELECT cause in detail.
SELECT
The SELECT statement is usually used to select the columns provided
using the logical entities that are given. The select clause can often be
simple but sometimes it may lead to complex query operations.
Usually, select statements are performed at the end of the query operation
by the SQL server software because it is technically not possible to select
the things without having an update and changed table. For this reason,
SELECT statements are usually performed at the end and often take much
longer time than the other query operations.
Here is the SQL statement:
SELECT {Table or column values}
-> FROM {Instance name}
Now, we will discuss the parameters that are present in the syntax.
1) Table/ Column values - In this instance you need to enter all the columns
that you are going or willing to select by this query. You can use logical
expressions to denote the columns you need to select automatically. If you
want to select all the columns present then you can use an asterisk '*'.
2) Instance names - In this, we need to enter the database name we need to
retrieve the columns from. Without mentioning FROM statement, it is
technically not possible to select the columns.
To say in a single sentence, the Select clause is used to determine all the
columns for a query operation.
What else can you do with a SELECT statement?
1) You can use any numerical information such as the primary key id for
selecting it.
2) You can enter various logical and conditional expressions to expect
automated results.
3) You can sometimes also use inbuilt or scalar functions that SQL offers
for its users.
4) If there is no system function satisfying for your requirement then you
can build a user-defined function from scratch and use it for selecting.
ALIASES
Aliases are column names that are generated for the queries. Usually, SQL
generates custom column names for the query results. However, they are
often clumsy and it is essential to generate our columns for both query
results and function results. To make your column names usually aliases are
used.
Aliases can also be used to return column names that are generated using
logical expressions.
Here is the SQL statement:
ALIAS {Column name} {Logical expression}
Creating Indexes
When querying large amounts of data, indexing technology can be applied.
An index is a special type of database object that holds the sort structure of
one or more columns in a data table. Effective use of indexes can improve
the efficiency of the data query. This part focuses on the creation of indexes
and related maintenance work.
An index is a separate, physical database structure. In SQL Server, an index
is a decentralized storage structure created to speed up the retrieval of data
rows in a table. It is built for a table, and the rows in each index page
contain logical pointers to physical locations in the data table to speed up
retrieval of physical data.
Therefore, whether to create indexes on the columns in the table will have a
great impact on the query speed. The storage of a table is composed of two
parts, one part is used to store the data pages of the table and the other part
is used to store the index pages.
Usually, index pages are much smaller for data pages. In data retrieval, the
system first searches the index page to find the pointer of the required data,
and then directly reads the data from the data page through the pointer, thus
improving the query speed.
The way the database uses indexes is very similar to the catalog of books.
Indexes allow a chance of viewing through a complete book. In a database,
indexes can be used for introspecting a table without having to scan the
entire table.
After understanding the basic concepts of indexes, the following describes
the advantages and disadvantages of using indexes and the conditions for
using indexes.
1. Advantages of using indexes
(1) Create a unique index to ensure the uniqueness of each row of data in
the database table.
(2) The creation of indexes is directly proportional to the increase in the
speed of the system.
(3) The accelerator and the table are of special significance in realizing the
extra advantages of the data.
(4) When using GROUP BY and ORDER BY you should be aware that the
grouping and ordering time in the query can also be reduced.
2. Disadvantages of Using Index
(1) It is a very high turmoil to look at indexes and maintain indexes, which
increases with the increase in data volume.
(2) The index needs a lot of storage indication. In addition to the data table
occupying data space, each index also occupies a certain amount of
physical space. If cluster indexes are to be established, the space required
will be larger.
(3) When adding, deleting and modifying the data irrespective of
circumstances, the index is used in a separate mechanism such that it
reduces the overall capability of the system.
Classification of Indexes
The data in the database page of SQL Server can be divided into two types
according to the storage structure: Clustered Index and Non clustered Index.
1. Cluster index
Cluster index (also called "clustered index") means that the physical storage
order of data rows in the table is the same as the index order. Cluster index
consists of upper and lower layers: the upper layer is an index page, which
contains index pages in the table and is used for data retrieval; The lower
layer is the data page, which contains the actual data page and stores the
data in the table.
When creating a clustered index in a column of a table, the data in the table
will be reordered according to the index order of the columns and the table
will be modified. Therefore, only one cluster index can be created in each
table. Cluster indexes are created on columns that are frequently searched
or accessed sequentially.
2. Non-cluster index
The non-clustered index (also called "non-clustered index") does not
change the physical storage location of the data rows in the table. Data is
stored separately from the index and is linked to the data in the table
through the pointer carried by the index. Non-clustered indexes are similar
to those in textbooks.
The data is present in one place, the index is present in another place, and
the index has a pointer to the storage location of the data. The items in the
index are stored in the order of index key values, while the information in
the table is stored in another order (non-clustered indexes can specify this).
A table can contain multiple non-clustered indexes, and a non-clustered
index can be created for each column commonly used when looking up data
in the table.
The method of creating a clustered index is similar to that of the non-cluster
index, both uses CREATE INDEX statement, except that the CLUSTERED
clause needs to be specified to create a clustered index.
How to Create An Index?
All you need to do is enter the following syntax for creating indexes. As
explained above clustered index needs a separate parameter for creation.
Here is the syntax:
CREATE INDEX {Parameters}
In these parameters, we will define whether it is a clustered or non-clustered
index.
You can also use SQL server management software to create indexes with a
click.
MySQL Functions
Functions are an easy way to repeat a task. Functions consist of a set of
code that can be used in logical execution and expressions. SQL provides a
lot of scalar and aggregation functions for the database users.
Why are Functions Used?
For example, you can use SQL functions to find maximum or minimum
values for your column values. You can also use functions created by
yourself from scratch to repeat tasks automatically.
Chapter 7: Turning Data into Information
Through this part, when we bring up the topic of the operators that are
inside of the database, we are going to talk about the characters and the
words that we have reserved, and any that can be used along with the
WHERE clause of the statements we write. Operators are helpful because
we are able to use them to perform operations in the statements, such as
comparisons and any of the equations that we need in math, but they can
also come in to make it easier to set up some of the parameters that we want
to see around our statements. And we need always to remember that they
are there to help us connect together two or more parameters that we have
in the same statement if this is something that our code needs.
During this process, we are going to need to remember that there are a few
options of operators that you are able to use in these statements. The four
most common options that we are able to work with include:
Operators that are used to help negate conditions.
Logical operators
Comparison operators
Arithmetic operators.
Logical Operators
The first operator type that we are going to spend some time focusing on in
this guidebook will be the logical operators. These are going to be the kinds
of operators that we are able to use with the keywords of our statements,
and they are going to be easier to form with comparisons inside of the
statements. Some of the different logical operators that we are able to add
into our statements will include:
Addition—you will just need to use the “+” sign to get started on
using addition. For this example, we are going to add up the
values for the overhead column and the materials column. The
statement that works for this is:
SELECT MATERIALS + OVERHEAD FROM
PRODUCTION_COST_TBL.
Subtraction—you can also do some basic subtraction when it
comes to your SQL statements. You will simply need to use the
“-“ sign to do this.
Multiplication—it is possible to do multiplication from your
tables as well. You would need to use the “*” sign to do this.
Division—this one will just need the “/” sign to get started.
With the arithmetic operators, you will be able to gain a lot of freedom in
some of the codes that you are writing. You can combine a few of these
operators together and even end up with your own equation inside of the
statement. For example, you could have one statement with a bit of
multiplication along with some division and addition if you would like. You
can also have it so that you are just using a few multiplication signs in the
same statement as well. Whatever is needed to bring the code together and
make it work in the way that you want is available when it comes to these
arithmetic operators.
One thing that we need to remember when we decide to add in more than
one of these operators to our statement at a time is that we need to work
with something known as the principles of precedence to make sure that we
get it right. This means that the syntax is going to be able to take care of the
arithmetic operators in a certain way to help us get the right answer.
When we are doing this kind of principle in SQL, the syntax is going to
take care of everything that we need to multiply first, and then it will pay
attention to everything that we need to divide, and then it will do those that
should be added together, before finishing up with the things that should be
subtracted from one another. And the syntax is going to do this in a manner
that goes from left to right, along with going through the symbols in the
order that we did above. This makes sure that we are going to get the right
answers, and is similar to what we should remember from basic algebra
classes in the past.
Working with the Conjunctive Operators
Another type of operator that we need to spend some of our time on here is
the conjunctive operators. When you are doing some work with your
statements, you may find that there will be some times when new criteria
will be needed in order to make sure that the command is going to behave
the way that we want.
For example, if you are sending out a database search and your results that
might be a bit more confusing, you may find that adding in some different
criteria to the mix to get the results you want is a much better idea. You can
also use these when you would like to be able to combine together a few
different types of criteria within the single statement, and then you would
create a brand-new conjunctive operator that works for your needs.
Even though you are able to create one of the conjunctive operators that you
want technically, there are going to be a few of these that are already
predefined inside of the SQL language, and you are able to use them as you
need. Some of these operators are going to include:
Now, using the DELETE clause record with identity value 3 can be
dropped.
DELETE FROM LxryItms
WHERE
id = 3;
After you run the query above, you will receive a message stating 1 row(s)
affected.
Now, to verify the data with the view use the query below:
SELECT
*
FROM
LxryItms;
The result set is displayed in the picture below:
Finally, use the syntax below to retrieve desired records from the
underlying table to confirm that the “DELETE” statement in fact removed
the record:
SELECT
*
FROM
itms;
The result set is displayed in the picture below, which confirms that the
record with identity value 3 has been deleted from the items table:
Now, assume that you would like to insert the jobtitl column to the cntcts
view. You can accomplish this with the syntax below:
CREATE OR REPLACE VIEW cntcts AS
SELECT
fstName, lstName, extnsn, eml, jobtitl
FROM
emplyes;
The result set is displayed in the picture below: