Connect to MySQL
If you have already installed MySQL on your Ubuntu machine, you can easily connect to the MySQL shell by issuing the following command in your Linux terminal.
[cc lang=”bash” width=”100%” height=”100%” escaped=”true” theme=”blackboard” nowrap=”0″]
$ sudo mysql -u root -p
[/cce_bash]
In the above command:
- -u is the user
- root is the MySQL username
- -p is the password
After running the above command, you will be asked to enter the password. Once you have entered the root user password, you will be logged in to the MySQL shell.
If you have recently installed MySQL, then you will not be asked for the password.
Set or Change Password
To set or change the password for any MySQL database user, issue the following command:
Replace the user_name and new_password with your username and desired password. For example:
View Databases
In MySQL, you can view the list of databases under the user’s granted privileges by running the simple command stated below:
The above command will list all the databases.
Create a Database
You can create a database by issuing the following command in the MySQL shell:
The above statement will not create a database if there already exists a database with the name that has been provided. For example:
As you can see in the screenshot, the db1 database has been created.
Select a Database
You can use any of the listed databases by running the following command in the MySQL shell:
Create a Table
A table can be created by using the CRETE TABLE command and defining the column names and their types in the parentheses, as follows:
col1 INT,
col2 VARCHAR(20),
...
PRIMARY KEY (col1)
);
In this creation query:
- tb1 is the name of the table
- col1, col2 are the names of the columns in the tb1 table
- INT and VARCHAR are the datatypes of the specified columns
- col1 is defined as the primary key
View Tables
To list all the tables in the present database, run the simple SHOW TABLES command in the MySQL shell.
View Table Structure
The structure of the table, including column names, their types, and their default values, can be seen by running the DESC command.
Insert Data
Data can be inserted into the table by providing the column names into which you want to insert data, as well as the data or values, using the following syntax:
VALUES (1, "value1"),
(2, "value2");
View Table Data
The SELECT statement is used to view the data in a table. You can either provide the asterisk symbol (*) to select all the columns, or you can provide the specific columns that you want to view.
mysql> SELECT col2 FROM tb1;
Delete Data from Table
To delete a row from a table, provide the table name to the DELETE FROM command, along with the WHERE conditional clause.
Delete a Table
Deleting a table is as easy as saying it. You can delete any table in the present database by running the statement given below.
Delete a Database
Deleting or dropping a database is the same as deleting a table in MySQL. Simply use the DROP DATABASE command with the database name.