The lesson will cover the sql query language: the basics of sql language syntax, working in phpMyAdmin and service for online checks sql queries

Database- centralized data warehouse providing storage, access, primary processing and information search.

Databases are divided into:

  • Hierarchical
  • Network
  • Relational
  • Object-oriented

SQL (Structured Query Language)— is a structured query language (translation from English). The language is focused on working with relational (tabular) databases. The language is simple and, in essence, consists of commands (interpreted), through which you can work with large amounts of data (databases), deleting, adding, changing information in them and carrying out convenient searches.

To work with SQL code, you need a database management system (DBMS), which provides functionality for working with databases.

Database Management System(DBMS) - a set of language and software, intended for creating, maintaining and sharing DB by many users.

Usually used for training DBMS Microsoft Access , but we will use a system more common in the web sphere - . For convenience, you will use the web interface or online service for building sql queries, the principle of working with which is described below.

Important: When working with relational or tabular databases, we will call table rows records, and the columns are fields.

Each column must have its own data type, i.e. must be intended for entering data of a certain type. are described in one of the lessons in this course.

Components of the SQL language

The SQL language consists of the following components:

  1. Data Manipulation Language (DML);
  2. Data Definition Language (DDL);
  3. Data Control Language (DCL).

1.
The data manipulation language consists of 4 main commands:

  • fetching data from the database -
  • inserting data into a database table -
  • updating (changing) data in database tables -
  • deleting data from the database -

Data Definition Language used to create and modify the structure of the database and its components - tables, indexes, views (virtual tables), as well as triggers and stored procedures.

We will look at just a few of them basic language commands. They are:

  • creating a database - CREATE DATABASE
  • creating a table - CREATE TABLE
  • changing a table (structure) - ALTER TABLE
  • deleting a table - DROP TABLE

Data management language is used to manage access rights accessing data and executing procedures in a multi-user environment.

How to make sql query in phpmyadmin

  1. Launch the start denwer shortcut.
  2. IN address bar In your browser, type http://localhost/tools/phpmyadmin.
  3. In the left part of the window, select the database of interest or create it (if not already created). Creating a database in phpmyadmin is covered.
  4. If you know the table with which the query will work, select this table in the left part of the window.
  5. Select the SQL tab and start entering a query.

Creating a database in phpmyadmin

First you need to complete the first two points from.
Then:

  • in the web interface that opens, select the tab Database;
  • in field Create a database enter the name of the database;
  • click on the button Create;
  • Now, to continue working in phpMyAdmin in the created database, you can go to .

Working in the sql fiddle service

Online checking of sql queries is possible using the service.
The simplest way to organize work consists of the following steps:


Another example:


Now let's look at some points in more detail.

Creating tables:

Example: create three tables at once (teachers, lessons and courses); add multiple values ​​to each table.

* for those who are unfamiliar with the syntax - just copy the entire code and paste it into the left window of the service

* lesson on creating tables in SQL language

/*teachers*/ CREATE TABLE `teachers` ( ​​`id` INT (11 ) NOT NULL , `name` VARCHAR (25 ) NOT NULL , `code` INT (11 ) , `zarplata` INT (11 ) , `premia` INT(11), PRIMARY KEY(`id`)); INSERT INTO teachers VALUES (1 , "Ivanov" , 1 , 10000 , 500 ) , (2 , "Petrov" , 1 , 15000 , 1000 ) , (3 , "Sidorov" , 1 , 14000 , 800 ) , (4 , " Bobrova", 1, 11000, 800); /*lessons*/ CREATE TABLE `lessons` ( `id` INT (11 ) NOT NULL , `tid` INT (11 ) , `course` VARCHAR (25 ) , `date` VARCHAR (25 ) , PRIMARY KEY (`id ` ) ) ; INSERT INTO lessons VALUES (1 , 1 , "php" , "2015-05-04" ) , (2 , 1 , "xml" , "2016-13-12" ) ; /*courses*/ CREATE TABLE `courses` ( `id` INT (11 ) NOT NULL , `tid` INT (11 ) , `title` VARCHAR (25 ) , `length` INT (11 ) , PRIMARY KEY (`id ` ) ) ; INSERT INTO courses VALUES (1, 1, "php" , 54 ) , (2 , 1 , "xml" , 72 ) , (3 , 2 , "sql" , 25 ) ;

/*teachers*/ CREATE TABLE `teachers` (`id` int(11) NOT NULL, `name` varchar(25) NOT NULL, `code` int(11), `zarplata` int(11), `premia` int(11), PRIMARY KEY (`id`)); insert into teachers values ​​(1, "Ivanov",1,10000,500), (2, "Petrov",1,15000,1000) ,(3, "Sidorov",1,14000,800), (4," Bobrova",1,11000,800); /*lessons*/ CREATE TABLE `lessons` (`id` int(11) NOT NULL, `tid` int(11), `course` varchar(25), `date` varchar(25), PRIMARY KEY (`id `)); insert into lessons values ​​(1,1, "php","2015-05-04"), (2,1, "xml","2016-13-12"); /*courses*/ CREATE TABLE `courses` (`id` int(11) NOT NULL, `tid` int(11), `title` varchar(25), `length` int(11), PRIMARY KEY (`id `)); insert into courses values ​​(1,1, "php",54), (2,1, "xml",72), (3,2, "sql",25);

As a result, we get tables with data:

Sending a request:
In order to test the functionality of the service, add the request code to the right window.

Example: using a query, select all data from the teachers table related to the teacher with the last name Ivanov

On further SQL lessons the same scheme will be used, so you will just need to copy the scheme and paste it into the left window of the service.

For online visualization of the database schema, you can use the service https://dbdesigner.net/:

  1. Create your account (log in if you already have one).
  2. Click the button Go to Application.
  3. Menu Schema -> Import.
  4. Copy and paste the code for creating and filling database tables into the window that appears

Section 4 Information systems

Introduction to SQL.

Creating, modifying and deleting tables.

Retrieving data from a table.

Creating SQL queries.

Data processing in SQL.

Methods of teaching this topic at school.

Introduction to SQL. SQL is a structured query language that makes it possible to create and work in relational databases, which are sets of related information stored in tables. The language is focused on operations with data presented in the form of logically interconnected sets of relation tables. The most important feature of the structures of this language is its focus on the final result of data processing, and not on the procedure for this processing. SQL itself determines where the data is located, the indexes, and even what the most efficient sequence of operations should be used to obtain the result.

Initially, SQL was the main way the user worked with the database and allowed the following set of operations to be performed: creating a new table in the database; adding new records to the table; changing records; deleting records; selecting records from one or more tables (in accordance with a given condition); changing table structures.

Over time, SQL provided the ability to describe and manage new stored objects (such as indexes, views, triggers, and stored procedures). SQL remains the only communication mechanism between application software and a database. At the same time, modern DBMSs, as well as Information Systems DBMS systems provide the user with developed means of visual query construction. Every SQL statement is either request data from the database, or a call to the database that leads to a change in the data in the database.

In accordance with what changes occur in the database, the following types of requests are distinguished: to create or change new or existing objects in the database; to receive data; to add new data (records); to delete data; calls to the DBMS.

The main storage object of a relational database is a table, so all SQL queries are operations on tables. In accordance with this, requests are divided into:

Queries that operate on the tables themselves (creating and changing tables);

Queries that operate on individual records (or table rows) or sets of records.

Each table is described in the form of a listing of its fields (table columns) indicating: the type of values ​​stored in each field; connections between tables (setting primary and secondary keys); information necessary to build indexes.



Thus, using SQL essentially comes down to generating all sorts of row samples and performing operations on all the records included in the set.

SQL commands are divided into the following groups:

1. Commands of the data definition language - DDL (Data Definition Language). These SQL commands can be used to create, modify and delete various database objects.

2. Commands of the data control language - DCL (Data Control Language). Using these SQL commands, you can control user access to the database and use specific data (tables, views, etc.).

3. Transaction control language commands - TCL (Tganstation Control Language). These SQL commands allow you to determine the outcome of a transaction.

4. Commands of the data manipulation language - DML (Data Manipulation Language). These SQL commands allow the user to move data into and out of the database.

SQL statements are divided into:

Data definition operators ( Data Definition Language, DDL)

CREATE creates a database object (the database itself, table, view, user, etc.)

ALTER changes the object

DROP removes an object

Data manipulation operators ( Data Manipulation Language, DML)

SELECT reads data that meets specified conditions

INSERT adds new data

UPDATE modifies existing data

DELETE deletes data

Data access definition operators ( Data Control Language, DCL)

GRANT grants a user (group) permissions to certain operations on an object

REVOKE revokes previously issued permissions

DENY specifies a prohibition that takes precedence over permission

Transaction control statements ( Transaction Control Language, TCL)

COMMIT applies the transaction.

ROLLBACK rolls back all changes made in the context of the current transaction.

SAVEPOINT divides a transaction into smaller sections.

Advantages: 1. Independence from a specific DBMS (SQL query texts containing DDL and DML can be quite easily transferred from one DBMS to another). 2. Availability of standards (the presence of standards and a set of tests to determine the compatibility and compliance of a specific SQL implementation with a generally accepted standard only contributes to the “stabilization” of the language). 3. Declarative (using SQL, the programmer describes only what data needs to be extracted or modified)



Flaws: 1. Inconsistency with the relational data model 2. Repeated rows 3. Nulls 4. Explicitly specifying the order of columns from left to right 5. Unnamed columns and duplicate column names 6. Lack of support for the “=” property 7. Use of pointers 8. High redundancy

2.2 Creating, changing and deleting tables.

Creating a table:

Tables are created with the CREATE TABLE command. This command creates an empty table - a table with no rows. Values ​​are entered using the DML INSERT command. The CREATE TABLE command basically defines tables by describing a set of column names specified in a specific order. It also defines data types and column sizes. Every table must have at least one column.

Command syntax:

CREATE TABLE

( [()],

[()] ...);

Changing the table:

The ALTER TABLE command is a meaningful form, although its capabilities are somewhat limited. It is used to change the definition of an existing table. Typically, it adds columns to a table. It may sometimes remove or resize columns, and in some programs, add or remove constraints. The typical syntax to add a column to a table is:

ALTER TABLE

ADD

;

The column will be added with a NULL value for all rows in the table. The new column will become the last column in the table. In fact, you can add several new columns at once, separated by commas, in one command. It is possible to delete or change columns. Most often, changing a column may simply be increasing its size, or adding (removing) a constraint.
Delete table:

You must be the creator of the table to be able to delete it. So don't worry about accidentally destroying your data, SQL will first require you to clean up the table before it deletes it from the database. A table with rows in it cannot be deleted. The syntax for deleting your table, assuming it is empty of course, is as follows:

DROP TABLE< table name >;

2.3 Retrieving data from a table

SELECT - DML statement SQL language, returning a set of data (sample) from a database that meets a given condition. In most cases, sampling is carried out from one or more tables. When forming a SELECT query, the user describes the expected data set: its type (set of columns) and its contents (criteria for a record to be included in the set, grouping of values, order in which records are displayed, etc.).

The request is executed as follows: First, all records from the table are retrieved, and then for each record in the set, its compliance with the specified criterion is checked. If a merge of several tables is carried out, then a product of the tables is first compiled, and only then the required records are selected from the resulting set.

Request format using this operator:

SELECT list of fields FROM list of tables WHERE conditions...

(*[list of fields]) FROM<список таблиц>

WHERE - used to determine which rows should be selected or included in the GROUP BY.

GROUP BY - Used to combine rows with common values ​​into elements of a smaller set of rows.

HAVING - used to determine which rows after GROUP BY should be selected.

ORDER BY - used to determine which columns are used to sort the result set of data.

The same set of data can be obtained by executing different queries. Finding the optimal execution plan of this request is the task of the optimizer.

2.4 Creating SQL queries.

Query is a means of choice necessary information from the database. A question generated in relation to a database is a query. SQL queries are queries that are compiled (by programmers) from a sequence of SQL statements. These instructions specify what must be done with the input data set to generate the output set. All Access queries builds on the basis of SQL queries; to view them, you need to active window design the query, run the View/SQL command.

There are several types of queries: select (given above), update, add, delete, cross-query, create tables. The most common is a sample request. Select queries are used to select required by the user information contained in the tables. They are created only for related tables.

In order to create a query in SQL, we first create an empty query in design mode. To do this, on the Create tab, select Query Builder. Next, on the Design tab, select Mode and change the query building mode to SQL Mode.

Creating a query in SQL mode is similar to programming; the user must manually type the necessary commands. A request to select information begins with the word SELECT. Next, you need to list the required fields of the table, print the name of the table, put a dot, and in square brackets type the name of the field from this table, etc. Fields are separated by commas.

Request ends official word FROM, followed by the table from which the information was taken. There must be a semicolon at the end of the query string.

To check that the request is written correctly, let's execute it. To do this, click on the Run button on the Design panel. The result of the request will be displayed in the table. If the request was written incorrectly, an error message will be received. In this case, you first need to check the correct spelling of table and field names.

2.5 Data processing in SQL:

Queries can perform generalized group processing of field values, which is implemented using aggregate functions. The following aggregate functions are allowed in SQL:

COUNT - counts the number of rows or non-NULL field values ​​that the query selected;

SUM - calculates the arithmetic sum of all selected values of this field;

AVG - averages all selected values ​​of this field;

MAX - finds and returns the largest of all selected values

MIN - Finds and returns the smallest of all selected values ​​for a given field.

To organize the display of fields SQL tables uses the ORDER BY command, allowing you to sort the query output according to the values ​​in the number of columns you select.

To put multiple queries together and combine the output, use the UNION clause. The UNION clause combines the output of two or more SQL queries into a single set of rows and columns. The UNION command automatically eliminates duplicate rows from the output.

Computer networks

Are you new to programming or have you simply avoided learning SQL in the past? Then you have come to the right address, since any developer is eventually faced with the need to know this query language. You may not be the main database designer, but it is almost impossible to avoid working with them. I hope this brief overview of basic SQL query syntax will help the interested developer and anyone else who needs it.

What is a SQL database?

Structured Query Language is a database communication standard that is supported by ANSI. The most latest version– SQL-99, although a new standard, SQL-200n, is already in development. Most databases adhere firmly to the ANSI-92 standard. There has been much discussion about introducing more modern standards, but commercial database vendors are moving away from this by developing their own new concepts for storing stored data. Almost every single database uses some unique set of syntax, although very similar to the ANSI standard. In most cases, this syntax is an extension of the base standard, although there are cases where this syntax produces different results for different databases. It is always a good idea to review the database documentation, especially if you are getting unexpected results.

If you're new to SQL, there are some basic concepts you need to understand.

In general terms, "SQL database" is the generic name for a relational database management system (RDBMS). For some systems, "database" also refers to a group of tables, data, or configuration information that is an inherently separate part from other, similar constructs. In this case, each installation SQL database data may consist of several databases. In other systems, they are referred to as tables.

A table is a database structure that consists of columns containing rows of data. Typically, tables are created to contain related information. Multiple tables can be created within the same database.

Each column represents an attribute or set of attributes of objects, such as employee identification numbers, height, car color, etc. Often the term field is used to refer to a column, followed by a name, such as “in the Name field.” A row field is the minimum element of a table. Each column in a table has a specific name, data type, and size. Column names must be unique within the table.

Each row (or record) represents a collection of attributes of a specific object, for example, a row may contain the employee’s identification number, his salary, his year of birth, etc. Table rows do not have names. To access a specific row, the user must specify some attribute (or set of attributes) that uniquely identifies it.

One of the most important operations that are performed when working with data is retrieving information stored in the database. To do this, the user must execute a query.

Now let's look at the main types of database queries that focus on manipulating data within the database. For our purposes, all examples are provided in standard SQL to suit any environment.

Types of Data Queries

There are four main types of data queries in SQL, which fall under the so-called Data Manipulation Language (DML):
SELECT – select rows from tables;
INSERT – add rows to the table;
UPDATE – change rows in the table;
DELETE – delete rows in the table;

Each of these queries has different operators and functions that are used to perform some actions on the data. The SELECT query has the largest number of options. There are also additional query types used in conjunction with SELECT, such as JOIN and UNION. But for now, we will focus only on basic queries.

Using a SELECT query to select the data you want

To retrieve information stored in the database, a SELECT query is used. The basic effect of this query is limited to a single table, although there are designs that allow you to select from multiple tables at the same time. In order to get all rows of data for specific columns, a query like this is used:
SELECT column1, column2 FROM table_name;

You can also get all columns from a table using the wildcard character "*":
SELECT * FROM table_name;

This can be useful when you are going to select data with a specific WHERE clause. The following query will return all columns from all rows where "column1" contains the value "3":
SELECT * FROM table_name WHERE column1=3;

In addition to “=” (equals), there are the following conditional operators:

Conditional statements:
= Equal
<>Not equal
> More
< Меньше
>= Greater than or equal to
<= Меньше или равно

Additionally, you can use the BITWEEN and LIKE conditions to compare with the WHERE condition, as well as combinations of the AND and OR operators.
SELECT * FROM table_name WHERE ((Age >= 18) AND (LastName BETWEEN 'Ivanov' AND 'Sidorov')) OR Company LIKE '%Motorola%';

What in Russian translation means: select all columns from the table_name table, where the value of the age column is greater than or equal to 18, and the value of the LastName column is in the alphabetical range from Ivanov to Sidorov inclusive, or the value of the Company column is Motorola.

Usage INSERT query to insert new data

The INSERT query is used to create new line data. To update existing data or empty row fields, you must use an UPDATE query.

Example INSERT query syntax:
INSERT INTO table_name (column1, column2, column3) VALUES ('data1', 'data2', 'data3');

If you are going to insert all the values ​​in the order in which the table columns appear, you can omit the column names, although this is preferable for readability. Also, if you list columns, you don't have to list them in the order they appear in the database, as long as the values ​​you enter match that order. You should not list columns that do not contain information.

Already existing information in the database is changed in a very similar way.

UPDATE Query and WHERE Condition

UPDATE is used to change existing values ​​or free a field in a row, so the new values ​​must match existing type data and provide acceptable values. If you do not want to change the values ​​in all rows, then you need to use the WHERE clause.
UPDATE table_name SET column1 = 'data1', column2 = 'data2' WHERE column3 = 'data3';

You can use WHERE on any column, including the one you want to change. This is used when it is necessary to replace one specific value to another.
UPDATE table_name SET FirstName = 'Vasily' WHERE FirstName = 'Vasily' AND LastName = 'Pupkin';

Be careful! DELETE query deletes entire rows

A DELETE query completely deletes a row from the database. If you want to delete one single field, then you need to use an UPDATE request and set this field to a value that will be analogous to NULL in your program. Be careful to limit your DELETE query to a WHERE clause, otherwise you may lose the entire contents of the table.
DELETE FROM table_name WHERE column1 = 'data1';

Once a row has been deleted from your database, it cannot be recovered, so it is advisable to have a column named "IsActive", or something like that, which you can change to null, which will indicate that the view of the data from that row is locked.

Now you know the basics of SQL queries

SQL is a database language and we have covered the most important and basic commands used in data queries. There are a lot of core concepts that weren't covered (SUM and COUNT, for example), but the few commands we managed to list above should encourage you to get active and dig deeper into the wonderful query language called SQL.

I have already written about a variety of SQL queries, but it's time to talk about more complex things, for example, SQL query for selecting records from several tables.

When you and I made a selection from one table, everything was very simple:

SELECT names_of_required_fields FROM table_name WHERE selection_condition

Everything is very simple and trivial, but sampling from several tables at once It's getting a little more complicated. One difficulty is matching field names. For example, every table has a field id.

Let's look at this query:

SELECT * FROM table_1, table_2 WHERE table_1.id > table_2.user_id

Many who have not dealt with such queries will think that everything is very simple, thinking that only the table names have been added before the field names. In fact, this avoids conflicts between identical field names. However, the difficulty lies not in this, but in algorithm for such an SQL query.

The working algorithm is as follows: the first record is taken from table_1. Takes id this entry from table_1. Below is the full table table_2. And all records are added where the field value user_id less id selected entry in table_1. Thus, after the first iteration it may appear from 0 to infinite number resulting records. At the next iteration, the next table record is taken table_1. The entire table is scanned again table_2, and the sampling condition is triggered again table_1.id > table_2.user_id. All records that meet this condition are added to the result. The output can be a huge number of records, many times larger than the total size of both tables.

If you understand how it works after the first time, then it’s great, but if not, then read until you fully understand it. If you understand this, then it will be easier.

Previous SQL query, as such, is rarely used. It was just given for explanations of the algorithm for selecting from several tables. Now let's look at the more squat one SQL query. Let's say we have two tables: with goods (there is a field owner_id responsible for id product owner) and with users (there is a field id). We want one SQL query get all the records, and each one contains information about the user and his one product. The next entry contained information about the same user and his next product. When this user's products run out, move on to the next user. So we have to join two tables and get a result where each record contains information about the user and one of his products.

A similar query will replace 2 SQL queries: to select separately from the table with products and from the table with users. In addition, such a request will immediately match the user and his product.

The request itself is very simple (if you understood the previous one):

SELECT * FROM users, products WHERE users.id = products.owner_id

The algorithm here is already simple: the first record from the table is taken users. Next it is taken id and all records from the table are analyzed products, adding to the result those with owner_id equals id from the table users. Thus, in the first iteration, all goods from the first user are collected. At the second iteration, all products from the second user are collected, and so on.

As you can see, SQL queries for selecting from several tables not the simplest, but the benefits from them can be enormous, so knowing and being able to use such queries is very desirable.

Each of us regularly encounters and uses various databases. When we choose an address Email, we are working with a database. Databases use search services, banks for storing customer data, etc.

But despite the constant use of databases, even for many developers software systems There are many “blank spots” left due to different interpretations of the same terms. We'll give a brief definition of basic database terms before covering the SQL language. So.

Database - a file or collection of files for storing ordered data structures and their relationships. Very often a management system is called a database - it is only a repository of information in a specific format and can work with various DBMSs.

Table - Let's imagine a folder in which documents are stored, grouped according to a certain characteristic, for example, a list of orders for the last month. This is a table in a computer. A separate table has its own unique name.

Data type - the type of information allowed to be stored in a separate column or row. These can be numbers or text of a certain format.

Column and Row- We have all worked with spreadsheets, which also have rows and columns. Any relational database works with tables in a similar way. Rows are sometimes called records.

Primary key- Each row of a table can have one or more columns to uniquely identify it. Without a primary key, it is very difficult to update, change, and delete relevant rows.

What is SQL?

SQL(English - structured query language) was developed only for working with databases and in currently is a standard for all popular DBMSs. The language syntax consists of a small number of operators and is easy to learn. But despite external simplicity, it allows you to create SQL queries for complex operations with a database of any size.

Since 1992, there has been a generally accepted standard called ANSI SQL. It defines the basic syntax and functions of operators and is supported by all DBMS market leaders, such as ORACLE. It is impossible to consider all the capabilities of the language in one short article, so we will briefly consider only basic SQL queries. Examples clearly show the simplicity and capabilities of the language:

  • creating databases and tables;
  • data sampling;
  • adding records;
  • modification and deletion of information.

SQL Data Types

All columns in a database table store the same data type. Data types in SQL are the same as in other programming languages.

We create tables and databases

There are two ways to create new databases, tables and other queries in SQL:

  • via the DBMS console
  • Using the interactive administration tools included with the database server.

Created new base operator data CREATE DATABASE<наименование базы данных>; . As you can see, the syntax is simple and concise.

We create tables inside the database using the CREATE TABLE statement with the following parameters:

  • table name
  • column names and data types

As an example, let's create a Commodity table with the following columns:

Create a table:

CREATE TABLE Commodity

(commodity_id CHAR(15) NOT NULL,

vendor_id CHAR(15) NOT NULL,

commodity_name CHAR(254) NULL,

commodity_price DECIMAL(8,2) NULL,

commodity_desc VARCHAR(1000) NULL);

The table consists of five columns. After the name comes the data type, the columns are separated by commas. The column value can accept empty values ​​(NULL) or must be filled (NOT NULL), and this is determined when the table is created.

Retrieving data from a table

The data fetch operator is the most commonly used SQL query. To obtain information, we must indicate what we want to select from such a table. First a simple example:

SELECT commodity_name FROM Commodity

After SELECT statement we specify the name of the column to obtain information, and FROM specifies the table.

The result of the query will be all rows of the table with Commodity_name values ​​in the order in which they were entered into the database, i.e. without any sorting. An additional ORDER BY clause is used to order the result.

To query for several fields, list them separated by commas, as in the following example:

SELECT commodity_id, commodity_name, commodity_price FROM Commodity

It is possible to get the value of all columns of a row as a query result. To do this, use the “*” sign:

SELECT * FROM Commodity

  • Additionally SELECT supports:
  • Sorting data (ORDER BY operator)
  • Selection according to conditions (WHERE)
  • Grouping term (GROUP BY)

Add a line

To add a row to a table, SQL queries with the INSERT operator are used. Addition can be done in three ways:

  • add a new whole line;
  • string part;
  • query results.

To add a full row, you must specify the table name and the values ​​of the columns (fields) of the new row. Here's an example:

INSERT INTO Commodity VALUES("106 ", "50", "Coca-Cola", "1.68", "No Alcogol ,)

The example adds a new product to the table. Values ​​are listed after VALUES for each column. If not corresponding value for a column, you must specify NULL. The columns are populated with values ​​in the order specified when the table was created.

If you add only part of a row, you must explicitly specify the names of the columns, as in the example:

INSERT INTO Commodity (commodity_id, vendor_id, commodity_name)

VALUES("106 ", ‘50", "Coca-Cola",)

We entered only the identifiers of the product, supplier and its name, and left the remaining fields blank.

Adding Query Results

INSERT is primarily used to append rows, but can also be used to append the results of a SELECT statement.

Changing data

To change information in the fields of a database table, you must use the UPDATE statement. The operator can be used in two ways:

  • All rows in the table are updated.
  • Only for a specific line.

UPDATE consists of three main elements:

  • table in which changes need to be made;
  • field names and their new values;
  • conditions for selecting rows to change.

Let's look at an example. Let's say the price of a product with ID=106 has changed, so this line needs to be updated. We write the following operator:

UPDATE Commodity SET commodity_price = "3.2" WHERE commodity_id = "106"

We specified the name of the table, in our case Commodity, where the update will be performed, then after SET - the new value of the column and found the desired record by specifying the required ID value in WHERE.

To change multiple columns, the SET statement is followed by multiple column-value pairs separated by commas. Let's look at an example in which the name and price of a product are updated:

UPDATE Commodity SET commodity_name=’Fanta’, commodity_price = "3.2" WHERE commodity_id = "106"

To remove information in a column, you can assign it the value NULL if the table structure allows it. It must be remembered that NULL is precisely “no” value, and not zero in the form of text or number. Let's remove the product description:

UPDATE Commodity SET commodity_desc = NULL WHERE commodity_id = "106"

Removing rows

SQL queries to delete rows in a table are executed using the DELETE statement. There are two use cases:

  • Certain rows in the table are deleted;
  • All rows in the table are deleted.

An example of deleting one row from a table:

DELETE FROM Commodity WHERE commodity_id = "106"

After DELETE FROM we indicate the name of the table in which the rows will be deleted. The WHERE clause contains the condition by which rows will be selected for deletion. In the example, we delete the product line with ID=106. Specifying WHERE is very important because omitting this statement will delete all rows in the table. This also applies to changing the value of fields.

The DELETE statement does not specify column names or metacharacters. It deletes rows completely, but it cannot delete a single column.

Using SQL in Microsoft Access

Typically used interactively to create tables, databases, manage, modify, analyze data in a database and to implement SQL Access queries through a convenient interactive query designer (Query Designer), using which you can build and immediately execute SQL statements of any complexity.

The server access mode is also supported, in which the Access DBMS can be used as a generator of SQL queries to any ODBC data source. This feature allows Access applications to interact with any format.

SQL extensions

Since SQL queries do not have all the capabilities of procedural programming languages, such as loops, branching, etc., DBMS manufacturers are developing their own version of SQL with advanced capabilities. First of all, this is support for stored procedures and standard operators of procedural languages.

The most common dialects of the language:

  • Oracle Database - PL/SQL
  • Interbase, Firebird - PSQL
  • Microsoft SQL Server- Transact-SQL
  • PostgreSQL - PL/pgSQL.

SQL on the Internet

The MySQL DBMS is distributed under the free GNU General Public License. There is a commercial license with the ability to develop custom modules. How component included in the most popular assemblies of Internet servers, such as XAMPP, WAMP and LAMP, and is the most popular DBMS for developing applications on the Internet.

It was developed by Sun Microsystems and is currently supported by Oracle Corporation. Databases up to 64 terabytes in size, SQL:2003 syntax standard, replication of databases and cloud services are supported.