An article from a series about creating a Java project (links to other materials are at the end). Its goal is to analyze key technologies, the result is to write a telegram bot.
Good health, dear readers. Today we will talk little and write more, because hands remember much better than eyes. You might have a question: why do we use MySQL? There are, for example, the same PostgreSQL and other solutions. Everything is simple here. First, it's free. Secondly, it has a convenient UI interface (we will talk about it in a separate article). Thirdly, she is familiar and pleasant to me. In addition to all of the above, MySQL is also very common.
Who did - well done! It doesn't matter what level you are in CodeGym - it is important to be able to install and configure the necessary tools for work according to the manuals. And I assure you, there will be many. Who tried, but did not succeed - is also not scary. It doesn’t always happen the first time: sometimes you need to take a break and try again with fresh energy. Who installed MySQL himself - write "+" in the comments so that I know that someone is doing it. I will do it myself too. Since I no longer have a laptop on Windows, I will show on a macbook. I do not think that the installation process will be very different. For those who want to install it themselves using the English manual, please click here .
Download and run. However, things didn't go right for me right away. It turned out that Apple does not skip the installation of the latest version:
You can, of course, disable this business, but the MacBook is working, and I will not go into its settings. Therefore, by trial and error, I found a version that can be installed - this is 5.7.21 . To find other versions, I went to the Archive tab and selected the required version:
Next, run the downloaded file. Click next, next, next, but at the end, be careful! There will be a username and password to access the database. It looked like this for me:
It says that there will be a user namedroot and password Vac/zto=.24q . This completes the installation.
Go to it and see that the server must first be enabled. To do this, simply click Start MySQL Server and that's it. Yes, you can still check the box so that the server rises in the background every time the machine starts.
It is important to note that I will begin all requests in the terminal with the $ symbol to make it clear. Next, you need to directly connect to the MySQL server through the command line. To do this, open the terminal and write: $ mysql And there are two options:
There we write the password, and we will be happy like this:
Just the last line says that we entered the MySQL terminal server. But this password for root turned out to be one-time, and it needs to be changed to another one. For security reasons, of course. If you do not change and try to create a new database in the server, we will get the following response: ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement .
Forced, so to speak. Well, okay, what is it for us, is it difficult or what? Let's make the password simpler (this is only for local work, for real work the password should be complex, of course). Let's say root∫, like the username. To change the password, enter the line: ALTER USER 'root'@'localhost' IDENTIFIED BY 'root'; And the answer will be:
To make sure everything works correctly, let's try to create a new database using the following syntax : $ CREATE DATABASE test_db; To make sure that such a database is created on the server, let's write the command: $ SHOW DATABASES; And as a result we get the following:
That's all :) It's impossible to say that it was simple and fast. Therefore, whoever did it himself is a separate respect. Who did not - do not worry, the manuals are for all types of installation:
There everything is described quite sane. The only thing is that everything is in English. Now we write our first lines in SQL.
In general, the word DROPis responsible for deleting something. In this case, we have deleted the database. But it can also be used for other purposes (like dropping a table). And now, to trust me to check the result, let's see how many databases are left: $ SHOW DATABASES;
As we can see, there is no database named test_db. CHTD :D
The next step is to create a table. Which? Well, for starters, let's create a table with countries. Why is that? Because cities need a foreign key of the country in which it is located. And it can be obtained after these countries generally begin to be in the database (hereinafter - abbreviated from the database). As we remember from the previous article, in the table where countries are described, we have two fields - ID and name, where ID is a unique identifier, and name is the name of the country. Therefore, creating such a database is as easy as shelling pears. We write: $ CREATE TABLE country (id INT, name VARCHAR(30));
As you can see, everything went well. By analogy with SHOW DATABASES, we can see the list of tables: $ SHOW TABLES;
Here it is, our table. Now let's learn how to delete tables. To do this, write: $ DROP TABLE country;
It is important to note that once a table or database is deleted, it cannot be restored!
As you can see, all information about the table is written here. To add something to a table, you need to use the INSERT INTO table_name VALUES() command . Let's add three entries for three countries: $ INSERT INTO country VALUES (1, 'Ukraine'); $ INSERT INTO country VALUES(2, 'Russia'); $ INSERT INTO country VALUES(3, 'Belorus');
To look at what is stored in the database, we will use the simplest query to get data. To do this, we want to get the data of all fields from the country table. Remember the previous sentence carefully because the sql query would be: $ SELECT * FROM country; Where * means to take all fields.
Here is the beauty we have. If we want to get only a specific field, it’s not a problem: instead of a star, we write the names of the fields that should be separated by commas. Try it yourself ;) We have already created a table, but somehow we have little information on it. The population would obviously not hurt, right? And how to do it?
As you can see from the figure, we have successfully added a new field for the table and with the second command we checked that the field is in the schema. The second step is to update the records in the table and assign values. We take the number from Wikipedia. Not that I trust them, but nevertheless, this is the fastest and easiest way. To do this, use another DML command. But first, let's talk about what we want to do: we want to update the country table, make the population field equal to 41806221 in the record where id = 1. And here's what this construction looks like: UPDATE table_name SET table_row1 = value WHERE table_row2 = value2; Based on it, we write: $ UPDATE country SET population = 41806221 WHERE id = 1; This is where we used the WHERE
keyword for the first time.. It is needed to add values for filtering. For example, this time we said that we only need to update for the field (fields) whose id is equal to one (until we told sql that this is the primary key - there may be several of them). We could also specify a condition like WHERE id > 2. If we did not specify filtering at all, then this value would be written to all table records. For Russia, Belarus and other countries, update yourself ;) And now let's try to get data from the table for the field name and population, provided that id is greater than 1. $ SELECT name, population FROM country WHERE id >1;
Thus, we received only those fields that we ourselves wanted. Through INSERT INTO, you can specify which fields we want to add and which ones to leave empty. To do this, write: $INSERT INTO country (name, population) VALUES('Georgia', 1234566); To check what happened, let's look at the list of all fields: $ SELECT * FROM country;
Without an id, the record is bad, and the population, of course, is taken out of your head, so you need to delete it. Use the command DELETE FROM table_name : $ DELETE FROM country WHERE name = 'Georgia';
Thus, we learned to delete a field from the table.
- Checking homework
- Installing MySQL on MacOS
- Entering the MySQL command line
- Writing our first lines in SQL
- Homework
- Results
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 1](https://cdn.javarush.com/images/article/fac0d5cb-4b9e-42d8-aa4f-952a025a5717/800.jpeg)
Checking homework
Last time I gave the task - to install MySQL on my machine and go into it. Through the console, or in some other way. All done?!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 2](https://cdn.javarush.com/images/article/93e3bb97-d2fa-4150-9c62-1907f43456e1/800.jpeg)
Installing MySQL on MacOS
First of all, go to the following link , select the operating system, be it Windows, Ubuntu or MacOS, as shown below:!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 3](https://cdn.javarush.com/images/article/1f9144c4-1333-4b50-a1e8-c34c34a96ed9/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 4](https://cdn.javarush.com/images/article/dcdda15e-34e1-4810-84fb-d2302df04917/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 5](https://cdn.javarush.com/images/article/2216fec1-2bc8-4d0d-bb84-76ac6bfb372b/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 6](https://cdn.javarush.com/images/article/ff086372-b560-4f20-9158-d1a26db269e8/512.jpeg)
Entering the MySQL command line
To log in, you need to make sure that the MySQL server is enabled. To do this, go to System Preferences , find the MySQL icon there:!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 7](https://cdn.javarush.com/images/article/f1d88583-6101-4eb7-9fa8-3ee2074fb1a7/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 8](https://cdn.javarush.com/images/article/a98d9994-6dab-45b6-a6ea-6b8b0ee32925/512.jpeg)
- If the response is "ERROR 1045 (28000): Access denied for user 'roman_beskrovnyi'@'localhost' (using password: NO" ) , then the server is installed and retracted. You just need to pass the correct command to it.
- If the answer is command not found: mysql , it's okay - there is simply no link to the server. To do this, you will need to make an alias with the mysql variable, so we write the following line:
$ alias mysql=/usr/local/mysql/bin/mysql
This will only help for this terminal session. But we don’t need more - we definitely won’t work through the terminal all the time.
- MySQL is not installed, do not fool your computer.
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 9](https://cdn.javarush.com/images/article/fa59d6ad-cb6d-4651-bb18-282583b49eb7/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 10](https://cdn.javarush.com/images/article/4a143191-212b-483f-abf5-27cdd3e2f482/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 11](https://cdn.javarush.com/images/article/8defcfae-6d0b-41f9-872b-e5d0cd2795b1/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 12](https://cdn.javarush.com/images/article/1c3f3566-ed2c-4a8e-a900-77ab5cec46b4/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 13](https://cdn.javarush.com/images/article/5994bd31-cd8e-40dc-8a92-276a4af58033/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 14](https://cdn.javarush.com/images/article/a27b7388-7969-4bc1-92e2-b779b65c64cf/1080.jpeg)
Writing our first lines in SQL
As you may have noticed above, we know two commands:- CREATE DATABASE - creates a database on the server;
- SHOW DATABASES - Shows all created and available databases on the server.
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 15](https://cdn.javarush.com/images/article/e4915fa4-90e9-4e21-bc10-e23b73647073/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 16](https://cdn.javarush.com/images/article/aa74752e-7ded-4cb2-8e62-4327bbbbc2df/512.jpeg)
Create your database and table
Create the cities database: $ CREATE DATABASE cities; To work in a specific database, you need to log into it. To do this, use the USE database_name command , after which all commands for working with tables will be carried out in this particular database. $ USE cities; As you can see from the response - Database changed , the transition to this database was successful.!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 17](https://cdn.javarush.com/images/article/9c239065-8f91-49c6-9a4f-7a011aa4713f/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 18](https://cdn.javarush.com/images/article/866ccc9b-86c5-4cd1-bb61-d7e42f4d6e4d/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 19](https://cdn.javarush.com/images/article/bf7c42e3-7ecd-4c91-88d4-8373bd888a7f/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 20](https://cdn.javarush.com/images/article/77779b71-4a55-42d5-b14b-7240f1b97f41/512.jpeg)
Working with a table
Let's restore the table we created. To do this, you just need to repeat what you have already done. To look at the table structure, you need to use the following command - DESC table_name : $ DESC country;!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 21](https://cdn.javarush.com/images/article/e3cf91cd-3e24-4a88-b572-ddc299fb8a38/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 22](https://cdn.javarush.com/images/article/0cf6b5f0-f904-4342-b4f6-06e3f797f315/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 23](https://cdn.javarush.com/images/article/05664f83-761a-4eec-91fb-d3f750d5eacf/512.jpeg)
- Add a new field to the table schemas - population.
- Update existing entries.
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 24](https://cdn.javarush.com/images/article/afa0739a-6fc1-407c-86dd-2179c857f88b/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 25](https://cdn.javarush.com/images/article/dd2512e7-6d8e-456a-9fbb-ddf1bef5a7ee/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 26](https://cdn.javarush.com/images/article/085093a1-3af2-4f7a-9568-cb7417272880/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 27](https://cdn.javarush.com/images/article/fadba59c-8300-4b12-a259-5783ed6889d8/512.jpeg)
!["Java-project from A to Z": we analyze databases and the SQL language. Part 2 - 28](https://cdn.javarush.com/images/article/e6e409b2-f926-412a-854b-17a50a543d19/512.jpeg)
Homework
Homework will be as follows:- You need to add a primary key (PRIMARY KEY) from the ID field to the schema of the country table.
- Add another country to the country table - Moldova.
- According to the scheme of the previous article, create a table city, which will contain all the described fields. The field names will be the following: id, name, country_id, population.
- Add a primary key to the city table.
- Add a foreign key to the city table.
Summing up
In this article, we have done a lot of things. First, we installed and started the MySQL database server. Secondly, we learned a whole list of keywords and queries with which we can now create / delete databases and tables. We learned how to add data to tables, update and delete them. Here are all the commands we used, along with their descriptions:- CREATE DATABASE database_name; - create a new database
- SHOW DATABASES; - view the list of databases
- DROP DATABASE database_name; - delete database
- USE database_name; - work in database
- CREATE TABLE table_name (id INT, ....); - create a table
- SHOW TABLES; - view list of tables
- DESC TABLE table_name; - view table schema
- DROP TABLE table_name; - delete table
- INSERT INTO table_name VALUES(); - add a field to the table
- SELECT * FROM table_name WHERE table_row = value; - get the fields in the table under the condition after WHERE
- SELECT * FROM table_name; - get all fields in the table
- ALTER TABLE table_name ADD COLUMN column_name COLUMN_TYPE; - add a new field field to the table schema
- UPDATE talbe_name SET talbe_row1 = value1 WHERE table_row2 = value2; - update records in the table: set the values of one field under the condition for another field
- DELETE FROM table_name WHERE table_row1 = value1; - remove a field (or fields) from the table under a certain condition.
GO TO FULL VERSION