Introduction
In this book you will find practical tips and detailed instructions on how to learn the SQL language in just one day.
This eBook will teach you the most important aspects of SQL. It will give you theoretical explanations, realistic examples, actual syntax and lots of codes. If you're looking for a comprehensive guide about the SQL language, this is the material you're looking for.
By reading this book, you'll learn about a wide range of SQL-related topics. Here are some examples:
- The different types of databases
- How to use SQL for a relational database
- The different types of SQL commands
- How to create a database
- How to give/revoke account privileges to users
- How to create tables and columns
- How to customize columns
- The different data types in SQL
- How to apply constraints on tables and columns
- How to work with related tables
- How to enter new information into a database
You can certainly benefit from this eBook even if you have never programmed anything before. This eBook will teach you the basics of SQL programming and give you the syntax of important SQL commands. If you want to become a skilled SQL programmer in just 24 hours, get this eBook now and read it carefully.
Lets begin the journey
Chapter 1: Fundamental Concepts Regarding SQL and Databases
This chapter will teach you the basics of the SQL language and the relational databases. By reading this material, you'll know how the language works and how to use a relational database.
The Databases
SQL (i.e. Structured Query Language) allows you to create and maintain relational databases. It also allows the management of information inside those databases. Before discussing SQL further, let's define what the term database means. People use this term when referring to a collection of information (e.g. names, phone numbers, addresses, etc.) that is organized according to a definite structure.
Here are the most common structures used in modern databases:
- Hierarchical This structure uses parent-child relationships. It organizes information through nodes, which are the counterpart of tables in relational databases. Parent nodes can have multiple child nodes. Child nodes, however, can have just one parent. The hierarchical structure is extremely popular, although it is inflexible and doesn't support complicated relationships.
- Network The network structure solves the problems found in the hierarchical structure. Here, you will organize information into record types. This structure arranges information into a system that links pairs of records: members and owners. A record type can work with other record types. Thus, a network database supports complicated searches and relationships. The main disadvantage of this structure is its complexity: you need to know how the structure works and how information is stored.
- Relational This is the most popular structure these days. A relational database addresses the issues linked with network and hierarchical databases. In a network or hierarchical system, you'll rely on a particular database implementation, which you will hard code onto the database application you are using. You need to adjust the database application whenever you add items to your database. In a relational database, however, you won't have to worry about your application. You may alter your database without affecting the application itself.
Relational Databases
Since you will use SQL with relational databases, we need to discuss this kind of structure in detail. The main element of a relational database is the relation, which are essentially groups of rows and columns placed in a table that represents one object. An object (also known as entity), on the other hand, consists of related information.
Entities are things, events, places, persons, or concepts about which information is gathered. Each relation consists of different columns (which represent attributes). Attributes are units that define or characterize entities. For instance, in the table given below, the entity is a book with size, color, and weight as attributes.
Name_of_ Pen:Brand | Pen_Size: Size | Pen_Color: Color | Product_ Weight: Weight |
Pen A | Small | Red | Light |
Pen B | Medium | Yellow | Heavy |
Pen C | Large | Blue | Heavy |
In this table, all of the attributes have a domain (i.e. the name that specifies which type of information you can store in the attribute). You shouldn't confuse attributes with data types. Data types are particular kinds of constraints (i.e. tools used to secure information integrity) linked with columns, while domains describe what information you can include in the attributes associated with them. For instance, the attribute named Product_Weight is linked with the Weight domain.
Important Note: It would be best if you will use a domain that describes the attribute it will hold. Although this approach is optional, it can help you improve the usability and readability of your database entries.
The SQL Language
SQL is a computer language designed for relational databases. However, this language isn't a database implementation. Although the relational structure gives the core elements of a relational database, SQL supports the actual implementation of this kind of database.
SQL, one of the most popular languages today, is not similar to other programming languages (e.g. Java, C, C++, etc.). Most programming languages are procedural, which means they define how a program must perform its functions. SQL, which is a non-procedural language, focuses on the results of a program's functions. The software environment you're using will determine the database application that will complete its tasks.
The SQL language lacks the programming features of other languages. This is the reason why some programmers refer to SQL as a sublanguage. Basically, you will use SQL with other languages (e.g. C#) to perform tasks.
The SQL Statements
SQL has different types of statements that you can use in managing a database. In this section, let's divide SQL statements according to their functions:
- DDL (i.e. Data Definition Language) These statements allow you to generate, delete, or modify database objects (e.g. schemas, triggers, tables, etc.). The most popular statements of this category are CREATE, DROP, and ALTER. For instance, you must use CREATE TABLE to generate a new table, ALTER TABLE to change the properties of a table, and DROP TABLE to remove the information regarding a table from your database.
- DCL (i.e. Data Control Language) With these statements, you can specify the people or programs that can access the objects inside your database. Using DCL, you may give or take away access using GRANT or REVOKE, respectively. You can also use DCL statements to manage the kind of access database users have to your information. For instance, you may set which database users can edit a certain block of data.
- DML (i.e. Data Manipulation Language) A DML statement allows you to add, modify, delete, or retrieve information stored inside your database entries. The main keywords of the DML category are: UPDATE, SELECT, DELETE, and INSERT. You may use SELECT to retrieve information from a database table and INSERT to add information.