Mysql

From Hackepedia
Jump to navigationJump to search

To login to mysql from the shell as user root of mysql:

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.

If you don't yet have a mysql root user password:

mysql> SET PASSWORD FOR root@localhost=PASSWORD('SeCreT');

What databases exist:

mysql> show databases;

To create a database:

mysql> create database bsdcan;    
Query OK, 1 row affected (0.00 sec)

Create a user to with a grant of "all" where the username will be bsdadmin and the client machine is localhost:

mysql> grant all on bsdcan.* to 'bsdadmin'@'localhost';
Query OK, 0 rows affected (0.09 sec)

Now let's use our new database:

mysql> use bsdcan;

and see what tables exist:

mysql> show tables;

Create a basic table:

 mysql> CREATE TABLE volunteers (
 firstname VARCHAR(20),
 lastname VARCHAR(20),
 callsign VARCHAR(6),
 shirtsize CHAR(2),
 years CHAR(2),
 location VARCHAR(20)
 );
Query OK, 0 rows affected (0.01 sec)

Let's take a look at our new table:

mysql> describe volunteers;

Now to load some data:

mysql> INSERT INTO volunteers VALUES ('Diane','Bruce','va3db','l','2','room 412'); 

Edit the data, as ve3sql is now an extra large t-shirt:

mysql> update volunteers set shirtsize='xl' where callsign='ve3sql';

what do we have in the table now?

mysql> SELECT * FROM volunteers;

We accidently added a record with a volunteer we want to delete:

mysql> delete from volunteers where callsign='ve3sql';