SECTION 4: Inserting, Updating And Deleting Data
There is too much buzz available in the technological world about data. However, raw data is not so useful when on its own. SQL is pronounced as “sequel”. It stands for Structured Query Language. It is typically a language that allows for communication through databases. This happens so that it can easily manage all the data contained within the databases.
In modern days, most businesses, both small and large corporations, make use of data in running their operations. All the data being used is managed through the use of databases. For this reason, there has been a very high demand for experts in database administration. Being skilled in database development, basically, SQL development proves to be essential.
Data continues to be the central part of numerous mobile applications and web applications. For instance, the Facebook application holds the information of a user’s profile as well as data on their posts and friends. To maintain this kind of data, a database system is applied. Therefore, Structured Query Language, is one of the programming languages that allow programmers to work with the available data.
Typically, databases are not designed to understand other programming languages. They know the SQL programming language only. It is a must for those intending to work on app development and web development to learn the SQL programming language. Like all programming languages, SQL possesses its typical markup. Due to this, programmers should tables allow programmers to store all the information they need.
Several SQL commands are frequently used. Programmers must be familiar with the commands for database work. Some of the commands that a programmers is required to write include:
CREATE DATABASE – This enables the creation of a database
CREATE TABLE – It allows for the creation of tables
SELECT – This is when a programmer finds and extracts data from a database
UPDATE – Helps in making adjustments and editing data
DELETE – It is written when deleting data Other than the above that are most commonly used, other complicated commands depend on the complexity of the database. It means that the more the complexity of the database, the number of commands the programmer is supposed to use also increases. The commands are used anytime queries are being written. They are referred to as the inquiries that allow programmers to operate the data present in the databases.
In simpler words, when the commands are entered within a database system, they are interpreted and processed by the system. The outcomes are, for instance, new records in the database or new databases are created. In most cases, the initial query is responsible for coming up with a new database.
SQL programming language is categorized into multiple different elements of language; they include:
Clauses – They are the constituent elements of queries and statements. There are instances where provisions are optional.
Expressions – They come up with scalar values or tables that consist of rows and columns holding data.
Predicates – They act as the specifications for conditions that have been evaluated to SQL 3VL (three-valued logic). It can also be in the form of Boolean truth values. They are used in limiting the effects brought about by queries and statements. They are also used in changing the flow of programs.
Queries – They are used when retrieving data based on specified criteria. Queries are essential elements to SQL.
Statements – They have consistent effects on data and schemata. They also control program flow, diagnostics, transactions, sessions and connections
All statements in SQL programming include a semicolon that is a symbol of statement termination. The semicolon is, however, not necessary on each platform. It is one of the standard parts of the SQL language.
Insignificant Whitespace – In most cases it is ignored in SQL queries and statements. This makes it simpler for the formatting of SQL and enhancing the readability of the language.
Inserting, Updating, and Deleting Data
In this section, the topic will be on how to include new records in a database, how to update the existing files, and how to delete the data you no longer need or use. Inserting data is one of the simplest tasks. It is because it starts with simple problems of inserting single rows. In many cases, it is, however, efficient to use set-based approaches when creating new rows. In such a case as a programmer, you will find numerous techniques that can help you insert multiple rows simultaneously.
Similar to inserting, updating and deleting start as simple tasks. A programmer can update a single record and delete a single record. One can as well update multiple files simultaneously using some more sophisticated methods. Similarly, there are numerous ways through which data can be deleted in SQL programming. For instance, one can delete entire rows in a table considering they are existing or not in another table.
SQL also has new additional ways through which programmers can insert, update, and delete data simultaneously. It is one of the best features that SQL programming language can offer.
Inserting New Data
The problem: In this, a programmer wants to add a new record into a given table. In such cases, the programmer uses the INSERT statement when inserting data into tables. The INSERT statement is also referred to as the INSERT INTO statement. It is used when inserting one or multiple rows into tables.
The SQL INSERT statement is used when inserting a single row into a table. Below is an illustration of the INSERT statement used in inserting one row into a table that already exists.
INSERT INTO table (column1, column2…)
VALUES (value1, value1,…)
When inserting a row into a table, a programmer is required to give three specifications. One is the table in which they want to insert the new row, in the INSERT, INTO clause. Second is a list of columns separated by a comma in the table and found within parentheses. The third is a list of values separated by a command surrounded by parentheses inside the values code. The list used for columns should have a similar number of elements as those in the values list. In cases where they fail to match, the SQL database engine will typically issue an error.
An example is using an INSERT statement when inserting new rows into a shipper’s table.
INSERT INTO shippers (company name, phone.)
VALUES (Alliance Shippers’, 1-680-199-6)
The two constants Alliance Shippers and 1-680-199-6 are identified in the VALUE clause. The SQL engine respectively inserts them into the company name and phone columns. After the execution of the statement, the server in the database brings back a message. That massage helps in indicating the number of rows that have been affected. In such a case, the programmers get a message that “1 row affected”. This message informs the programmers that there is one new row that has been successfully inserted.
The shipper’s ID column is not specified in the column list. This is because the column used for the shipper’s ID is an AUTOINCREMENT column. The next sequence is automatically generated by the database engine anytime a new row is inserted into the existing table.
SQL programming provides programmers with a shorter form of using the INSERT Statement to write lesser code.
INSERT INTO table
VALUES (value1, value2,…)
When this form is applied, the list used for values is supposed to have a similar order as the list of columns present in the table. When a programmer uses the INSERT INTO statement, they are required to supply all columns with values apart from the AUTOINCREMENT column. The best thing about practicing the use of column names in INSERT statements is that it helps to make code maintainable.
SQL INSERT statement can also allow programmers to insert multiple rows into an existing table. One can add the rows using one statement.
INSERT INTO table- name (column 1, column2,…)
VALUES (value1, value2 …)
In such a form, a programmer is required to provide numerous values’ lists with each list separated from the other using a comma. Below is an example of an INSERT statement used in inserting two rows into a shippers table.
INSERT INTO shippers (company name, phone number)
VALUES (ALL, 1-400-670-7569’)
(DHL’, 1-400-522-5435’)
SQL INSERT statement also can be used to copy the data table. Other than coming up with specifications of values’ lists, a programmer can make use of the SELECT statement in selecting values form other tables and supplying them to the INSERT statement. In such a case, the programmers can easily copy data from one particular table to another.
It is also possible for programmers to block users or delinquent software applications from inserting new values into particular columns in a table. In this case, a programmer comes up with a view on the specific table that exposes only the columns that they wish to unearth. All inserts are then forced to pass through the view. Access is allowed to only users and software that are eligible.
Updating Data in SQL Programming
The SQL UPDATE statement is used in updating the existing data in SQL tables. It is used in changing existing data in a single row or multiple rows present in a table. Below is an illustration of the UPDATE statement.
UPDATE table
SET
Column1= new_value1,
Column2= new_value2,
WHERE
Condition;
When updating data in a table, a programmer is required first to give a specification of the name of the table that they want to change data in the UPDATE clause. Secondly, include new values for the column that wants to be updated. When a programmer wants to update the data present in numerous columns, every column= value pair and a comma is used when separating the two. Thirdly the programmers should consider specifying the rows that want to be updated in the WHERE clause. The use of WHERE clause is optional and when omitted all the existing rows in a table are updated.
The SQL engines issue messages that specify the number of rows that are affected after the statement is executed. Below are some examples of the SQL UPDATE statements. We can use an example of an employee, Juliet having an id 3, and gets married. The employer is required to adjust the table to be accurate by changing her last name. The existing record name of Juliet may be as follows:
SELECT
Employed, Lastname, first name
FROM
Alliance-ph. Employees
WHERE
EMPLOYEE ID
Employee id last name first name
3 Jolie Juliet
The UPDATE statement will help in changing Juliet’s last name from Jolie to Francis
UPDATE employees
SET
Lastname- Francis
WHERE
EmployeeID=3
THE select statement is executed to help in verifying the change the will occur as:
Employee id last name first name
3 Francis Juliet
SQL Update Multiple Columns
There are cases where programmers will be required to update data existing in various columns. For instance, in the case of Juliet above, she moves to a new home after getting married. It will mean that the address filled in the original record will change. The UPDATE statement is used in the following way:
UPDATE employees
SET
Address= ‘1200 Carter St’
City= New York
Postalcode= 4093
Region= CA
WHERE
EmployeeID= 3
Deleting Data from SQL Table
The SQL DELETE statement is used when a programmer wants to delete existing data in a table. To successfully use the SQL DELETE statement, one needs to first come up with the table where the data should be removed in the DELETE FROM clause. Then one should put up conditions within the WHERE clause to help in specifying rows that are to be removed. When the WHERE clause is omitted, every row existing in the table is removed. It is also possible for programmers to delete all rows existing in a table without necessarily deleting the table.
<<<PREVIOUS SECTION||||||||||||||||||||||||||||NEXT SECTION>>>