A Guide to MySQL for Beginners

MySQL is an open-source relational database administration system with assist for Structured Question Language (SQL). It helps within the growth of a variety of web-based purposes and on-line content material. MySQL runs on all OS platforms like Linux/UNIX, macOS, Home windows and is a vital element of the LAMP stack.

MySQL is without doubt one of the hottest database programs and plenty of cloud suppliers provide it as a part of their service. It makes use of instructions with queries and knowledge to carry out duties and capabilities by speaking with the database. This text covers essentially the most generally used MySQL statements that enable customers to effectively work with databases.

Utilizing the MySQL Shopper

MySQL allows you to hook up with a server utilizing a consumer just like the command-line instrument, mysql. Use the -u and -p flags to offer your username and password:

mysql -u [username] -p
mysql -u [username] -p [database]

If you’re completed, exit the MySQL command-line consumer as follows:


Working With Person Accounts

To create a brand new person account, open the brand new terminal to entry MySQL as the basis and create a brand new person as follows:

$ sudo mysql -u root -p
mysql> CREATE USER 'username' IDENTIFIED BY 'password';

You can even arrange a person account with restricted entry by specifying a bunch that they need to entry the database from:

CREATE USER 'person'@'localhost';
CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';


To specify a distant connection, you possibly can change the ‘localhost’ with the machine’s IP tackle as follows:

CREATE USER 'username'@'ip_address' IDENTIFIED BY 'password';

Lastly, you possibly can delete an account with the next assertion:

DROP USER 'person'@'ip_address' IDENTIFIED BY 'password';

Person Account Privileges

Earlier than transferring on, you will have to set the suitable permissions on the brand new person account. This avoids the danger of pointless person entry throughout the database.

You possibly can work with person privileges in MySQL utilizing statements akin to GRANT, REVOKE, and ALTER. Relying on the actions you need a person to have the ability to perform, you possibly can assign all or some permissions. These permissions are ALL PRIVILEGES, SELECT, UPDATE, INSERT, DELETE, CREATE, DROP, AND GRANT OPTION.

You possibly can assign the executive privilege of inserting knowledge to all tables belonging to any database:

GRANT INSERT ON *.* TO 'username'@'ip_address';

Nonetheless, you can even restrict person entry by specifying the database earlier than the interval. You possibly can enable a person to pick, insert, and delete knowledge to and from all of the tables inside a database as follows:

GRANT SELECT, INSERT, DELETE ON database.* TO 'person'@'ip_address' IDENTIFIED BY 'password';

Equally, you possibly can prohibit person entry to a selected desk by specifying a desk identify after the interval.

GRANT SELECT, INSERT, DELETE ON database.table_name TO 'person'@'ip_address' IDENTIFIED BY 'password';

You possibly can grant all permissions to each desk inside a selected database as follows:

GRANT ALL PRIVILEGES ON database.* TO 'person'@'ip_address' IDENTIFIED BY 'password';

To revoke permissions of a person from a single database:

REVOKE ALL PRIVILEGES ON database.* FROM 'person'@'ip_address'; 

You possibly can revoke all person privileges from each database as follows:


Lastly, you possibly can set passwords like this:

SET PASSWORD FOR 'person'@'ip_address' = PASSWORD('new_password');

Word using the PASSWORD perform which hashes the plaintext password.

Working With Databases

You possibly can create a brand new database with a reputation that doesn’t exist already:

CREATE DATABASE database_name;

You possibly can change the present database to a different that you just wish to work with:

USE database_name;

Lastly, you possibly can delete a whole database together with its tables as follows:

DROP DATABASE database_name;

Working With Tables

A desk is the principle structural component of a MySQL database, grouping a set of associated information as rows. Every row has columns with completely different knowledge sorts that may be CHAR, VARCHAR, and TEXT amongst many others.

The final syntax to create a desk is as follows:

CREATE TABLE table_name (column_1 data_type1, column_2 data_type2);

You can even create a brand new desk from an present desk by choosing particular columns as follows:

CREATE TABLE new_table_name AS SELECT column_1, column_2 FROM existing_table_name;

You possibly can add knowledge to a desk utilizing the next command:

INSERT INTO table_name (column_1, column_2) VALUES (value_1, value_2);

To delete a desk, use the DROP TABLE assertion as follows:

DROP TABLE table_name;

Otherwise you preserve the desk however delete all its knowledge utilizing:

TRUNCATE TABLE table_name; 

Accessing Databases

Use the next assertion to indicate all of the out there databases contained in the MySQL DMS:


Equally, you possibly can record all tables within the present database:


To view all columns inside a desk:

DESCRIBE table_name;

To show column data inside a desk:

DESCRIBE table_name column_name;

Querying Databases

MySQL means that you can use a SELECT assertion to question knowledge from the database. You should use varied MySQL clauses to increase its base performance.

The next assertion returns a outcome set consisting of two columns from each row in a desk:

SELECT column1, column2 FROM table_name; 

Or show all columns as follows:

SELECT * FROM table_name; 

You can even question databases/tables and retrieve data utilizing circumstances as follows:

SELECT column1, column2 FROM table_name WHERE situation; 

The SELECT assertion additionally means that you can group the outcome set by a number of columns utilizing the GROUP BY clause. You possibly can then use mixture capabilities to calculate abstract knowledge:

SELECT COUNT(CustomerID), Nation FROM Prospects GROUP BY Nation;

Updating Tables

You possibly can modify knowledge contained in the desk through the use of the UPDATE or ALTER statements. The UPDATE assertion means that you can replace present single or a number of information/rows.

The next MySQL command modifications the UserName and Metropolis of a single report the place the UserID is 2:

UPDATE Customers SET UserName = 'Alfred James', Metropolis= 'Munich' WHERE UserID = 2;

Whereas this instance updates all UserNames for all information the place the Metropolis is Munich:

UPDATE Customers SET UserName='Juan' WHERE Metropolis='Munich'; 

You possibly can add a column to a desk like this:

ALTER TABLE table_name ADD COLUMN column_name;

To take away a column from the desk, use the ALTER TABLE assertion as follows:

ALTER TABLE table_name DROP COLUMN column_name; 

MySQL for Newbies

On this article, you have seen the commonest MySQL instructions. They permit you to handle person accounts, change the construction of databases, and manipulate knowledge.

When you’re snug with the fundamentals, it is helpful to find out about MySQL and safety. Your database would possibly maintain precious and delicate private knowledge, so maintaining it secure from prying eyes is significant.

woman with laptop working
9 Superior MySQL Safety Suggestions

Safe your MySQL database server by following these straightforward ideas.

Learn Subsequent

About The Creator

Leave a Comment

Your email address will not be published. Required fields are marked *