Contents of mysql
Introduction Commands examples
Introduction to mysql
MySQL is a powerful Relational Database Management System (RDBMS) which we will use to learn the basic principles of database and data manipulation using Structured Query Language (SQL) statements. SQL is a database language that is used to retrieve, insert, delete and update stored data. This is achieved by constructing conditional statements that conform to a specific syntax
How does mysql works?
MySQL is a database server program and as such is installed on one machine, but can 'serve' the database to a variety of locations
Mysql syntax
mysql syntax: The great thing about everything you do in MySQL
is that the "code" is very easy for humans to read, as opposed to harder programming languages like C or C++. Very few special characters and symbols are required to create a MySQL query,
Mysql commands
CREATE DATABASE
CREATE TABLE
INSERT
REPLACE
UPDATE
SELECT
DELETE
WHERE
IN
AND
OR
RLIKE
DISTINCT
VALUES
SET
MAX
Create table
CREATE DATABASE:
CREATE DATABASE database_name ;
Will create a MySQL database.
Example:
mysql> CREATE TABLE users (
-> id INT NOT NULL AUTO_INCREMENT,
-> name VARCHAR (50),
-> email VARCHAR (50),
-> PRIMARY KEY (id));
Table created.
Primary key
The Primary Key is a type of index MySQL uses. This index can do such things as;
Quickly find the rows that match a WHERE clause. Retrieve rows from other tables when performing
joins. Sort or group a table if the sorting or grouping is done on a leftmost prefix of a usable key
This can definitely help boost the speeds of your queries as well.
To add data
Adding Data to a Table Adding your Data to a table is not that hard of a
process at all. Basically you specify what table you are inserting the values into, then you go ahead and do so. The syntax is as follows;
mysql> INSERT INTO users VALUES ("NULL","BlairIreland","[email protected]");
If successful, you should get something like the following for a response;
Query Ok, 1 row affected (0.05 sec)
Viewing data
Viewing Data:
After you add data to your table, you probably want to check it out to make sure everything went as planned. To do so, you would utilize the SELECT command.
To view all data in the table, you would use something like this;
mysql> SELECT * FROM users;
This will give you an output like this
2 rows is set.
To select a particular row
To select a particular row in this database though, you would use this sort of command;
mysql> SELECT * FROM users WHERE (name="Blair Ireland");
This would give you
+----+---------------+---------------------------------+
| id | name | email |
+----+---------------+---------------------------------+
| 1 | Blair Ireland | [email protected] |
+----+---------------+---------------------------------+
To select specific column
select specific columns, like this; mysql> select name from users;
+----------------+ | name | +----------------+ | Blair Ireland | | Mark Hardy | +----------------+
Modifying database
Modifying Database Data
If you have data already in the database that needs some modifying, you would change it by utilizing the UPDATE command in mysql.
Its use is something like this;
mysql> UPDATE users SET email = '[email protected]'
-> WHERE email = "[email protected]";
This would just change all rows with email set to [email protected] and change them to [email protected]. In this case though, only one entry has [email protected] as its email, so only one entry would be changed.
Deleting data
Deleting Database Data If you want to remove data in the database, you
would use MySQL's DELETE command. Its use would be as follows
mysql> DELETE FROM users WHERE (name="Mark Hardy");
This would delete Mark Hardy's entry in the database, leaving only Blair Ireland's entry in it.
Advanced commands: sql> CREATE TABLE users ( -> id INT NOT NULL AUTO_INCREMENT, -> name VARCHAR (50), -> email VARCHAR (50), -> PRIMARY KEY (id)); Table created.
Search command
To do a general search, you would use the following syntax;
mysql> SELECT * FROM test WHERE
-> (name LIKE "%B%");
This will result in finding anything with the capital letter B in the column name. Notice the two %'s used. This checks for anything before or after that letter. You can use just one if you like though.
You can place that % sign anywhere within the query though, as the search is based upon the placement of this character.
Order by command
To use a literal wildcard character in your searches, you Order By
mysql> SELECT * FROM users WHERE
-> (name = "Joe%") ORDER BY id DESC;
This will return all the records containing someone with the first name of Joe, and will output it from the greatest ID Number, descend until the lowest ID number is reached.
The default for ORDER BY is ascending, so if you want it to go by the lowest ID number first, you would just type in ORDER BY id, or you could plug in the ASC keyword where DESC is currently. Both would give you the same result.
Logical operators
mysql> CREATE TABLE users ( -> id INT NOT NULL AUTO_INCREMENT, -> name VARCHAR (50), -> email VARCHAR (50), -> PRIMARY KEY (id));
Not operator
NOT (or) ! mysql> SELECT * FROM users WHERE -> (name != "Blair Ireland"); or mysql> SELECT * FROM users WHERE -> (name NOT = "Blair Ireland"); This query would return all records without Blair
Ireland present as the name.
And operator
AND (or) &&
mysql> SELECT * FROM users WHERE
mysql> (name = "Blair Ireland") AND mysql> (email = "[email protected]");
or
mysql> SELECT * FROM users WHERE
-> (name = "Blair Ireland") &&
-> (email = "[email protected]");
This query would return all records with Blair Ireland present as the name, and [email protected] as the email.
Or operator
mysql> SELECT * FROM test WHERE
-> (name = "Blair Ireland") OR
-> (email = "[email protected]");
or
mysql> SELECT * FROM test WHERE
-> (name = "Blair Ireland") ||
-> (email = "[email protected]");
This query would return all records with Blair Ireland present as the name, or records with [email protected] as the email.
Renaming and deleting an entire table
Renaming a Table: mysql> ALTER TABLE users RENAME public; Deleting an entire table: To delete (or drop) an entire table, you would use
the following syntax; mysql> DROP TABLE public; If you would like to drop more tables at once
though, you would do this; mysql> DROP TABLE public, tests;
Remove and optimize table
Remove a Column:
mysql> ALTER TABLE public DROP COLUMN time;
After you make these changes to the table, you may want to optimize the table afterwards (especially if you are using VARCHAR's, TEXT's or BLOB's, as this will optimize its memory allocation. You will also want to do it if you have deleted a large part of a table.
During a table optimization, the original table is available to clients, however, modifying and adding to the table is stalled until optimization is complete.
The syntax is:
OPTIMIZE TABLE table_name_goes_here