390 likes | 408 Views
COMP4332/RMBI4310. How to Use MySQL. Prepared by Raymond Wong Presented by Raymond Wong. You have already installed MySQL successfully. The installation guideline of the tutorial webpage shows some steps of how to use MySQL (i.e., the last few steps)
E N D
COMP4332/RMBI4310 How to Use MySQL Prepared by Raymond Wong Presented by Raymond Wong
You have already installed MySQL successfully. • The installation guideline of the tutorial webpage shows some steps of how to use MySQL (i.e., the last few steps) • For clarity, let us illustrate how to use MySQL in this set of lecture notes.
Before that, you need to understand the following concepts • Server • Client
mysqld mysql DatabaseServer DatabaseClient connect success
mysqld mysql DatabaseServer DatabaseClient SQL Statement Result of SQL
mysqld mysql A “.sql” file containing SQL Statement DatabaseServer DatabaseClient Result of SQL
Note that “server” and “client” are two concepts. • Usually, a server is a machine and a client is another machine. • However, in some cases (e.g., development), both the server and the client could be in the same machine.
Next, we will illustrate how to use MySQL in “Windows”. • Similar steps could be applied to other operating systems (e.g., Mac and linux) • Please see the tutorial webpage (near to the end of the steps of the installation guideline)
In this example, we assume that you have database called “university”. • Besides, in this database, you also have the table “student”. • After “reading” this set of lecture notes, you should know how to use MySQL. • After that, you could create your own database and create your own table by following the commands shown on the course webpage.
Type “mysqld --console” in the left command prompt and press <enter>
Some messages are shown. Those messages are related to the messages when we start the database server. Focus on the right command prompt. This is used for the database client.
Type “cd M:\Course\4332\2018\Notes\Program\MySQL” and press <enter>
Show the content of “TestQuery.sql” Type “type TestQuery.sql” and press <enter> use university; select * from student;
This TEXT message is shown and it asks us to enter our password of the database server (set up in the installation) .
Enter our password of the database server (set up in the installation) and press <enter>
We could connect the database server successfully. The “MySQL” welcome prompt is shown.
Type “select * from student;” and press <enter> The following prompt is shown.
Type “source TestQuery.sql” and press <enter> The following prompt is shown.
Type “exit” and press <enter> The following prompt is shown.
We have just quitted the “database client”. • The next few slides show how we could quit the “database server”.
Focus on the “left” command prompt.This is for the database server. Then, press “<Ctrl> C”.
We “quitted” the “database server” and returned back to the command prompt.
After you see a set of screenshots, you should know how to use MySQL • The remaining issue is how to “edit” and “open” the SQL script file (e.g., “TestQuery.sql”)
You could use any TEXT editor (e.g., notepad) to edit and open the SQL script file. • However, a “normal” editor (e.g., notepad) does not show the “color” for the syntax of SQL.
If you want to have some editors with “colorful” syntax, you could consider the following. • MySQL Workbench (in "MySQL Community Edition")(particularly for writing MySQL statements) • Code Writer (in Windows)(in general for many programming languages including HTML, JavaScript, JSON, C#, VB, C++, ASP, PHP, Perl, Python, Ruby, SQL and MongoDB) • Sublime Text (in Windows, Mac and Linux)(in general for many programming languages including HTML, JavaScript, JSON, C#, C++, ASP, PHP, Perl, Python, Ruby, SQL and MongoDB) Recommendation: Sublime Text
About SQL Statement stored in a File • We know that we could type “source TestQuery.sql” in the MySQL command prompt to execute the SQL statement stored in the file “TestQuery.sql”. • Alternatively, we could open file “TestQuery.sql” in a TEXT editor (e.g., sublime text) and then could “copy-and-paste” the content of this file to the MySQL command prompt to execute the SQL statement (stored in the file)