Showing posts with label MYSQL. Show all posts
Showing posts with label MYSQL. Show all posts

Introduction to MySQL

About this tutorial

This is MySQL tutorial. It covers the MySQL database, various mysql command line tools and the SQL language covered by the database engine. It is an introductory tutorial for the beginners.

MySQL database

MySQL is a leading open source database management system. It is a multi-user, multithreaded database management system. MySQL is especially popular on the web. It is one of the parts of the very popular LAMP platform. Linux, Apache, MySQL and PHP. Currently MySQL is owned by Oracle. MySQL database is available on most important OS platforms. It runs under BSD Unix, Linux, Windows or Mac. Wikipedia, YouTube, Facebook use MySQL. These sites manage millions of queries each day. MySQL comes in two versions. MySQL server system and MySQL embedded system.
The development of MySQL begun in 1994 by a Swedish company MySQL AB. Sun Microsystems acquired MySQL AB in 2008. Sun was bought by Oracle in 2010. So today, Oracle corporation is the owner of the MySQL database.
MySQL, PostgreSQL, Firebird, SQLite, Derby and HSQLDB are the most well known open source database systems.
MySQL is developed in C/C++. Except of the C/C++, APIs exist for PHP, Python, Java, C#, Eiffel, Ruby, Tcl or Perl.

Definitions

A relational database is a collection of data organized in tables. There are relations among the tables. The tables are formally described. They consist of rows and columns. SQL (Structured Query Language) is a database computer language designed for managing data in relational database management systems. A table is a set of values that is organized using a model of vertical columns and horizontal rows. The columns are identified by their names. A schema of a database system is its structure described in a formal language. It defines the tables, the fields, relationships, views, indexes, procedures, functions, queues, triggers and other elements. A database row represents a single, implicitly structured data item in a table. It is also called a tuple or a record. A column is a set of data values of a particular simple type, one for each row of the table. The columns provide the structure according to which the rows are composed. A field is a single item that exists at the intersection between one row and one column. A primary key uniquely identifies each record in the table. A foreign key is a referential constraint between two tables. The foreign key identifies a column or a set of columns in one (referencing) table that refers to a column or set of columns in another (referenced) table. A trigger is a procedural code that is automatically executed in response to certain events on a particular table in a database. A view is a specific look on data in from one or more tables. It can arrange data in some specific order, highlight or hide some data. A view consists of a stored query accessible as a virtual table composed of the result set of a query. Unlike ordinary tables a view does not form part of the physical schema. It is a dynamic, virtual table computed or collated from data in the database. A transaction is an atomic unit of database operations against the data in one or more databases. The effects of all the SQL statements in a transaction can be either all committed to the database or all rolled back. An SQL result set is a set of rows from a database, returned by the SELECT statement. It also contains meta-information about the query such as the column names, and the types and sizes of each column as well. An index is a data structure that improves the speed of data retrieval operations on a database table.

Tables used

Here we will list all the tables, that are used throughout the tutorial.
-- SQL for the Cars table

USE mydb;
CREATE TABLE IF NOT EXISTS Cars(Id INTEGER PRIMARY KEY, Name VARCHAR(50), 
Cost INTEGER);
INSERT INTO Cars VALUES(1,'Audi',52642);
INSERT INTO Cars VALUES(2,'Mercedes',57127);
INSERT INTO Cars VALUES(3,'Skoda',9000);
INSERT INTO Cars VALUES(4,'Volvo',29000);
INSERT INTO Cars VALUES(5,'Bentley',350000);
INSERT INTO Cars VALUES(6,'Citroen',21000);
INSERT INTO Cars VALUES(7,'Hummer',41400);
INSERT INTO Cars VALUES(8,'Volkswagen',21600);
Cars table.
-- SQL for the Customers, Reservations tables

USE mydb;

CREATE TABLE IF NOT EXISTS Customers(CustomerId INTEGER AUTO_INCREMENT 
    PRIMARY KEY, Name VARCHAR(55));
INSERT INTO Customers(Name) VALUES('Paul Novak');
INSERT INTO Customers(Name) VALUES('Terry Neils');
INSERT INTO Customers(Name) VALUES('Jack Fonda');
INSERT INTO Customers(Name) VALUES('Tom Willis');

CREATE TABLE IF NOT EXISTS Reservations(Id INTEGER AUTO_INCREMENT
    PRIMARY KEY, CustomerId INTEGER, Day DATE);
INSERT INTO Reservations(CustomerId, Day) VALUES(1, '2009-11-22');
INSERT INTO Reservations(CustomerId, Day) VALUES(2, '2009-11-28');
INSERT INTO Reservations(CustomerId, Day) VALUES(2, '2009-11-29');
INSERT INTO Reservations(CustomerId, Day) VALUES(1, '2009-11-29');
INSERT INTO Reservations(CustomerId, Day) VALUES(3, '2009-12-2');
Customers and Reservations.
-- SQL for the Books table

USE mydb;

CREATE TABLE IF NOT EXISTS Books(Id INTEGER PRIMARY KEY, 
    Title VARCHAR(100), Author VARCHAR(60));
INSERT INTO Books VALUES(1,'War and Peace','Leo Tolstoy');
INSERT INTO Books VALUES(2,'The Brothers Karamazov','Fyodor Dostoyevsky');
INSERT INTO Books VALUES(3,'Paradise Lost','John Milton');
INSERT INTO Books VALUES(4,'Crime and Punishment','Fyodor Dostoyevsky');
INSERT INTO Books VALUES(5,'Cousin Bette','Honore de Balzac');
Books.

Sources

Continue Reading

Database engine and database table types in mysql-mysqli

Engines or Engine types are storage handlers for database tables. MySQL / MySQLi supports different engines like MyIASM, InnoDB, HEAP (Memory), CSV, ARCHIVE, BLACKHOLE, MERGE (MRG_MyISAM) etc.
Continue Reading

Backing Up & Restoring A MYSQL Database

Have you ever forgotten to back up your data... and regretted it? Losing data, however it may occur, can pretty much ruin your day. (Don't ask me how I know!) It is always important to back up your data, and databases are no exception. Lucky for us, MYSQL has already anticipated the problem and provided us with the means of easily backing up our database
Using "mysqldump", you dump all of the MYSQL statements necessary to re-create the database into a single file.
Continue Reading

Altering MYSQL Tables

The ability to change the structure of a database table after it has been created, and even after it contains data, will come in handy sooner or later. The ALTER TABLE statement gives you the ability to add, delete and rename columns, as well as create and destroy indexes and change the type of an existing column.
Continue Reading

Deleting MYSQL Data

When a record or row in a MYSQL table is no longer necessary to keep around, you can get rid of it using the DELETE statement.
Continue Reading

Updating MYSQL Data

Often, data that is stored in your MYSQL table will need to be changed or updated/replaced with new data. The UPDATE statement is used for this purpose.
Continue Reading

Selecting MYSQL Data

Now that you have inserted data into your database, you will at some point in time want to access that data. This can be done by "selecting" the data that you want.
Continue Reading

Inserting MYSQL Data

"Insert" is the term generally used to refer to the act of putting data into a MYSQL database table. You will need to know not only the name of the table that you will be inserting data into, but also the names and types of each column. You don't want to put letters into a column that only accepts integers, or a 75-character phrase into a column that only accepts 25 characters!
The syntax used to insert data into a MYSQL table is: INSERT INTO tablename () VALUES ();
Continue Reading

Deleting MySQL Tables and Databases

At some point in time you might need to get rid of a MYSQL database table. A single command will get the job done, so take care to not delete the wrong table. Also, make sure that you really don't want any of the information stored in the table, because it will all be deleted as well.
Continue Reading

Showing & Describing MySQL Tables

Now that we have learned how to create a table, let's look at the list of tables we have created in our database.
Continue Reading

Creating MYSQL Database Tables

Creating a table must begin with a plan. What do you want to store in your table? How many columns does your table need? What should each column be labeled? What kind of data do you plan to store in each column?
Continue Reading

Creating & Using A MYSQL Database

You may have an Administrator that will create your database for you, but if not, creating a database is a simple process.
Continue Reading

Connecting & Disconnecting From the MySQL Server

From the command line, connecting to MYSQL may require knowledge of the host, the username and the password that are provided by the hosting company that you are using.
Continue Reading

MYSQL Syntax

Syntax is a set of rules that define how MYSQL can and cannot be written. MYSQL is, in that sense, no different from any other language. If I wrote this sentence backward and upside down, it would not be following the rules, and it is doubtful that anyone would understand it.
Continue Reading

Database Design Concepts

Are you familiar with Excel spreadsheets, or HTML tables, where data can be organized into rows with multiple columns, each column neatly labeled? If not, take a peek at the following examples.
An Excel spreadsheet:
undefined
An HTML table:
QuestionAnswer
Why do birds fly South?Because it's too far to walk.
What did the bee say to the flower?Hello honey!
How do you know that cats are sensitive creatures?They never cry over spilt milk!
MYSQL databases can have multiple tables, each of which function in the same manner as an excel spreadsheet or HTML table. From the command line, a MYSQL database table might look something like this:

The moral of this story is preparation by organization. Before creating a database, write down your plan of action. Decide how many tables that you want to begin with (more can be added at any time) and then determine how many columns each table should have (the limit is 4096 columns per table) by creating a column for each type of data that will be stored. (Additional columns can be added later if you miss something now.)
We will learn how to make the following table to store information about our pets.
Continue Reading

MYSQL Requirements

Since it is safe to assume that you have access to a browser, a computer, an electrical socket to plug in your computer, and a power plant to generate the power available to your electrical socket, then the only remaining requirement to run MYSQL is a web server, which should already come with MYSQL installed if you anticipated this need.
Continue Reading

What is MYSQL?

A database is a storage place for information. Usually the information is organized into a series of related categories, where it is stored in an orderly fashion, and can be modified, updated, searched for, retrieved and/or deleted as often as necessary.
Continue Reading

MYSQL Cheat Sheet

Database Commands

  SHOW DATABASES;

  CREATE DATABASE databasename;

  USE databasename;
Continue Reading

Advanced MYSQL Queries

MYSQL queries can benefit from the use of several operators and clauses that we have not yet covered. Although for the sake of our examples we will be mostly using the SELECT statement, most of these options will work on other statements, such as UPDATE and DELETE as needed.
Continue Reading

MYSQL Data Types

Data types are assigned to a MYSQL table when the table is first created.
Let's take a quick look at some of the basic data type options available:
Continue Reading