This Is A MySQL Tutorial For Real Beginners
MySql for Beginners |
MySQL is one of the most popular and widely used in the world, so if you are planning to be a programmer, especially web-based programming, skill in MySQL is one of the required skills you must acquire.
If you just started learning programming, here’s some really step by step to work with MySQL.
1. Start by installing MySQL
I am a big windows fan, so installing mysql on windows is just like you are installing any other software, download the installer and start configuring your MySQL root password. Just set your mysql password 1234, it’s easier to remember because you are gonna use this password a lot, don’t worry it’s okay for our local computer.
To install MySQL, you can download the installer here https://www.mysql.com/downloads/, follow the installation step.
2. Make MySQL available on terminal
You must be able to use MySQL on the terminal, on windows, open environment variable and add mysql.exe to your system path, mysql binary by default is at C:\Program Files\MySQL\MySQL Server 8.0\bin.
So after opening the terminal, you can now use mysql command. The first thing to do is to login using our root user. Type in command prompt:
mysql -u root -p
2. Now some basic MYSQL
After successfully login to MySQL on your command prompt, the first thing I used to do is to create a database.
To create a database, use this command:
create database your_database_name;
Now try to check if your database exists, by showing all the databases in our system.
show databases;
3. Start using the database
You can’t do anything interesting without a database, the database is the top level schema, you can add many tables, views, functions etc inside a database.
To use the database, use use command:
use your_database_name;
4. Create a table
Now you successfully navigate to your database, it’s time to create a table to store some data in it. It is not hard. In this example we are gonna create an example table to store Movie list, with very simple columns. Here’s a sql command to create a table named MOVIES, with some columns: ID, TITLE, YEAR and RATING.
create table MOVIES( ID int 5 not null auto_increment, TITLE varchar(100) not null, YEAR int(4), RATING float(3), primary key(id) );
If you notice that there’s a primary key in our table, it is an identifier for a set of rows. You may not understand right now because our database is not integrated to any real application yet, but you will find later that it’s very important that each row must have an identifier.
5. Now check if our table is created
We can check if our table is created and have the same structure as we already define above.
To show a list of tables on a database, use show tables.
show tables;
Then to show the structure of our table, use a describe command, with syntax DESCRIBE table_name. Now to show our table that we created above, use this command:
describe movies;
6. Insert data into MYSQL table
After successfully creating a table, now we can begin to insert a bunch of data into it. Inserting data into MySQL table, you can do something like INSERT INTO table_name (col1,col2) VALUES (‘value 1’, ‘value 2’).
From our MOVIES table that we created, now we can add a bunch of movies into it.
INSERT INTO `movies` (`ID`, `TITLE`, `YEAR`, `RATING`) VALUES (1, 'The Shawshank Redemption', 1994, 9.2), (2, 'The Godfather', 1972, 9.1), (3, 'The Godfather: Part II', 1974, 9);
7. Show the data
You use SELECT command to show a data like this:
SELECT * FROM MOVIES;
8. Edit a data
You use UPDATE command to update a data, then specify which row you want to update using Where clause like this:
UPDATE FROM MOVIES SET RATING='10' WHERE ID=1;
9. Delete a data
You use DELETE command to delete a data, then specify which row you want to delete using Where clause like this:
DELETE FROM MOVIES WHERE ID=3;
From those examples above, you can try to add more tables, you create tables, insert data into it, update and delete, just do it a lot so you can get used to it.
MySQL is probably the recommended database system that every beginner should learn at first, it’s very popular and widely used, and all the commands are easy to digest, learning mysql is not that hard, after you are doing a lot practise you then get used to it and learning other database system like Postgres or MS-SQL Server wouldn’t be that much different, if you already understand all the concept on MySQL.