[MySQL] A brief introduction to using MySQL on a Linux terminal

This article is for learning reference only!

MySQL is an open source cross-platform relational database management system (RDBMS). MySQL uses the standard query language (SQL) to communicate with the MySQL software.
Database administrators and developers interact with the RDBMS by running statements, which in database management are called queries. In this article, you will learn how to use MySQL to run queries on the Linux terminal.

How to create a database in the MySQL shell

Linux terminal and MySQLExample of a Linux terminal window

Databases are used to store organized data and information. In a database, related items are stored in tables. These tables, in turn, contain columns that store data entries of a particular type, and each column has a corresponding row.

Armed with this background knowledge, it's time to start your MySQL server. If you don't have MySQL installed, you can use the following command to do so:

$ sudo apt-get install mysql-server
$ sudo mysql_secure_installation # follow the prompts

If you just installed MySQL, your server should already be up. You can check the status of the server with the following command:

$ sudo service mysql status

If you already have MySQL installed, you can start the server with:

$ sudo service mysql start

With the service started, you can now enter an interactive MySQL shell on the terminal:

$ sudo mysql 

To create a database, use the keyword CREATE DATABASE followed by the database name:

> CREATE DATABASE school;

It should be noted that statements in MySQL are not case-sensitive. So the above statement looks like:

> create database school;

Note that the first style - all caps - is preferred due to convention . Therefore, the remainder of this tutorial will use that format.

Note the semicolon ; at the end of the statement . It signifies the completion of the statement. If you do not press the Enter key, the statement will not run until the symbol is encountered.

After the database is created, it's good to know that the database was saved successfully. You can display the currently available databases with:

> show databases;

Next, you need to choose the database you want to interact with. In this case, the school database:

> use school;

Now, you need to create some tables in the database. The syntax for creating a table in a MySQL database is:

CREATE TABLE table_name (column1 datatype1, column2 datatype2, …);

So, to create a student table with columns ** id , fname , lname , and age ,** would look like this:

> create table student(
 -> ID int,
 -> fname varchar(255),
 -> lname varchar(255),
 -> age int);

MySQL supports various data types, which can be referenced on its official page. For our purposes, the ID column is of data type int , representing integer values.

The varchar type represents variable-length strings. It takes the maximum number of expected characters as its argument. For the example above, fname and lname cannot exceed 255 characters. The maximum number of characters that varchar can accept is ((2^16)-1).

Note the indentation in the above MySQL statement. It helps to split the code into a more readable format. As mentioned earlier, the statement will not run until a ; is encountered.

To see the table we created, use the DESCRIBE keyword:

> DESCRIBE student;

The created table is currently missing any entries and has NULL values. To populate a table, use the keyword INSERT . The syntax is as follows:

INSERT INTO table_name(column1,column2,...) VALUES (value1,value2,...);

Here is an example of how to insert values ​​into a table using MySQL:

> INSERT INTO      ->student(ID,fname,lname,age)  ->VALUES(2501,"Jack","Andrews",16);

How to update and delete from MySQL database

Suppose you made a wrong entry and want to modify it. This is where the UPDATE statement comes in. The syntax for making this database query is:

UPDATE table_name
SET column1 = value1, column2 = value2, … columnN = valueN
WHERE condition;

Below is an example showing how to update a database entry using the Update keyword in MySQL :

> UPDATE student 
 -> SET lname ="Anders"
 -> WHERE fname = "Jack";

Now, suppose you want to delete a student's details. To do this, you would use the DELETE FROM statement with the following syntax:

DELETE FROM table_name 
WHERE condition;

> DELETE FROM student 
WHERE fname = "Jack";

It is important to note that a WHERE clause must be included . Not including it will drop the entire table.

You might also wonder what data is stored in the table. Use the SELECT statement with the following syntax to view the data stored in the table:

SELECT column1, column2,.. columnN FROM table_name;

To select all columns, use asterisks ***** or wildcards instead:

SELECT * FROM table_name;

END

There are many database management systems that use SQL, including SQL Server, Oracle, and Postgres. This means that the knowledge you gain here can be transferred (with minimal changes) to other database software. MySQL is one of the most powerful RDBMS software out there, so it's worth spending some time learning it carefully.

Guess you like

Origin blog.csdn.net/m0_47015897/article/details/131417849