




























































































Prepara tus exámenes y mejora tus resultados gracias a la gran cantidad de recursos disponibles en Docsity
Gana puntos ayudando a otros estudiantes o consíguelos activando un Plan Premium
Esta página no es visible en la vista previa
¡No te pierdas las partes importantes!





























































































Easy SQL Programming & Database Management For Beginners. Your Step-By-Step Guide To Learning The SQL Database.
Felix Alvaro
Firstly, I want to thank God for giving me the knowledge and inspiration to put this informative book together. I also want to thank my parents, my brothers and my partner Silvia for their support.
Chapter Eight: Transactional Control Commands
Chapter Nine: Database Views
Chapter Ten: Enhancing Database Designs
Chapter Eleven: Database Advance Topics
Recap + Final Words
© Copyright 2016 by D.A.Alvaro - All rights reserved.
This document is geared towards providing exact and reliable information in regards to the topic and issue covered.
It is not legal to reproduce, duplicate, or transmit any part of this document in either electronic means or in printed format. Recording of this publication is strictly prohibited.
In this chapter you will learn a brief background on how and why SQL came into existence. Gaining knowledge on the history of this computer language will help you understand its importance to most IT professionals who focus on the field of data manipulation. You will also have an idea on how to maximize the potentials of SQL in the ever-changing world of Information Technology.
The current trend in most businesses today is to invest in technology that will gather data in the most efficient and effective way. However, gathering information is only the start of the extensive process of data manipulation. Companies, especially multi-national ones, require experts who possess the skills of analyzing, presenting, managing and storing data. In other words, they need to use computer programs that will transform raw company data to useful information. Now, thanks to Structured Query Language , or simply SQL , that brought about such transformation in accessing and manipulating data in a very meaningful way.
History of SQL
Pronounced as ees-que-ell or see’qwl , SQL is a computer language initially invented by an American multinational technology and consulting company known as IBM (International Business Machines Corporation) way back 1970s using Dr. E. F. Codd’s paper on “A Relational Model of Data for Large Shared Data Banks” for the prototype design. It was originally called SEQUEL (Structured English QUEry Language) that handled queries on the collection and organization of data - or simply known as a database. More features were added to the computer software to improve its performance, like building and managing database security, among others. When IBM researchers learned that there is another company that had the same “Sequel” trademark, they renamed it to “S-Q-L” (presently expanded as Structured Query Language ).
Since it was first released to the public, SQL already had many versions. In 1979, Relational Software, Incorporated (which later became the Oracle Corporation) released ORACLE , the first SQL product. Now, as the demand for computers that manage data has increased, the more SQL has become an industry standard in the field of Information Technology. Such formal standard is set and maintained by the International Standards Organization , or simply known as ISO. It was on 1986, based on IBM’s implementation, that SQL has been recognized as the standard language in database communication. The following year, ISO accepted ANSI SQL as the international standard. ANSI stands for American National Standards Institute , which is an organization that approves certain standards in various US industries. Many revisions of the standards followed, such as in 1992 (SQL-92) and in 1999 (SQL-99). The latest one is now called SQL-2011, which was officially released in December 2011.
Uses of SQL
The corporate world is now shifting from merely producing products and providing services to investing in digital technologies that handle vast amounts of data, to be transformed to meaningful pieces of information that will generate more profitable income for the company. This is the primary objective of SQL – to access and manipulate data that will further lead to business insights. This flexible computer language has been the most widely used communication tool in handling databases (specifically relational databases that will be further discussed in Chapter 3 of this book).
Try to imagine that you are going to a foreign country for a vacation. You may need to learn that country’s language to find your way around as you explore the new place. When you try ask someone for directions, who is local to that place and only speaks the country’s language, then surely you will have a hard time understanding him. In this scenario, the foreign land will be your database in which you need to seek information while SQL is the language that you will use to get what you need from the database.
From time to time, you will encounter the term query , which is also a part of the abbreviation of SQL. Query is basically the question written using an SQL statement that is being asked from the database. SQL then retrieves the needed information when any of the data in the database meets the requirements of the conditions of the given query. So in real-life applications, such as an online store, when you execute your query for a specific item by entering your search criteria, SQL programming usually takes place in the background to manage the database connections. You are actually telling the database, through the help of SQL, what information you want to see and how you want it to be presented to you.
People Using SQL
SQL is not only applicable for IT professionals or geeks who possess remarkable programming skills. With the growing corporate world of today, non-IT personnel such as businessmen and managers, can also benefit from learning the semantics of SQL. This is because the computer language enables them to understand the ins and outs of their businesses using the data that drives every company. Moreover, it opens several career opportunities in the analytical, managerial, strategic or research fields
In this chapter you have learnt an overview of SQL – its history, its primary purpose why it was created and those who will profit from learning this powerful database software. In the next chapter you will learn the essentials of database, which is the primary reason why SQL was designed in the first place.
Whether a database contains a simple collection of a few records or a massive system composed of millions of records, it can be categorized into three types: personal, workgroup or departmental, and enterprise. Each category is characterized by the database size, the machinery size into which the database runs and how big the organization that manages it.
Relational Database Fundamentals
Taking the discussion further into a more technical aspect, a relational database is an entity consisting of logical units known as tables. This relational database model was first formulated by Dr. E. F. Codd in 1970. How the tables are related to each other defines their relationships. In this scenario, data is simplified into smaller yet more logical and manageable units that optimize the database performance. The following figure shows an illustration on how the various components of a relational database are connected to each other.
A table consists of rows and columns that store data. In a relational database, these tables are related to one another improving the data retrieval process when a query is submitted by the user. For you to clearly picture out this idea, convert the information found on the calling card holder into a spreadsheet like a Microsoft Excel file. Assume that these are your contacts from companies that have ordered products and services from your business. You will have at least a CUSTOMER TABLE (containing all important information about your contacts) similar to the following:
1 Kathy Ale President Tile Industrial TX 3461234567 2 Kevin Lord VP Best Tooling NY 5181234567 3 Kim Ash Director Car World CA 5101234567 4 Abby Karr Manager West Mart NV 7751234567
You will also have an ORDER TABLE that will store information such as order ID, date, quantity and more. Check the following table:
It was during the late 1960s when the first commercial DBMS appeared. It evolved from file systems that basically provided data storage for over a certain period of time. Though such systems were capable of storing tremendous amount of data, computer professionals still have to face problems of data loss and an inefficient information retrieval system. There were also issues on control access where errors occur when two users modify the same file at the same time. Some examples of these applications are airline reservation systems, banking systems and corporate record keeping systems, among others.
With the advancement of technology in the market today, you can find numerous programs that will be suitable for your DBMS requirements. There are applications that run on a small scale level like your personal computer or tablet. Some are built to run on a large and powerful equipment, like those being used by multinational companies. Nevertheless, the on-going trend is for DBMS to be executed on multiple platforms or machines (whether large or small) that are interconnected to one another, forming an immense scalable network. IT experts have also found ways on how store data using Internet technology in powerful data centers or more popularly known as clouds. This cloud can be public entity (managed by a large company like Microsoft or Google) or a private one (maintained and stored via the intranet within an organization).
In this chapter you have learnt the essential features of a database and how it transforms to become a relational database. Also, you had an overview of the history and characteristics of database management systems. In the next chapter you will learn the fundamental structure of the SQL language and its components.
In this chapter you will learn the fundamental features of the SQL language and an overview of its programming aspect. In addition, you will be presented with a step-by-step instruction on where and how to download SQLite, a version of the SQL software that will be used all throughout the discussion of this e-Book.
SQL Fundamental Features
SQL is a flexible computer language that you can deploy in different ways to communicate with relational databases. This software has some distinct features that differentiates it from other programming applications. First and foremost, SQL is a nonprocedural language. Most computer programs (e.g., C, C++ and Java) solve problems by following a sequence of commands that is called a procedure. In this case, one specific operation is performed after another until the required task has been accomplished. The flow of operation can either be a linear sequence or a looping one, depending on what the programmer had specified. This is not the same for SQL. In using this application, you will just have to specify the output that you want, not how you want to generate the output. From the CUSTOMER TABLE, if you want to create a separate list of contacts whose company are located in Texas then you have to retrieve the rows where the STATE column contains “TX” as its value. In writing the SQL command, you don’t have to indicate how the information should be retrieved. It is the primary role of the database management system to examine the database and decide how to generate the results you wanted.
Learning the SQL syntax is like understanding the English language structure. Its command language, comprised of a limited number of statements, performs three primary data functions - definition, manipulation and control. The SQL programming language also includes reserved words that are only to be used for specific purposes. Thus, you cannot use these words as names for variables, tables and columns; or in any other way apart from their intended use. Below are some of the most common reserved words in SQL:2011.
with defined table characteristics, column constraints determine its domain. Using the same STATE column as an example with the given constraint of only the four values, if a database user enters “NJ” for New Jersey, then the entry will not be accepted. The system will not proceed until a valid value is entered for the STATE attribute, unless the database structure needs to be updated due to sudden business changes.
SQL Command Types
Before you start programming in SQL, you need to understand its basic command categories in performing various functions – database creation, object manipulation, data population and update, data deletion, query submission, access control and database administration, among others. The following are the main categories:
Data Definition Language (or simply DDL) enables you to create, change or restructure, and even destroy the basic elements that are contained in a relational database. DDL focuses only on the structure, not the data contained within the elements. These basic elements or data objects include tables, schemas, views and more. Having no independent physical existence, a view is regarded as a virtual table in which its definition only exists in the metadata. However, the view’s data comes from the table (or tables) where you will derive the view. Stated below are some of the most common DDL commands:
◦ CREATE – This command statement is responsible for building the database structure. Its syntax is:
◦ ALTER – This command statement is in charge of changing the database structure after it has been created. Its syntax is:
◦ DROP – This command is the reverse of the CREATE statement, which destroys the database structure. Its syntax is:
Data Manipulation Language (or simply DML) consists of SQL commands that handle data maintenance functions. This means that you are able to manipulate the data contained within the relational database objects. The command statements, which read like normal English sentences, will allow you to enter, change, remove or retrieve data. The following are the DML statements commonly used:
◦ INSERT – This command statement is used to insert new data values into a certain table. To add values into a table with two columns, use the following syntax:
VALUES (‘value1’, ‘value2’);
TABLE_NAME is the name of the table where you will be adding the new values. The number of items inside the VALUES parenthesis represents the number of columns of the table, which are arranged in the same order as the said columns. If the values are of character or date/time data types, they need to be enclosed by single quotation marks. This is not required for numeric or null values (the null value should be written as NULL).
◦ UPDATE – This command statement is used to modify or alter pre-existing data values in a table, not add or remove records. The update is done one table at a time or multiple rows/columns of one table within a database. To change a single column, use the following syntax:
SET COLUMN_NAME = ‘value’ [WHERE CONDITION ];
As long as the given WHERE clause is satisfied, then the value of the COLUMN_NAME will be updated. This could be within one or multiple records of the given TABLE_NAME.
◦ DELETE – This command statement deletes certain records or even the entire table, not data values from specific columns. To remove a single row or multiple records from a table, use the following syntax:
◦ REVOKE – This statement revokes your privileges, like removing your permission to access the database. Its syntax is:
Transactional control commands allow users to manipulate various transactions in maintaining database integrity. In SQL, transactions begin when applications are executed. The very first transaction is started at the onset of the SQL application, while the last transaction is ended when the application is terminated. The following are the common transactional control commands:
◦ COMMIT – This statement completes a transaction by making the changes you made to the database permanent, or simply saving the transactions. Its syntax is:
In the previous command line, the keyword WORK is optional.
◦ ROLLBACK – This statement’s primary function is to restore the database system to its previous state or undo all the actions that took place in the transaction log. Its syntax is:
In the previous command line, the keyword WORK is optional.
◦ SAVEPOINT – This statement works with the ROLLBACK command, wherein it creates sections or points within groups of transactions in which you will be performing the ROLLBACK command. Its syntax is:
SQLite Installation Instructions and Database Features
Before you start overwhelming yourself with various database solutions and SQL command lines, you need to determine first your purpose why you are creating a database. This will further determine other database design considerations such as size, complexity, type of machine where the application will run, storage medium and more. When you start thinking of your database requirements, you need to know up to what level of detail should be considered in your design. Too much detail will result to a very complex design that further wastes time and effort, and even your computer’s storage space. Too little will lead to a poor performing, corrupt and worthless database. Once you are done with the design phase, then you can decide which database software you can download to start your SQL experience.
For the sake of this e-Book’s discussion, SQLite , a simple software library, will be used as a starter database engine to design, build and deploy applications. A free and stand-alone database software that is quick to download and easy to administer, SQLite was developed by Richard Hipp and his team of programmers. It is was designed so that it can be easily configured and implemented, which does not require any client-server setup at all. Thus, SQLite is considered as one of the most widely used database software applications in the world.
Stated below are some of the major features of SQLite:
In using SQLite, you need to download SQLiteStudio as your database manager and editor. With its intuitive interface, this software is very light yet fast and powerful. You don’t even need to install it, just download, unpack and run the application. Follow these simple steps in downloading SQLiteStudio on a Windows 10 computer: