A selection of materials for studying databases and SQL. Learning SQL

  • Tutorial

What is this tutorial about?

This tutorial is something like a “stamp of my memory” in the SQL language (DDL, DML), i.e. this is information that has accumulated along the way professional activity and is constantly stored in my head. This is a sufficient minimum for me, which is used most often when working with databases. If there is a need to use more complete SQL constructs, then I usually turn to the MSDN library located on the Internet for help. In my opinion, it is very difficult to keep everything in your head, and there is no particular need for this. But knowing the basic structures is very useful, because... they are applicable in almost the same form in many relational databases, such as Oracle, MySQL, Firebird. The differences are mainly in the data types, which may differ in detail. There are not many basic SQL constructs, and with constant practice they are quickly memorized. For example, to create objects (tables, constraints, indexes, etc.) it is enough to have at hand text editor environment (IDE) for working with a database, and there is no need to study visual tools tailored to work with a specific type of database (MS SQL, Oracle, MySQL, Firebird, ...). This is also convenient because all the text is in front of your eyes, and you don’t need to run through numerous tabs in order to create, for example, an index or constraint. At permanent job with a database, creating, changing, and especially re-creating an object using scripts is many times faster than if you do it in visual mode. Also in script mode (and, accordingly, with due care), it is easier to set and control the rules for naming objects (my subjective opinion). In addition, scripts are convenient to use when changes made in one database (for example, test) need to be transferred in the same form to another database (productive).

The SQL language is divided into several parts, here I will look at the 2 most important parts:
  • DML – Data Manipulation Language, which contains the following constructs:
    • SELECT – data selection
    • INSERT – inserting new data
    • UPDATE – data update
    • DELETE – deleting data
    • MERGE – data merging
Because I am a practitioner; there will be little theory as such in this textbook, and all constructions will be explained using practical examples. In addition, I believe that a programming language, and especially SQL, can only be mastered through practice, by experiencing it yourself and understanding what happens when you execute this or that construct.

This textbook was created according to the Step by Step principle, i.e. you need to read it sequentially and preferably immediately follow the examples. But if along the way you need to learn about a certain command in more detail, then use a specific search on the Internet, for example, in the MSDN library.

MS SQL database was used to write this tutorial. Server version 2014, I used MS SQL Server to execute scripts Management Studio(SSMS).

Briefly about MS SQL Server Management Studio (SSMS)

SQL Server Management Studio (SSMS) is a utility for Microsoft SQL Server for configuring, managing and administering database components. This utility contains a script editor (which we will mainly use) and a graphical program that works with server objects and settings. The main tool of SQL Server Management Studio is Object Explorer, which allows the user to view, retrieve, and manage server objects. This text partially borrowed from Wikipedia.

To create a new script editor, use the “New Query” button:

To change the current database you can use the drop-down list:

To execute a specific command (or group of commands), select it and press the “Execute” button or the “F5” key. If there is only one command currently in the editor, or you need to execute all commands, then you do not need to select anything.

After running scripts, especially those creating objects (tables, columns, indexes), to see the changes, use refresh from the context menu by highlighting the appropriate group (for example, Tables), the table itself, or the Columns group in it.

Actually, this is all we will need to know to complete the examples given here. The rest of the SSMS utility is easy to learn on your own.

A little theory

A relational database (RDB, or in the following context simply DB) is a collection of tables interconnected. Roughly speaking, a database is a file in which data is stored in a structured form.

DBMS – Database Management System, i.e. this is a set of tools for working with a specific type of database (MS SQL, Oracle, MySQL, Firebird, ...).

Note
Because in life, in colloquial speech, we mostly say: “Oracle DB”, or even just “Oracle”, actually meaning “Oracle DBMS”, then in the context of this textbook the term DB will sometimes be used. From the context, I think it will be clear what exactly we are talking about.

A table is a collection of columns. Columns can also be called fields or columns; all these words will be used as synonyms expressing the same thing.

The table is the main object of the RDB; all RDB data is stored row by row in the columns of the table. Lines and records are also synonyms.

For each table, as well as its columns, names are specified by which they are subsequently accessed.
The name of an object (table name, column name, index name, etc.) in MS SQL can have maximum length 128 characters.

For reference– in the ORACLE database, object names can have a maximum length of 30 characters. Therefore, for a specific database, you need to develop your own rules for naming objects in order to meet the limit on the number of characters.

SQL is a language that allows you to query a database using a DBMS. In a specific DBMS, the SQL language may have a specific implementation (its own dialect).

DDL and DML are a subset of the SQL language:

  • The DDL language is used to create and modify the database structure, i.e. to create/modify/delete tables and relationships.
  • The DML language allows you to manipulate table data, i.e. with her lines. It allows you to select data from tables, add new data to tables, as well as update and delete existing data.

In SQL, you can use 2 types of comments (single-line and multi-line):

One line comment
And

/* multiline comment */

Actually, this will be enough for the theory.

DDL – Data Definition Language

For example, consider a table with data about employees, in a form familiar to a person who is not a programmer:

IN in this case The columns of the table have the following names: Personnel number, Full name, Date of birth, E-mail, Position, Department.

Each of these columns can be characterized by the type of data it contains:

  • Personnel number – integer
  • Full name – string
  • Date of birth - date
  • Email – string
  • Position - string
  • Department - line
Column type is a characteristic that indicates what type of data a given column can store.

To begin with, it will be enough to remember only the following basic data types used in MS SQL:

Meaning Notation in MS SQL Description
Line variable length varchar(N)
And
nvarchar(N)
Using the number N, we can specify the maximum possible string length for the corresponding column. For example, if we want to say that the value of the “Name” column can contain a maximum of 30 characters, then we need to set it to type nvarchar(30).
The difference between varchar and nvarchar is that varchar allows you to store strings in ASCII format, where one character occupies 1 byte, and nvarchar stores strings in Unicode format, where each character occupies 2 bytes.
The varchar type should only be used if you are 100% sure that this field will not need to store Unicode characters. For example, varchar can be used to store addresses email, because they usually contain only ASCII characters.
Fixed length string char(N)
And
nchar(N)
This type differs from a variable-length string in that if the length of the string is less than N characters, then it is always padded on the right to a length of N with spaces and stored in the database in this form, i.e. in the database it takes up exactly N characters (where one character takes up 1 byte for char and 2 bytes for nchar). In my practice, this type is very rarely used, and if it is used, it is used mainly in the char(1) format, i.e. when a field is defined by a single character.
Integer int This type allows us to use only integers in the column, both positive and negative. For reference (now this is not so relevant for us), the range of numbers that the int type allows is from -2,147,483,648 to 2,147,483,647. Usually this is the main type that is used to specify identifiers.
Real or real number float If we talk in simple language, then these are numbers that may contain a decimal point (comma).
Date date If the column needs to store only the Date, which consists of three components: Day, Month and Year. For example, 02/15/2014 (February 15, 2014). This type can be used for the column “Date of admission”, “Date of birth”, etc., i.e. in cases where it is important for us to record only the date, or when the time component is not important to us and can be discarded or if it is not known.
Time time This type can be used if the column needs to store only time data, i.e. Hours, Minutes, Seconds and Milliseconds. For example, 17:38:31.3231603
For example, daily “Flight departure time”.
Date and time datetime This type allows you to simultaneously save both Date and Time. For example, 02/15/2014 17:38:31.323
For example, this could be the date and time of an event.
Flag bit This type is convenient to use to store values ​​of the form “Yes”/“No”, where “Yes” will be stored as 1, and “No” will be stored as 0.

Also, the field value, if it is not prohibited, may not be specified; the NULL keyword is used for this purpose.

To run the examples, let's create a test database called Test.

A simple database (without specifying additional parameters) can be created by running the following command:

CREATE DATABASE Test
You can delete the database with the command (you should be very careful with this command):

DROP DATABASE Test
In order to switch to our database, you can run the command:

USE Test
Alternatively, select the Test database from the drop-down list in the SSMS menu area. When working, I often use this method of switching between databases.

Now in our database we can create a table using the descriptions as they are, using spaces and Cyrillic characters:

CREATE TABLE [Employees]([Personnel number] int, [Name] nvarchar(30), [Date of birth] date, nvarchar(30), [Position] nvarchar(30), [Department] nvarchar(30))
In this case, we will have to enclose names in square brackets […].

But in the database, for greater convenience, it is better to specify all object names in Latin and not use spaces in names. In MS SQL, usually in this case each word begins with a capital letter, for example, for the “Personnel Number” field, we could set the name PersonnelNumber. You can also use numbers in the name, for example, PhoneNumber1.

Note
In some DBMSs, the following naming format “PHONE_NUMBER” may be more preferable; for example, this format is often used in the ORACLE database. Naturally, when specifying a field name, it is desirable that it does not coincide with the keywords used in the DBMS.

For this reason, you can forget about the square brackets syntax and delete the [Employees] table:

DROP TABLE [Employees]
For example, a table with employees can be named “Employees”, and its fields can be given the following names:

  • ID – Personnel number (Employee ID)
  • Name - full name
  • Birthday – Date of birth
  • Email – Email
  • Position - Position
  • Department - Department
Very often the word ID is used to name an identifier field.

Now let's create our table:

CREATE TABLE Employees(ID int, Name nvarchar(30), Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30))
To specify required columns, you can use the NOT NULL option.

For an existing table, fields can be redefined using the following commands:

Update ID field ALTER TABLE Employees ALTER COLUMN ID int NOT NULL -- update Name field ALTER TABLE Employees ALTER COLUMN Name nvarchar(30) NOT NULL

Note
The general concept of the SQL language remains the same for most DBMSs (at least, this is what I can judge from the DBMSs that I have worked with). The differences between DDL in different DBMSs mainly lie in the data types (not only their names may differ here, but also the details of their implementation), and the very specifics of the implementation of the SQL language may also differ slightly (i.e., the essence of the commands is the same, but there may be slight differences in dialect, alas, but there is no one standard). Having mastered the basics of SQL, you can easily switch from one DBMS to another, because... In this case, you will only need to understand the details of the implementation of commands in the new DBMS, i.e. in most cases, simply drawing an analogy will suffice.

Creating a table CREATE TABLE Employees(ID int, -- in ORACLE the int type is the equivalent (wrapper) for number(38) Name nvarchar2(30), -- nvarchar2 in ORACLE is equivalent to nvarchar in MS SQL Birthday date, Email nvarchar2(30) , Position nvarchar2(30), Department nvarchar2(30)); -- updating the ID and Name fields (here MODIFY(...) is used instead of ALTER COLUMN) ALTER TABLE Employees MODIFY(ID int NOT NULL,Name nvarchar2(30) NOT NULL); -- adding PK (in this case the construction looks like in MS SQL, it will be shown below) ALTER TABLE Employees ADD CONSTRAINT PK_Employees PRIMARY KEY(ID);
For ORACLE there are differences in terms of implementation of the varchar2 type; its encoding depends on the database settings and the text can be saved, for example, in UTF-8 encoding. In addition, the field length in ORACLE can be specified both in bytes and in characters; for this, additional options BYTE and CHAR are used, which are specified after the field length, for example:

NAME varchar2(30 BYTE) -- the field capacity will be 30 bytes NAME varchar2(30 CHAR) -- the field capacity will be 30 characters
Which option will be used by default BYTE or CHAR, in the case of simply specifying the varchar2(30) type in ORACLE, depends on the database settings, and it can sometimes be set in the IDE settings. In general, sometimes you can easily get confused, so in the case of ORACLE, if the varchar2 type is used (and this is sometimes justified here, for example, when using UTF-8 encoding), I prefer to explicitly write CHAR (since it is usually more convenient to calculate the length of the string in characters ).

But in this case, if there is already some data in the table, then for successful execution of commands it is necessary that the ID and Name fields in all rows of the table be filled in. Let's demonstrate this with an example, insert data into the table in the ID, Position and Department fields, this can be done with the following script:

INSERT Employees(ID,Position,Department) VALUES (1000,N"Director",N"Administration"), (1001,N"Programmer",N"IT"), (1002,N"Accountant",N"Accounting" ), (1003,N"Senior Programmer",N"IT")
In this case, the INSERT command will also generate an error, because When inserting, we did not specify the value of the required Name field.
If we already had this data in the original table, then the command “ALTER TABLE Employees ALTER COLUMN ID int NOT NULL” would be executed successfully, and the command “ALTER TABLE Employees ALTER COLUMN Name int NOT NULL” would produce an error message, that the Name field contains NULL (unspecified) values.

Let's add values ​​for the Name field and fill in the data again:


The NOT NULL option can also be used directly when creating a new table, i.e. in the context of the CREATE TABLE command.

First, delete the table using the command:

DROP TABLE Employees
Now let’s create a table with the required ID and Name columns:

CREATE TABLE Employees(ID int NOT NULL, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30))
You can also write NULL after the column name, which will mean that NULL values ​​(not specified) will be allowed in it, but this is not necessary, since this characteristic is implied by default.

If, on the contrary, you want to make an existing column optional, then use the following command syntax:

ALTER TABLE Employees ALTER COLUMN Name nvarchar(30) NULL
Or simply:

ALTER TABLE Employees ALTER COLUMN Name nvarchar(30)
With this command we can also change the field type to another compatible type, or change its length. For example, let's expand the Name field to 50 characters:

ALTER TABLE Employees ALTER COLUMN Name nvarchar(50)

Primary key

When creating a table, it is desirable that it have a unique column or a set of columns that is unique for each of its rows - a record can be uniquely identified by this unique value. This value is called the table's primary key. For our Employees table, such a unique value could be the ID column (which contains the “Employee Personnel Number” - even though in our case this value is unique for each employee and cannot be repeated).

You can create a primary key to an existing table using the command:

ALTER TABLE Employees ADD CONSTRAINT PK_Employees PRIMARY KEY(ID)
Where "PK_Employees" is the name of the constraint responsible for the primary key. Typically, the primary key is named with the prefix “PK_” followed by the table name.

If the primary key consists of several fields, then these fields must be listed in parentheses, separated by commas:

ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY(field1,field2,…)
It is worth noting that in MS SQL, all fields that are included in the primary key must have the NOT NULL characteristic.

The primary key can also be determined directly when creating a table, i.e. in the context of the CREATE TABLE command. Let's delete the table:

DROP TABLE Employees
And then we'll create it using the following syntax:

CREATE TABLE Employees(ID int NOT NULL, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30), CONSTRAINT PK_Employees PRIMARY KEY(ID) -- describe the PK after all fields as a limitation)
After creation, fill the table with data:

INSERT Employees(ID,Position,Department,Name) VALUES (1000,N"Director",N"Administration",N"Ivanov I.I."), (1001,N"Programmer",N"IT",N" Petrov P.P."), (1002,N"Accountant",N"Accounting",N"Sidorov S.S."), (1003,N"Senior Programmer",N"IT",N"Andreev A. A.")
If the primary key in a table consists of only the values ​​of one column, then you can use the following syntax:

CREATE TABLE Employees(ID int NOT NULL CONSTRAINT PK_Employees PRIMARY KEY, -- specify as a characteristic of the field Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30))
In fact, you don’t have to specify the name of the constraint, in which case it will be assigned a system name (like “PK__Employee__3214EC278DA42077”):

CREATE TABLE Employees(ID int NOT NULL, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30), PRIMARY KEY(ID))
Or:

CREATE TABLE Employees(ID int NOT NULL PRIMARY KEY, Name nvarchar(30) NOT NULL, Birthday date, Email nvarchar(30), Position nvarchar(30), Department nvarchar(30))
But I would recommend that for permanent tables you always explicitly specify the name of the constraint, because With an explicitly specified and understandable name, it will be easier to manipulate it later; for example, you can delete it:

ALTER TABLE Employees DROP CONSTRAINT PK_Employees
But such a short syntax, without specifying the names of the restrictions, is convenient to use when creating temporary database tables (the name of the temporary table begins with # or ##), which will be deleted after use.

Let's summarize

So far we have looked at the following commands:
  • CREATE TABLE table_name (listing of fields and their types, restrictions) – used to create a new table in the current database;
  • DROP TABLE table_name – used to delete a table from the current database;
  • ALTER TABLE table_name ALTER COLUMN column_name... – used to update the column type or change its settings (for example, to set the NULL or NOT NULL characteristic);
  • ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY(field1, field2,...) – adding a primary key to an existing table;
  • ALTER TABLE table_name DROP CONSTRAINT constraint_name – removes a constraint from the table.

A little about temporary tables

Extract from MSDN. There are two types of temporary tables in MS SQL Server: local (#) and global (##). Local temporary tables are visible only to their creators until the connection session to the SQL Server instance ends when they are first created. Local temporary tables are automatically deleted after a user disconnects from the instance of SQL Server. Global temporary tables are visible to all users during any connection sessions after those tables are created, and are deleted when all users referencing those tables disconnect from the instance of SQL Server.

Temporary tables are created in the tempdb system database, i.e. By creating them we do not clog the main database; otherwise, temporary tables are completely identical to regular tables; they can also be deleted using the DROP TABLE command. Local (#) temporary tables are more commonly used.

To create a temporary table, you can use the CREATE TABLE command:

CREATE TABLE #Temp(ID int, Name nvarchar(30))
Since a temporary table in MS SQL is similar to a regular table, it can also be deleted using the DROP TABLE command:

DROP TABLE #Temp

You can also create a temporary table (like a regular table) and immediately fill it with the data returned by the query using the SELECT ... INTO syntax:

SELECT ID,Name INTO #Temp FROM Employees

Note
The implementation of temporary tables may differ in different DBMSs. For example, in the ORACLE and Firebird DBMS, the structure of temporary tables must be determined in advance by the CREATE GLOBAL TEMPORARY TABLE command, indicating the specifics of storing data in it, then the user sees it among the main tables and works with it as with a regular table.

Database normalization – splitting into subtables (directories) and identifying connections

Our current Employees table has the disadvantage that in the Position and Department fields the user can enter any text, which is primarily fraught with errors, since for one employee he can simply indicate “IT” as the department, and for a second employee, for example , enter “IT department”, the third has “IT”. As a result, it will be unclear what the user meant, i.e. Are these employees employees of the same department, or did the user describe himself and these are 3 different departments? Moreover, in this case, we will not be able to correctly group the data for some report, where it may be necessary to show the number of employees by each department.

The second disadvantage is the volume of storage of this information and its duplication, i.e. For each employee, the full name of the department is indicated, which requires space in the database to store each character from the department name.

The third drawback is the difficulty of updating these fields if the name of a position changes, for example, if you need to rename the position “Programmer” to “Junior Programmer”. In this case, we will have to make changes to each row of the table whose Position is equal to “Programmer”.

To avoid these shortcomings, so-called database normalization is used - splitting it into subtables and reference tables. It is not necessary to go into the jungle of theory and study what they are normal forms, it is enough to understand the essence of normalization.

Let's create 2 reference tables “Positions” and “Departments”, let's call the first Positions, and the second, respectively, Departments:

CREATE TABLE Positions(ID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_Positions PRIMARY KEY, Name nvarchar(30) NOT NULL) CREATE TABLE Departments(ID int IDENTITY(1,1) NOT NULL CONSTRAINT PK_Departments PRIMARY KEY, Name nvarchar(30 ) NOT NULL)
Note that here we used the new IDENTITY option, which says that the data in the ID column will be numbered automatically, starting from 1, in increments of 1, i.e. When adding new records, they will be sequentially assigned the values ​​1, 2, 3, etc. Such fields are usually called auto-incrementing. A table can only have one field defined with the IDENTITY property, and usually, but not necessarily, that field is the primary key for that table.

Note
In different DBMSs, the implementation of fields with a counter can be done differently. In MySQL, for example, such a field is defined using the AUTO_INCREMENT option. In ORACLE and Firebird, this functionality could previously be emulated using SEQUENCE. But as far as I know, ORACLE has now added the GENERATED AS IDENTITY option.

Let's fill these tables automatically, based on the current data recorded in the Position and Department fields of the Employees table:

We fill the Name field of the Positions table with unique values ​​from the Position field of the Employees table INSERT Positions(Name) SELECT DISTINCT Position FROM Employees WHERE Position IS NOT NULL -- discard records for which the position is not specified
Let's do the same for the Departments table:

INSERT Departments(Name) SELECT DISTINCT Department FROM Employees WHERE Department IS NOT NULL
If we now open the Positions and Departments tables, we will see a numbered set of values ​​for the ID field:

SELECT * FROM Positions

SELECT * FROM Departments

These tables will now play the role of reference books for specifying positions and departments. We will now refer to job and department IDs. First of all, let's create new fields in the Employees table to store identifier data:

Add a field for position ID ALTER TABLE Employees ADD PositionID int -- add a field for department ID ALTER TABLE Employees ADD DepartmentID int
The type of reference fields must be the same as in directories, in this case it is int.

You can also add several fields to the table at once with one command, listing the fields separated by commas:

ALTER TABLE Employees ADD PositionID int, DepartmentID int
Now let's write links (reference restrictions - FOREIGN KEY) for these fields so that the user does not have the opportunity to write into these fields values ​​that are not among the ID values ​​​​found in the directories.

ALTER TABLE Employees ADD CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID)
And we'll do the same for the second field:

ALTER TABLE Employees ADD CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID)
Now the user will be able to enter only ID values ​​from the corresponding directory in these fields. Accordingly, in order to use a new department or position, he will first have to add a new entry to the corresponding directory. Because Positions and departments are now stored in directories in one single copy, so to change the name, it is enough to change it only in the directory.

The name of a reference constraint is usually a composite name, consisting of the prefix "FK_", followed by the table name, and followed by an underscore, followed by the name of the field that refers to the reference table identifier.

An identifier (ID) is usually an internal value that is used only for relationships and what value is stored there is completely indifferent in most cases, so there is no need to try to get rid of holes in the sequence of numbers that arise while working with the table, for example, after deleting records from the directory.

ALTER TABLE table ADD CONSTRAINT constraint_name FOREIGN KEY(field1,field2,…) REFERENCES reference_table(field1,field2,…)
In this case, in the “reference_table” table, the primary key is represented by a combination of several fields (field1, field2,...).

Actually, now let’s update the PositionID and DepartmentID fields with ID values ​​from the directories. Let's use the DML UPDATE command for this purpose:

UPDATE e SET PositionID=(SELECT ID FROM Positions WHERE Name=e.Position), DepartmentID=(SELECT ID FROM Departments WHERE Name=e.Department) FROM Employees e
Let's see what happens by running the request:

SELECT * FROM Employees

That’s it, the PositionID and DepartmentID fields are filled with the identifiers corresponding to positions and departments; the Position and Department fields are no longer needed in the Employees table, you can delete these fields:

ALTER TABLE Employees DROP COLUMN Position,Department
Now our table looks like this:

SELECT * FROM Employees

ID Name Birthday Email PositionID DepartmentID
1000 Ivanov I.I. NULL NULL 2 1
1001 Petrov P.P. NULL NULL 3 3
1002 Sidorov S.S. NULL NULL 1 2
1003 Andreev A.A. NULL NULL 4 3

Those. We eventually got rid of storing redundant information. Now, based on the job and department numbers, we can unambiguously determine their names using the values ​​in the reference tables:

SELECT e.ID,e.Name,p.Name PositionName,d.Name DepartmentName FROM Employees e LEFT JOIN Departments d ON d.ID=e.DepartmentID LEFT JOIN Positions p ON p.ID=e.PositionID

In the object inspector we can see all the objects created for a given table. From here you can perform various manipulations with these objects - for example, rename or delete objects.

It is also worth noting that the table can refer to itself, i.e. you can create a recursive link. For example, let’s add another field ManagerID to our table with employees, which will indicate the employee to whom he reports this employee. Let's create a field:

ALTER TABLE Employees ADD ManagerID int
This field allows a NULL value; the field will be empty if, for example, there are no superiors over the employee.

Now let's create a FOREIGN KEY for the Employees table:

ALTER TABLE Employees ADD CONSTRAINT FK_Employees_ManagerID FOREIGN KEY (ManagerID) REFERENCES Employees(ID)
Let's now create a diagram and see how the relationships between our tables look on it:

As a result, we should see the following picture (the Employees table is connected to the Positions and Depertments tables, and also refers to itself):

Finally, it is worth saying that reference keys can include additional options ON DELETE CASCADE and ON UPDATE CASCADE, which indicate how to behave when deleting or updating a record that is referenced in the reference table. If these options are not specified, then we cannot change the ID in the directory table for a record that is referenced from another table, and we will also not be able to delete such a record from the directory until we delete all rows referencing this record or, Let’s update the references in these lines to a different value.

For example, let's recreate the table specifying the ON DELETE CASCADE option for FK_Employees_DepartmentID:

DROP TABLE Employees CREATE TABLE Employees(ID int NOT NULL, Name nvarchar(30), Birthday date, Email nvarchar(30), PositionID int, DepartmentID int, ManagerID int, CONSTRAINT PK_Employees PRIMARY KEY (ID), CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID ) REFERENCES Departments(ID) ON DELETE CASCADE, CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID), CONSTRAINT FK_Employees_ManagerID FOREIGN KEY (ManagerID) REFERENCES Employees(ID)) INSERT Employees (ID,Name,Birthday,PositionID,DepartmentID,Man agerID )VALUES (1000,N"Ivanov I.I.","19550219",2,1,NULL), (1001,N"Petrov P.P.","19831203",3,3,1003), (1002 ,N"Sidorov S.S.","19760607",1,2,1000), (1003,N"Andreev A.A.","19820417",4,3,1000)
Let's delete the department with ID 3 from the Departments table:

DELETE Departments WHERE ID=3
Let's look at the data in the Employees table:

SELECT * FROM Employees

ID Name Birthday Email PositionID DepartmentID ManagerID
1000 Ivanov I.I. 1955-02-19 NULL 2 1 NULL
1002 Sidorov S.S. 1976-06-07 NULL 1 2 1000

As you can see, the data for department 3 from the Employees table was also deleted.

The ON UPDATE CASCADE option behaves similarly, but it is effective when updating the ID value in the directory. For example, if we change the ID of a position in the position directory, then in this case the DepartmentID in the Employees table will be updated to the new ID value that we set in the directory. But in this case it simply won’t be possible to demonstrate this, because the ID column in the Departments table has the IDENTITY option, which will not allow us to execute the following query (change department ID 3 to 30):

UPDATE Departments SET ID=30 WHERE ID=3
The main thing is to understand the essence of these 2 options ON DELETE CASCADE and ON UPDATE CASCADE. I use these options very rarely and recommend that you think carefully before specifying them in a reference constraint, because if you accidentally delete an entry from a directory table, this can lead to big problems and create a chain reaction.

Let's restore department 3:

We give permission to add/change IDENTITY value SET IDENTITY_INSERT Departments ON INSERT Departments(ID,Name) VALUES(3,N"IT") -- we prohibit adding/change IDENTITY value SET IDENTITY_INSERT Departments OFF
Let's completely clear the Employees table using the TRUNCATE TABLE command:

TRUNCATE TABLE Employees
And again we will reload the data into it using the previous INSERT command:

INSERT Employees (ID,Name,Birthday,PositionID,DepartmentID,ManagerID)VALUES (1000,N"Ivanov I.I.","19550219",2,1,NULL), (1001,N"Petrov P.P." ,"19831203",3,3,1003), (1002,N"Sidorov S.S.","19760607",1,2,1000), (1003,N"Andreev A.A.","19820417" ,4,3,1000)

Let's summarize

At the moment, several more DDL commands have been added to our knowledge:
  • Adding the IDENTITY property to a field – allows you to make this field an automatically populated field (counter field) for the table;
  • ALTER TABLE table_name ADD list_of_fields_with_characteristics – allows you to add new fields to the table;
  • ALTER TABLE table_name DROP COLUMN list_fields – allows you to remove fields from the table;
  • ALTER TABLE table_name ADD CONSTRAINT constraint_name FOREIGN KEY(fields) REFERENCES table_reference (fields) – allows you to define the relationship between the table and the reference table.

Other restrictions – UNIQUE, DEFAULT, CHECK

Using a UNIQUE constraint, you can say that the value for each row in a given field or set of fields must be unique. In the case of the Employees table, we can impose such a constraint on the Email field. Just pre-fill Email with values ​​if they are not already defined:

UPDATE Employees SET Email=" [email protected]" WHERE ID=1000 UPDATE Employees SET Email=" [email protected]" WHERE ID=1001 UPDATE Employees SET Email=" [email protected]" WHERE ID=1002 UPDATE Employees SET Email=" [email protected]"WHERE ID=1003
Now you can impose a uniqueness constraint on this field:

ALTER TABLE Employees ADD CONSTRAINT UQ_Employees_Email UNIQUE(Email)
Now the user will not be able to enter the same E-Mail for several employees.

A unique constraint is usually named as follows - first comes the prefix “UQ_”, then the name of the table and after the underscore comes the name of the field on which this constraint is applied.

Accordingly, if a combination of fields must be unique in the context of table rows, then we list them separated by commas:

ALTER TABLE table_name ADD CONSTRAINT constraint_name UNIQUE(field1,field2,…)
By adding a DEFAULT constraint to a field, we can specify a default value that will be substituted if, when inserting a new record, this field is not listed in the list of fields of the INSERT command. This restriction can be set directly when creating the table.

Let's add a new field “Hire Date” to the Employees table and call it HireDate and say that the default value for this field will be current date:

ALTER TABLE Employees ADD HireDate date NOT NULL DEFAULT SYSDATETIME()
Or if the HireDate column already exists, then the following syntax can be used:

ALTER TABLE Employees ADD DEFAULT SYSDATETIME() FOR HireDate
Here I did not specify the name of the constraint, because... in the case of DEFAULT, I have the opinion that this is not so critical. But if you do it in a good way, then I think you don’t need to be lazy and you should set a normal name. This is done as follows:

ALTER TABLE Employees ADD CONSTRAINT DF_Employees_HireDate DEFAULT SYSDATETIME() FOR HireDate
That's how of this column did not exist before, then when you add it to each record, the current date value will be inserted into the HireDate field.

When adding a new entry, the current date will also be inserted automatically, of course, unless we explicitly set it, i.e. We will not indicate it in the list of columns. Let's show this with an example without specifying the HireDate field in the list of added values:

INSERT Employees(ID,Name,Email)VALUES(1004,N"Sergeev S.S."," [email protected]")
Let's see what happened:

SELECT * FROM Employees

ID Name Birthday Email PositionID DepartmentID ManagerID HireDate
1000 Ivanov I.I. 1955-02-19 [email protected] 2 1 NULL 2015-04-08
1001 Petrov P.P. 1983-12-03 [email protected] 3 4 1003 2015-04-08
1002 Sidorov S.S. 1976-06-07 [email protected] 1 2 1000 2015-04-08
1003 Andreev A.A. 1982-04-17 [email protected] 4 3 1000 2015-04-08
1004 Sergeev S.S. NULL [email protected] NULL NULL NULL 2015-04-08

The CHECK check constraint is used when it is necessary to check the values ​​inserted into the field. For example, let's impose this restriction on the personnel number field, which for us is an employee identifier (ID). Using this constraint, we say that personnel numbers must have a value from 1000 to 1999:

ALTER TABLE Employees ADD CONSTRAINT CK_Employees_ID CHECK(ID BETWEEN 1000 AND 1999)
The constraint is usually named the same way, first with the prefix “CK_”, then the name of the table and the name of the field on which this constraint is imposed.

Let's try to insert an invalid record to check that the constraint works (we should get the corresponding error):

INSERT Employees(ID,Email) VALUES(2000," [email protected]")
Now let’s change the inserted value to 1500 and make sure that the record is inserted:

INSERT Employees(ID,Email) VALUES(1500," [email protected]")
You can also create UNIQUE and CHECK constraints without specifying a name:

ALTER TABLE Employees ADD UNIQUE(Email) ALTER TABLE Employees ADD CHECK(ID BETWEEN 1000 AND 1999)
But this is not a very good practice and it is better to specify the name of the constraint explicitly, because To figure it out later, which will be more difficult, you will need to open the object and look at what it is responsible for.

With a good name, a lot of information about the constraint can be learned directly from its name.

And, accordingly, all these restrictions can be created immediately when creating a table, if it does not exist yet. Let's delete the table:

DROP TABLE Employees
And we will recreate it with all the created restrictions with one CREATE TABLE command:

CREATE TABLE Employees(ID int NOT NULL, Name nvarchar(30), Birthday date, Email nvarchar(30), PositionID int, DepartmentID int, HireDate date NOT NULL DEFAULT SYSDATETIME(), -- for DEFAULT I will make an exception CONSTRAINT PK_Employees PRIMARY KEY (ID), CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID), CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID), CONSTRAINT UQ_Employees_Email UNIQUE (Email), CONSTRAINT CK_Employees_ID CHECK (ID BETWE EN 1000 AND 1999))

INSERT Employees (ID,Name,Birthday,Email,PositionID,DepartmentID)VALUES (1000,N"Ivanov I.I.","19550219"," [email protected]",2,1), (1001,N"Petrov P.P.","19831203"," [email protected]",3,3), (1002,N"Sidorov S.S.","19760607"," [email protected]",1,2), (1003,N"Andreev A.A.","19820417"," [email protected]",4,3)

A little about the indexes created when creating PRIMARY KEY and UNIQUE constraints

As you can see in the screenshot above, when creating the PRIMARY KEY and UNIQUE constraints, indexes with the same names (PK_Employees and UQ_Employees_Email) were automatically created. By default, the index for the primary key is created as CLUSTERED, and for all other indexes as NONCLUSTERED. It is worth saying that the concept of a cluster index is not available in all DBMSs. A table can only have one CLUSTERED index. CLUSTERED – means that the table records will be sorted by this index, we can also say that this index has direct access to all data in the table. This is the main index of the table, so to speak. To put it even more roughly, this is an index attached to a table. A clustered index is a very powerful tool that can help with query optimization, but let's just remember this for now. If we want to tell the clustered index to be used not on the primary key, but on another index, then when creating the primary key we must specify the NONCLUSTERED option:

ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY NONCLUSTERED(field1,field2,…)
For example, let's make the constraint index PK_Employees non-clustered, and the constraint index UQ_Employees_Email clustered. First of all, let's remove these restrictions:

ALTER TABLE Employees DROP CONSTRAINT PK_Employees ALTER TABLE Employees DROP CONSTRAINT UQ_Employees_Email
Now let's create them with the CLUSTERED and NONCLUSTERED options:

ALTER TABLE Employees ADD CONSTRAINT PK_Employees PRIMARY KEY NONCLUSTERED (ID) ALTER TABLE Employees ADD CONSTRAINT UQ_Employees_Email UNIQUE CLUSTERED (Email)
Now, by selecting from the Employees table, we will see that the records are sorted by the UQ_Employees_Email clustered index:

SELECT * FROM Employees

ID Name Birthday Email PositionID DepartmentID HireDate
1003 Andreev A.A. 1982-04-17 [email protected] 4 3 2015-04-08
1000 Ivanov I.I. 1955-02-19 [email protected] 2 1 2015-04-08
1001 Petrov P.P. 1983-12-03 [email protected] 3 3 2015-04-08
1002 Sidorov S.S. 1976-06-07 [email protected] 1 2 2015-04-08

Previously, when the clustered index was the PK_Employees index, records were sorted by the ID field by default.

But in this case, this is just an example that shows the essence of a clustered index, because Most likely, queries will be made to the Employees table using the ID field and in some cases, perhaps, it itself will act as a directory.

For directories, it is usually advisable for the clustered index to be built on the primary key, because in requests we often refer to the directory identifier to obtain, for example, the name (Position, Department). Let us remember here what I wrote above, that a clustered index has direct access to table rows, and it follows that we can get the value of any column without additional overhead.

It is advantageous to apply a cluster index to fields that are sampled most frequently.

Sometimes tables are created with a key based on a surrogate field; in this case, it can be useful to save the CLUSTERED index option for a more suitable index and specify the NONCLUSTERED option when creating a surrogate primary key.

Let's summarize

On at this stage we got acquainted with all types of restrictions, in their simplest form, which are created by a command like “ALTER TABLE table_name ADD CONSTRAINT constraint_name …”:
  • PRIMARY KEY– primary key;
  • FOREIGN KEY– setting up connections and monitoring referential integrity of data;
  • UNIQUE– allows you to create uniqueness;
  • CHECK– allows you to ensure the correctness of the entered data;
  • DEFAULT– allows you to set a default value;
  • It is also worth noting that all restrictions can be removed using the command “ ALTER TABLE table_name DROP CONSTRAINT constraint_name".
We also partially touched on the topic of indexes and examined the concept of cluster ( CLUSTERED) and non-clustered ( NONCLUSTERED) index.

Creating standalone indexes

By independent here we mean indexes that are not created under the PRIMARY KEY or UNIQUE constraint.

Indexes on a field or fields can be created with the following command:

CREATE INDEX IDX_Employees_Name ON Employees(Name)
Also here you can specify the options CLUSTERED, NONCLUSTERED, UNIQUE, and you can also specify the sorting direction of each individual field ASC (default) or DESC:

CREATE UNIQUE NONCLUSTERED INDEX UQ_Employees_EmailDesc ON Employees(Email DESC)
When creating a non-clustered index, the NONCLUSTERED option can be omitted, because it is implied by default and is shown here simply to indicate the position of the CLUSTERED or NONCLUSTERED option in the command.

You can delete the index with the following command:

DROP INDEX IDX_Employees_Name ON Employees
Simple indexes, as well as constraints, can be created in the context of the CREATE TABLE command.

For example, let's delete the table again:

DROP TABLE Employees
And we will recreate it with all the created restrictions and indexes with one CREATE TABLE command:

CREATE TABLE Employees(ID int NOT NULL, Name nvarchar(30), Birthday date, Email nvarchar(30), PositionID int, DepartmentID int, HireDate date NOT NULL CONSTRAINT DF_Employees_HireDate DEFAULT SYSDATETIME(), ManagerID int, CONSTRAINT PK_Employees PRIMARY KEY (ID ), CONSTRAINT FK_Employees_DepartmentID FOREIGN KEY(DepartmentID) REFERENCES Departments(ID), CONSTRAINT FK_Employees_PositionID FOREIGN KEY(PositionID) REFERENCES Positions(ID), CONSTRAINT FK_Employees_ManagerID FOREIGN KEY (ManagerID) REFERENCES Employees(ID), CONSTRAINT UQ_Emplo yees_Email UNIQUE(Email), CONSTRAINT CK_Employees_ID CHECK(ID BETWEEN 1000 AND 1999), INDEX IDX_Employees_Name(Name))
Finally, let’s insert our employees into the table:

INSERT Employees (ID,Name,Birthday,Email,PositionID,DepartmentID,ManagerID)VALUES (1000,N"Ivanov I.I.","19550219"," [email protected]",2,1,NULL), (1001,N"Petrov P.P.","19831203"," [email protected]",3,3,1003), (1002,N"Sidorov S.S.","19760607"," [email protected]",1,2,1000), (1003,N"Andreev A.A.","19820417"," [email protected]",4,3,1000)
Additionally, it is worth noting that you can include values ​​in a non-clustered index by specifying them in INCLUDE. Those. in this case, the INCLUDE index will be somewhat reminiscent of a clustered index, only now the index is not attached to the table, but the necessary values ​​are attached to the index. Accordingly, such indexes can greatly improve the performance of selection queries (SELECT); if all the listed fields are in the index, then access to the table may not be needed at all. But this naturally increases the size of the index, because... the values ​​of the listed fields are duplicated in the index.

Extract from MSDN. General command syntax for creating indexes

CREATE [UNIQUE] [CLUSTERED | NONCLUSTERED ] INDEX index_name ON (column [ ASC | DESC ] [ ,...n ]) [ INCLUDE (column_name [ ,...n ]) ]

Let's summarize

Indexes can increase the speed of data retrieval (SELECT), but indexes reduce the speed of table data modification, because After each modification, the system will need to rebuild all indexes for a specific table.

It is advisable to find the optimal solution in each case, golden mean so that both the sampling and data modification performance is at the proper level. The strategy for creating indexes and the number of indexes can depend on many factors, such as how often the data in the table changes.

Conclusion on DDL

As you can see, DDL is not as complicated as it might seem at first glance. Here I was able to show almost all of its main structures using just three tables.

The main thing is to understand the essence, and the rest is a matter of practice.

Good luck in mastering this wonderful language called SQL.

Today, SQL courses “for dummies” are becoming increasingly popular. This can be explained very simply, because in the modern world you can increasingly find so-called “dynamic” web services. They are distinguished by a fairly flexible shell and are based on All novice programmers who decide to dedicate websites, first of all enroll in SQL courses “for dummies”.

Why learn this language?

First of all, SQL is taught in order to further create a wide variety of applications for one of the most popular blog engines today - WordPress. After completing a few simple lessons, you will be able to create queries of any complexity, which only confirms the simplicity of this language.

What is SQL?

Or a structured query language, was created with one single purpose: to determine, provide access to and process them in fairly short periods of time. If you know the SQL meaning, then you will understand that this server is classified as a so-called “non-procedural” language. That is, its capabilities only include a description of any components or results that you want to see in the future on the site. But when does not indicate exactly what results are going to be obtained. Every new request in this language it is like an additional “superstructure”. It is in the order in which they are entered into the database that the queries will be executed.

What procedures can be performed using this language?

Despite its simplicity, the SQL database allows you to create a wide variety of queries. So what can you do if you learn this important programming language?

  • create a wide variety of tables;
  • receive, store and modify received data;
  • change table structures at your discretion;
  • combine the received information into single blocks;
  • calculate the received data;
  • ensure complete protection of information.

What commands are the most popular in this language?

If you decide to take SQL for Dummies courses, then you will receive detailed information about the commands that are used to create queries using it. The most common today are:

  1. DDL is a command that defines data. It is used to create, modify and delete a wide variety of objects in the database.
  2. DCL is a command that manipulates data. It is used to provide different users with access to information in the database, as well as to use tables or views.
  3. TCL is a team that manages a variety of transactions. Its main purpose is to determine the progress of a transaction.
  4. DML - manipulates the received data. Its purpose is to allow the user to move various information from the database or enter it there.

Types of privileges that exist in this server

Privileges refer to those actions that a particular user can perform in accordance with his status. The most minimal, of course, is a regular login. Of course, privileges may change over time. Old ones will be deleted and new ones will be added. Today, all those who take SQL Server "for dummies" courses know that there are several types of permitted actions:

  1. Object type - the user is allowed to execute any command only in relation to a specific object that is located in the database. At the same time, privileges differ for different objects. They are also tied not only to a particular user, but also to tables. If someone, using his capabilities, created a table, then he is considered its owner. Therefore, he has the right to assign new privileges to other users related to the information in it.
  2. The system type is the so-called data copyright. Users who have received such privileges can create various objects in the database.

History of SQL

This language was created by IBM Research Laboratory in 1970. At that time, its name was slightly different (SEQUEL), but after a few years of use it was changed, shortening it a little. Despite this, even today many world-famous programming experts still pronounce the name the old-fashioned way. SQL was created with one single purpose - to invent a language that would be so simple that it could be used without special problems even ordinary Internet users can learn. An interesting fact is that at that time SQL was not the only such language. In California, another group of specialists developed a similar Ingres, but it never became widespread. Before 1980, there were several variations of SQL that were only slightly different from each other. To prevent confusion, a standard version was created in 1983, which is still popular today. SQL courses "for dummies" allow you to learn much more about the service and fully study it in a few weeks.

A selection of books, video courses and online resources for studying databases, the fundamentals of relational theory and the SQL language.

Books

Alan Bewley "Learning SQL" (2007)

This book is an excellent choice for those who are at the beginning of the thorny path of learning SQL. It will not only allow you to acquire the necessary base of initial knowledge, but will also tell you about the most popular subtleties and powerful language tools used by experienced programmers.
Many textbooks on databases, relational theory, and SQL are filled with boring theoretical concepts. This book is a pleasant exception due to its light, lively style. The author skillfully presents the reader with information about SQL expressions and blocks, condition types, joins, subqueries and much more.
To consolidate the acquired knowledge in practice, the author creates a MySQL training base and provides many practical examples of queries covering all the theoretical material presented.

Chris Fiaily "SQL" (2013)


The book deals with the ANSI SQL-92 (SQL2) language version. It describes in detail how to use the query language to solve the corresponding classes of problems of retrieving and modifying data and working with objects of the database structure. All examples are explained in detail.
Particular attention in this publication is paid to the differences in SQL dialects in the implementation of the most common DBMSs: MySQL, Oracle, MS SQL Server and PostgreSQL.
The book is intended for anyone who wants to independently learn the SQL language or improve their knowledge on this topic.

Anthony Molinaro "SQL. Collection of recipes" (2009)


This publication is intended for those who already have some knowledge of SQL and want to improve their skills in this area. It will also be very useful for database experts, since the author offers examples of solving problems in different DBMSs: DB2, Oracle, PostgreSQL, MySQL and SQL Server.
The book will help you learn how to use SQL to solve a wider range of problems: from operations within a database to retrieving data and transmitting it over the network to applications.
You'll learn how to use window functions and special operators, as well as advanced techniques for working with data warehouses: creating histograms, summarizing data into blocks, performing sliding range aggregations, generating running sums and subtotals. You'll be able to expand rows into columns and vice versa, simplify calculations within a row and double-unfold the result set, and perform string traversal, which allows you to use SQL to parse a string into characters, words, or delimited string elements. The techniques proposed by the author will allow you to optimize the code of your applications and open up new possibilities for you in the SQL language.

Alex Kriegel et al. “SQL. User's Bible, 2nd edition (2010)


The book is unique in that each chapter compares the implementations of certain queries in the dialects of the three leading DBMSs. This makes it a comprehensive and practical guide to the SQL language for developers from beginners to gurus, a sort of desktop guide.
The publication covers topics from the very basics to transactions and locks, functions and database security.
There are several additional topics presented at the end: SQL to XML integration, OLAP business intelligence, and more.

Eric Redmond, Jim R. Wilson "Seven Databases in Seven Weeks." Introduction to modern databases and NoSQL ideology" (2015)

The book describes most of the modern bases data with open source code: Redis, Neo4J, CouchDB, MongoDB, HBase, PostgreSQL and Riak. For each base, examples of working with real data are provided, demonstrating the main ideas and strengths.
This book will shed light on the strengths and weaknesses of each of the seven databases and teach you how to choose the one that best meets your needs.

Martin Graber “SQL for mere mortals” Laurie, 2014, 382 pages (11.2 MB pdf)

The book can be described as a guide for beginners. Structured Query Language - SQL, a programming language for creating and managing relational databases (an applied, logical model for constructing a set of databases). The book is designed for the simplest (lowest) level of training in the IT field, that is, enough knowledge to cover the school curriculum. But this does not mean that the manual material is only an introduction to this programming language - no, SQL is described quite deeply (author's statement).

Each chapter adds new data describing interrelated concepts and definitions. All subsequent material is based on the previous one, discussed earlier, with a discussion of practical issues at the end of the chapter for better assimilation of the knowledge gained. You will find the answers in Appendix A.

An introduction to SQL is presented in the first seven chapters, which are a must-read if you're using a guide like SQL for Beginners. The next seven chapters (8 to 14) cover more complex examples: combined queries, queries to several tables at once. Other features of SQL: creating and editing tables, entering and setting values, opening and closing access to created tables - are outlined in chapters 15 to 23. Finally, about the structure of databases and the possibility of using SQL in programs developed in other languages. The appendices provide guidance on SQL commands and answers to assignments. The book is ideal for beginners to learn SQL.
ISBN: 978-5-85582-301-1

Chapter 1. Introduction to Relational Databases 1
What is a relational database? 3
Database Example 5
Results 7

Chapter 2. Introduction to SQL 9
How does SQL work? 10
Various data types 12
Results 15

Chapter 3. Using SQL to retrieve data from tables 17
Formation of request 18
Defining a sample - WHERE clause 24
Results 26

Chapter 4. Using relational and Boolean operators to create more complex predicates 29
Relational operators 30
Boolean operators 32
Results 37

Chapter 5. Usage special operators in "conditions" 39
Operator IN 40
Operator BETWEEN 41
Operator LIKE 44
IS NULL operator 47
Results 49

Chapter 6. Summarizing data using the aggregation function 51
What are aggregation functions? 52
Results 61

Chapter 7. Formatting Query Results 63
Strings and Expressions 64
Ordering output fields 67
Results 71

Chapter 8. Using multiple tables in one query 75
Joining tables 76
Results 81

Chapter 9 A join operation whose operands are represented by a single table 83
How to join two copies of the same table 84
Results 90

Chapter 10. Nesting queries 93
How are subqueries performed? 94
Results 105

Chapter 11. Related Subqueries 107
How to form related subqueries 108
Results 115

Chapter 12. Using the EXISTS Operator 117
How does the EXISTS statement work? 118
Using EXISTS with Related Subqueries 119
Results 124

Chapter 13. Using the ANY, ALL, and SOME Operators 127
Special operator ANY or SOME 128
Special operator ALL 135
Operation of ANY. ALL and EXISTS for data loss or
with unknown data 139
Results 143

Chapter 14. Using the UNION clause 145
Combining multiple requests into one 146
Using UNION with ORDER BY 151
Results 157

Chapter 15. Entering, deleting and changing zero values 159
DML 160 Update Commands
Entering values ​​160
Excluding rows from table 162
Changing field values ​​163
Results 165

Chapter 16. Using Subqueries with Update Commands 167
Using Subqueries in INSERT 168
Using Subqueries with DELETE 170
Using Subqueries with UPDATE 174
Results 177

Chapter 17. Creating tables 178
CREATE TABLE 179 command
Indexes 181
Changing a table that has already been created 182
Table 183 exception
Results 185

Chapter 18. Restrictions on the set of valid data values 186
Limitations in tables 195
Results 197

Chapter 19. Data integrity support 198
Foreign and parent keys 199
FOREIGN KEY restrictions 204
What happens when you run update command 209
Results 211

Chapter 20. Introduction to Views 212
What are views? 212
CREATE VIEW 221 command
Results 223

Chapter 21. Changing values ​​using views 224
Updating Views 228
Selecting Values ​​Placed in Views 232
Results 235

Chapter 22. Defining data access rights 236
Users 237
Transfer of privileges 241
Revocation of privileges 245
Other types of privileges 247
Results 249

Chapter 23. Global Aspects of SQL 250
Renaming tables 252
How is the database hosted for the user? 253
When does change become permanent? 255
How SQL works with multiple users at the same time Results 259

Chapter 24. How to Maintain Order in a SQL Database 261
System catalog 262

The theoretical foundations of the SQL Server 2012 DBMS are examined in a simple and accessible manner. The installation, configuration and support of MS SQL Server 2012 is shown. The Transact-SQL data manipulation language is described. Covers creating a database, modifying tables and their contents, queries, indexes, views, triggers, stored procedures, and user-defined functions.
The implementation of security using authentication, encryption and authorization is shown. Attention is paid to automating DBMS administration tasks. Discusses creating data backups and performing system restores. Describes Microsoft Analysis Services, Microsoft Reporting Services, and other business analysis tools. The technology of working with XML documents, spatial data management, full-text search and much more. For beginner programmers.

In the modern world, information has the highest value, but it is equally important to be able to manage this information. This book is about the SQL query language and database management. The material is presented from a description of basic queries to complex manipulations using joins, subqueries and transactions. If you're trying to understand database organization and management, this book will be great. practical guide and will provide you with all the necessary tools. The special feature of this publication is unique way presentation of material that sets O'Reilly's Head First series apart from the many boring books on programming.

This book will teach you how to work with SQL commands and statements, create and configure relational databases, load and modify database objects, run powerful queries, improve performance, and build security. You'll learn how to use DDL statements and APIs, integrate XML and Java scripts, use SQL objects, create web servers, work with remote access, and perform distributed transactions.
In this book, you will find information such as working with in-memory databases, streaming and embedded databases, databases for mobile and handheld devices, and much more.

SQL for Mere Mortals is a complete introduction to a structured query language, written specifically for beginners.

If you have no experience managing databases, this book will teach you how to work with SQL easily and fluently, using simple queries and complex operations. To master SQL:

- Understand the concepts associated with database management through a concise and simple introduction into relational databases.
— Follow these instructions to use basic SQL commands to find and manipulate information in data tables. Learn to select, summarize, and manage data skillfully.
— Work effectively with compound data tables by applying advanced query techniques to more than one table at a time, constructing complex queries and subqueries.
— Create new data tables for trading business applications. Learn important principles of effective database design and techniques for ensuring data integrity and security.
— Learn to use SQL with programming languages ​​using a special chapter for programmers.

SQL is older than most of us, so I can’t claim to be conveying some extraordinary stuff through this book. What makes this title unique is its slender size. If you are looking for a real compact practical guide on SQL, then this book is for you. For beginners, I have tried to confine an ocean to a bucket in order to equip them with SQL knowledge in the shortest time possible. SQL language is too voluminous and exposure of every aspect of this huge language is a very tedious task. Keeping aside the least utilized features, this book is rolled out to focus on the more operational areas of the language. It is meant to help you learn SQL quickly by yourself. It follows a tutorial approach wherein hundreds of hands-on exercises are provided, augmented with illustrations, to teach you SQL in a short period of time. Without any exaggeration, the book will expose SQL in record time. The book explicitly covers a free platform of the world’s number 1 DBMS to expose SQL: Oracle Database Express Edition. I have chosen Oracle XE because it is free to develop, deploy, and distribute; fast to download; and simple to administer.

Beginning Oracle PL/SQL gets you started in using the built-in language that every Oracle developer and database administrator must know. Oracle Database is chock-full of built-in application features that are free for the using, and PL/SQL is your ticket to learning about and using those features from your own code. With it, you can centralize business logic in the database, you can offload application logic, and you can automate database- and application-administration tasks.

Author Don Bales provides in Beginning Oracle PL/SQL a fast-paced and example-filled tutorial. Learn from Don\’s extensive experience to discover the most commonly used aspects of PL/SQL, without wasting time on obscure and obsolete features.

Book “SQL. The User's Bible is unique in that each chapter compares implementations of the SQL query language standard in the three leading DBMSs. The result is a comprehensive and practical guide for database users, from beginners to professionals. This book on SQL conveniently combines theory with practice, contains a description of new technologies and will allow you to understand the numerous nuances of the SQL query language standard and its implementations. It can be used as a reference book - a kind of desktop aid.
— Learn the basics of SQL query language and relational databases
— Master working with tables, views, sequences and other database objects
— Learn to use transactions and locks in a multi-user environment
— Explore the features offered by the SQL standard and three leading database vendors
— Learn how to access metadata and implement database security controls
- Explore additional topics: SQL to XML integration, OLAP business intelligence and more

If you have basic HTML skills, then with the help of the book by Robin Nixon, an experienced developer and author of numerous best-selling books on web mastering, you will easily learn how to create dynamic sites characterized by high level interaction with users.
Discover the combination of PHP and MySQL, learn how they make it easier to create modern websites, and learn how to add JavaScript capabilities to these technologies to create high-tech applications.
This guide looks at each technology separately, shows how to combine PHP, MySQL and javascript into one, and introduces the latest web programming concepts. With the help of detailed examples and test questions given in each chapter, you will be able to consolidate the studied material in practice.

This guide will help you:
- master PHP basics and object-oriented programming;
— thoroughly study MySQL, starting with database structure and ending with composing complex queries;
— create web pages that use PHP and MySQL to combine forms and other HTML elements;
— learn javascript, starting with functions and event handling and ending with access to the document object model (DOM);
- use libraries and software packages, including the Smarty system, the PEAR program repository and the Yahoo! User Interface;
— make Ajax calls and turn your website into a highly dynamic information environment;
— upload files and images to the website and work with them, check the data entered by the user;
— ensure the security of your applications.

Queries not running fast enough? Wondering about the in-memory database features in 2014? Tired of phone calls from frustrated users? Grant Fritchey’s book SQL Server Query Performance Tuning is the answer to your SQL Server query performance problems. The book is revised to cover the very latest in performance optimization features and techniques, especially including the newly-added, in-memory database features formerly known under the code name Project Hekaton. This book provides the tools you need to approach your queries with performance in mind.

SQL Server Query Performance Tuning leads you through understanding the causes of poor performance, how to identify them, and how to fix them. You’ll learn to be proactive in establishing performance baselines using tools like Performance Monitor and Extended Events. You'll learn to recognize bottlenecks and defuse them before the phone rings. You’ll learn some quick solutions too, but emphasis is on designing for performance and getting it right, and upon heading off trouble before it occurs. Delight your users. Silence that ringing phone. Put the principles and lessons from SQL Server Query Performance Tuning into practice today.

Covers the in-memory features from Project Hekaton
Helps establish performance baselines and monitor against them
Guides in troubleshooting and eliminating bottlenecks that frustrate users
What you'll learn
— Establish performance baselines and monitor against them
— Recognize and eliminate bottlenecks leading to slow performance
— Deploy quick fixes when needed, following up with long term solutions
— Implement best-practices in T-SQL so as to minimize performance risk
— Design in the performance that you need through careful query and index design
— Take advantage of the very latest performance optimization features in SQL Server 2014
— Understand the new, in-memory database features formerly code-named as Project Hekaton

The book SQL in 10 minutes offers simple and practical solutions for those who want to get results quickly. After working through all 22 lessons, each of which will take no more than 10 minutes, you will learn everything you need to practice using SQL. The examples given in the book are suitable for IBM DB2, Microsoft Access, Microsoft SQL Server, MySQL, Oracle, PostgreSQL, SQLite, MariaDB and Apache OpenOffice Base. Visual examples will help you understand how SQL statements are structured. Tips will suggest shortcuts to solutions. Warnings will help you avoid common mistakes. Notes will provide further clarification.




2024, leally.ru - Your guide in the world of computers and the Internet