SQL is Structured Query Language, which is a computer language for storing, manipulating and retrieving data stored in a relational database. As mentioned before, SQL is one of the most widely used query language over the databases.
I'm going to list few of them here:. This SQL tutorial is prepared for beginners to help them understand the basic as well as the advanced concepts related to SQL languages. This tutorial will give you enough understanding on the various components of SQL along with suitable examples.
Before you start practicing with various types of examples given in this tutorial, I am assuming that you are already aware about what a database is, especially the RDBMS and what is a computer programming language. I had even mentioned this as one of the top skills in my post about 10 things every programmer should know, if you haven't read that yet, you can read it here, it's completely worth your time.
L has been around for many years, and it will be around for many more coming years. It is also one of the matured technology; hence, there are a lot of free resources, like books, articles, tutorials, and courses are available online. I have been also sharing a lot of free eBooks, websites, and courses on different technologies, like Java, Python, and Database, and today, I'll share some of the free SQL books online and offline reading. Some of the books are available for free to read online others you can download in PDF format.
Unlike many other books available online, these are not pirated or illegal copies. In fact, these are the books that are made available for free from publishers and authors for the benefit of the community. So, you can safely download and read at your convenience.
Here is my list of some of the best books to learn SQL, which is absolutely free. You can either download their PDF version for offline reading, or you can read them online. This book is for developers; it avoids unnecessary details about database internals. All you need is to create an Udemy account to access this course and it will be free for a lifetime. Once you understand key concepts like how SQL query gets executed, and how indexes are used to make your query faster, you can read this book to dig deeper.
Ideal for novices and experienced Oracle programmers alike. If you like the paperback version, then you can buy that from Amazon here. Author: Ron Plew and Ryan Stephens.
Snodgrass is available for free in pdf format from the author. It offers incisive advice on recording temporal data using SQL data types, defining appropriate integrity constraints, updating temporal tables, and querying temporal tables with interactive and embedded SQL.
Also, it provides case studies detailing real-world problems and solutions in areas such as event data, state-based data, partitioned data, and audit logs. New material covers more information.
There is no more straightforward text for learning the syntax and structure of SQL. Download it for free. We will talk about and show a few examples in the coming lessons. Why Use a Database? Databases are most useful when it comes to storing information that fits into logical categories. For example, say that you wanted to store information of all the employees in a company.
With a database you can group different parts of your business into separate tables to help store your information logically. Example tables might be: Employees, Supervisors, and Customers. Each table would then contain columns specific to these three areas. To help store information related to each employee, the Employees table might have the following columns: Hire, Date, Position, Age, and Salary.
If you are unsure, please contact your web host. Although you can set up MySQL manually on your home PC, it can be rather difficult for a beginner to do, and would require more than a few lessons! If you think you've got what it takes, or you're just mentally unstable, head on over to MySQL.
This tutorial assumes that you are using the most popular, CPanel. First, find the link that allows you to administer MySQL. Once there, you will need to do the following before you can start using MySQL. Create a new database.
Create a new user with password. Assign the user to the database If you have problems with this steps, seek help from your web hosting provider or ask a question in the Tizag Forums. Helpful Tool - phpMyAdmin! Also supplied by most hosting services is phpMyAdmin you can also install it anywhere you want, as it's open source and free. This tool will allow you to view all the MySQL database, tables, and entries, as well as perform SQL queries remotely through a web browser.
It's easy-to-use interface will allow you to do many common MySQL tasks quickly and easily, saving you many beginner headaches and helping you understand what's going on in a more visual manner. If you already have that base covered feel free to skip on to the next lesson.
Type the keyword "cmd" into the text field and press Enter to launch Window's command line interface. The most popular options include:. MySQL Administrator This tool comes from the creators of MySQL, so you can be assured they have a solid understanding of database optimization and stability for power users.
A brief overview of their product Navicat Admin can be found on their website. Very few special characters and symbols are required to create a MySQL query, and most queries consist entirely of English words! Because this tutorial focuses on the combination of MySQL and PHP, most of the examples are ready for you to copy, paste, and run on your web server. This helps draw them out from the rest of the code and makes them much easier to read. Capitalizing them allows you to tell from a quick glance that this query selects data from a table.
Do not worry if it takes you more than a week to finish this tutorial. If you take the time to progress slowly, you will be much more well-informed about the MySQL database system than if you rushed through it in one sitting.
Rather a MySQL database is a way of organizing a group of tables. If you were going to create a bunch of different tables that shared a common theme, you would group them into one database to make the management process easier. Create a database and assign a new user to this database. For all of our beginning examples we will be using the following information:. Server - localhost. Database - test.
Table - example. Username - admin. Password - 1admin Note: The table may change in the advanced lessons, but everything else will remain the same! The server is the name of the server we want to connect to. Because all of our scripts are going to be placed on the server where MySQL is located the correct address is localhost.
If the MySQL server was on a different machine from where the script was running, then you would need to enter the correct url ask your web host for specifics on this.
Your database, table, username, and password do not have to match ours. If you choose a different set of login information, remember to insert your own information when copying the scripts in this tutorial.
Status Check So far, you should have created a new database and assigned a user to it. You should not have created a table yet. If you are up-to-date, then continue the tutorial.
We will be making our first table in an upcoming lesson. This is done with the MySQL connect function. MySQL localhost If you've been around the internet a while, you'll know that IP addresses are used as identifiers for computers and web servers. In this example of a connection script, we assume that the MySQL service is running on the same machine as the script. Please contact your web host for more details if localhost does not work. Server, username, and password. In our example above these arguments were:.
Password - 1admin The "or die mysql Double-check your username, password, or server if you receive this error. Choosing the Working Database After establishing a MySQL connection with the code above, you then need to choose which database you will be using with this connection. If you are up-to-date then continue the tutorial. We will be making our first table in the next lesson. In MySQL and other database systems, the goal is to store information in an orderly fashion.
The table gets this done by making the table up of columns and rows. The columns specify what the data is going to be, while the rows contain the actual data. Below is how you could imagine a MySQL table. This table has three categories, or "columns", of data: Name, Age, and Weight. This table has four entries, or in other words, four rows.
Create Table MySQL Before you can enter data rows into a table, you must first define what kinds of data will be stored columns.
We are now going to design a MySQL query to summon our table from database land. In future lessons we will be using this table, so be sure to enter this query correctly! That's a lot of code all at once!
Let's get down in the dirt and figure this stuff out. We will be going through the code line by line. The two capitalized words are reserved MySQL keywords. Clear names will ensure that you will know what the table is about when revisiting it a year after you make it.
The column "id" is not something that we need to worry about after we create this table, as it is all automatically calculated within MySQL. INT - This stands for integer or whole number. This means that no two ids can be the same, or else we will run into trouble.
This is why we made "id" an auto-incrementing counter in the previous line of code. It's "variable" because it can adjust its size to store as little as 0 characters and up to a specified maximum number of characters. We will most likely only be using this name column to store characters A-Z, a-z. The number inside the parentheses sets the maximum number of characters. In this case, the max is Notice that there are no parentheses following "INT".
MySQL already knows what to do with an integer. The possible integer values that can be stored in an "INT" are -2,,, to 2,,,, which is more than enough to store someone's age! Your Homework Using the MySQL administration tool that your web host has, check to see if the table was created correctly.
Afterwards, try creating a few of your own, with PHP or with a MySQL administration tool, to be sure that you have gotten the hang of it. When inserting data it is important to remember the exact names and types of the table's columns.
If you try to place a word essay into a column that only accepts integers of size three, you will end up with a nasty error!
Inserting Data Into Your Table Now that you have created your table, let's put some data into that puppy! This code is much simpler to understand than the create table code, as will be most of the MySQL queries you will learn in the rest of this tutorial. Once again, we will cover the code line by line. The name of the table we specified to insert data into was "example".
Here we enter the name Timmy Mellowman for "name", and 23 for "age". Be sure to use your MySQL administration program provided by your web host to ensure that the data was inserted into your table correctly. Be careful not to run this script more than once, otherwise you will be inserting the same people, multiple times.
This is called inserting duplicate records and is usually avoided. We have already created a new table and inserted data into that table. In this lesson we will cover the most common MySQL Query that is used to retrieve information from a database.
If you wanted to copy some information in a document, you would first select the desired information, then copy and paste. In this example, we will output the first entry of our MySQL "examples" table to the web browser.
Below is a step-by-step walkthrough of the code. In English, this line of code reads "Select every entry from the table example". In our MySQL table "example," there are only two fields that we care about: name and age. These names are the keys to extracting the data from our associative array.
In the next lesson we will see how to retrieve every entry of a table and put it into a nicely formatted table. If you have been jumping around our MySQL Tutorial then you would have already seen this function popping up all over the place. It isn't something you can directly manipulate, that is for sure. Additional PHP functions are required to extract the data from this Resource. Our table example basically looks like the table below.
In our table example these are: name and age. You can keep doing this until the MySQL Resource has reached the end which would be three times in our example. Sounds like an awfully repetitive task. We will get a new row of MySQL information that we can print out each time the while loop checks its conditional statement.
Now that we know what we need to do and how to go about doing it, the code pretty much writes itself, so let's move on to the next lesson. Just kidding! You could apply this script to any MySQL table as long as you change both the table name in the query and the column names that we have in the associative array. In this example we will select everything in our table "example" and put it into a nicely formatted HTML table.
If you added more entries to your database's table, then you would see each additional row appear in the above table. Since we want to use this data in our table we need to store it in a variable. By putting it in a while loop it will continue to fetch the next array until there is no next array to fetch. This function can be called as many times as you want, but it will return FALSE when the last associative array has already been returned.
By placing this function within the conditional statement of the while loop, we can kill two birds with one stones. We can tell the while loop to stop printingn out information when the MySQL Resource has returned the last array, as False is returned when it reaches the end and this will cause the while loop to halt.
In our MySQL table "example" there are only two fields that we care about: name and age. These fields are the keys to extracting the data from our associative array. It might be useful to try out other methods of HTML formatting as well. See which one you like best!
Those entries that do not pass the test will be left out. We will be assuming the data from a previous lesson for the following examples. To select Sandy only we could either specify Sandy's age 21 or we could use her name Sandy Smith. In the future there may be other people who are 21, so we will use her name as our requirement. WHERE is used in conjuction with a mathematical statement.
Here's how to do it. With the tools you have now, you could make 10 different queries, one for each age 20, 21, Experiment with it so you can see for yourself how powerful this little trick can be. Note: The wildcard was used for example purposes only. If you really wanted to explicilty select people who are in their 20's you would use greater than 19 and less than 30 to define the 20's range.
Using a wildcard in this example would select unwanted cases, like a 2 year old and your year old great-great-great-grandparents. A common way to do this in the real world is to order a big list of items by name or amount. What ORDER BY does is take the a column name that you specify and sort it in alphabetical order or numeric order if you are using numbers.
Ordering is also used quite frequently to add additional functionality to webpages that use any type of column layout. For example, some forums let you sort by date, thread title, post count, view count, and more.
We have an ordered MySQL result! Notice that we didn't have to change any of our PHP code. This is fine for simple takes, but in most real world MySQL usage you will often need to get data from multiple tables in a single query.
The act of joining in MySQL refers to smashing two or more tables into a single table. This means everything you have learned so far can be applied after you've created this new, joined table. The two tables we will be using relate to a families eating habits. In the "family" table, the Position column contains all the members of the family and their respective ages. In the "food" table the Position column contains the family member who enjoys that dish. It's only through a shared column relationship such as this that tables can be joined together, so remember this when creating tables you wish to have interact with each other.
We will be performing a generic join of these two tables using the Position column from each table as the connector. If you do not have either of them created, you can either create them using our MySQL Create Table lesson or do it manually yourself.
0コメント