Difference between revisions of "mySQL walkthrough"
(Created page with 'SSH to your account, and from the shell, type: mysql -ppassword (no space after -p ) that will get you in the SQL prompt, where the first command should be: mysql> use dataB…') |
(Made more professional) |
||
Line 1: | Line 1: | ||
SSH to your account, and from the shell, type: | SSH to your account, and from the shell, type: | ||
− | mysql - | + | mysql -p username |
− | + | That will get you in the SQL prompt: | |
− | + | ||
− | |||
− | |||
Welcome to the MySQL monitor. Commands end with ; or \g. | Welcome to the MySQL monitor. Commands end with ; or \g. | ||
Your MySQL connection id is 35795 to server version: 3.21.33b | Your MySQL connection id is 35795 to server version: 3.21.33b | ||
Type 'help' for help. | Type 'help' for help. | ||
+ | Then you can issue standard SQL commands. SQL commands are traditionally shown in all uppercase, but in reality they can be lowercase just as well. Table and column names, however, are case-sensitive. | ||
− | + | For example: | |
− | + | ||
− | + | ||
mysql> show tables; | mysql> show tables; | ||
shows a list of all the tables in the database, or none if none. | shows a list of all the tables in the database, or none if none. | ||
− | + | Note that most SQL commands need a ; semicolon at the end. | |
mysql> describe some_table; | mysql> describe some_table; | ||
− | describes a table | + | describes a table. |
− | + | You may not have any tables in the database at this point. Let's create one: | |
− | + | ||
− | mysql>create table my_table ( id INT, name CHAR(64), tel CHAR(16) ); | + | mysql> create table my_table ( id INT, name CHAR(64), tel CHAR(16) ); |
− | + | This means we create a table to store 3 columns: an id for each person, a name for each of them, and a phone number. | |
− | an id for each | + | |
− | + | ||
− | + | Now we have a table structure, however the table is still empty. Let's stuff some data in: | |
− | + | ||
− | mysql>insert into my_table (id, name, tel) values ( 1, "Mr. Tux", "408-282-1540"); | + | mysql> insert into my_table (id, name, tel) values ( 1, "Mr. Tux", "408-282-1540"); |
Query OK, 1 row affected (0.01 sec) | Query OK, 1 row affected (0.01 sec) | ||
− | means the | + | This means that the command you entered went all right. If it wasn't all right, you may read something like: |
− | + | ||
ERROR 2000: parse error near '' at line 1 | ERROR 2000: parse error near '' at line 1 | ||
− | + | If you had a typo, you can recall the last type-in with the upward arrow key, just like in the shell. | |
− | just like in the shell. | + | Let's enter a couple more: |
− | + | ||
mysql> insert into my_table (id, name, tel) values ( 2, "Ms. Tuxinette", "408-282-1540"); | mysql> insert into my_table (id, name, tel) values ( 2, "Ms. Tuxinette", "408-282-1540"); | ||
mysql> insert into my_table (id, name, tel) values ( 3, "Squishy Junior", "123-456-7890"); | mysql> insert into my_table (id, name, tel) values ( 3, "Squishy Junior", "123-456-7890"); | ||
− | + | To see what is in the table: | |
− | + | ||
− | mysql>select id,name,tel from my_table; | + | mysql> select id,name,tel from my_table; |
+------+----------------+--------------+ | +------+----------------+--------------+ | ||
Line 67: | Line 56: | ||
3 rows in set (0.01 sec) | 3 rows in set (0.01 sec) | ||
− | + | We could also have said: | |
mysql>select * from my_table; | mysql>select * from my_table; | ||
gives the same answer, * stands for all the fields (= columns). | gives the same answer, * stands for all the fields (= columns). | ||
− | + | This is the SELECT sql statement, the most used sql command. It retrieves data according to what you ask for. | |
− | + | ||
− | + | As another example, if we want the telephone and name only: | |
mysql> select tel,name from my_table; | mysql> select tel,name from my_table; | ||
Line 89: | Line 77: | ||
− | + | Let's get a bit more picky, we want only those whose name contains "ux": | |
− | mysql>select * from my_table where name | + | mysql> select * from my_table where name like '%ux%'; |
+------+---------------+--------------+ | +------+---------------+--------------+ | ||
Line 111: | Line 99: | ||
2 rows in set (0.00 sec) | 2 rows in set (0.00 sec) | ||
− | mysql> select id from | + | mysql> select id from my_table where name='Squishy Junior'; |
+------+ | +------+ | ||
Line 121: | Line 109: | ||
− | + | Now we have a small table. | |
− | The UPDATE sql command | + | The UPDATE sql command changes a row in a table. You can change the table structure itself with the ALTER TABLE command. |
Latest revision as of 11:13, 6 May 2014
SSH to your account, and from the shell, type:
mysql -p username
That will get you in the SQL prompt:
Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 35795 to server version: 3.21.33b Type 'help' for help.
Then you can issue standard SQL commands. SQL commands are traditionally shown in all uppercase, but in reality they can be lowercase just as well. Table and column names, however, are case-sensitive.
For example:
mysql> show tables;
shows a list of all the tables in the database, or none if none. Note that most SQL commands need a ; semicolon at the end.
mysql> describe some_table;
describes a table. You may not have any tables in the database at this point. Let's create one:
mysql> create table my_table ( id INT, name CHAR(64), tel CHAR(16) );
This means we create a table to store 3 columns: an id for each person, a name for each of them, and a phone number.
Now we have a table structure, however the table is still empty. Let's stuff some data in:
mysql> insert into my_table (id, name, tel) values ( 1, "Mr. Tux", "408-282-1540"); Query OK, 1 row affected (0.01 sec)
This means that the command you entered went all right. If it wasn't all right, you may read something like:
ERROR 2000: parse error near at line 1
If you had a typo, you can recall the last type-in with the upward arrow key, just like in the shell. Let's enter a couple more:
mysql> insert into my_table (id, name, tel) values ( 2, "Ms. Tuxinette", "408-282-1540"); mysql> insert into my_table (id, name, tel) values ( 3, "Squishy Junior", "123-456-7890");
To see what is in the table:
mysql> select id,name,tel from my_table; +------+----------------+--------------+ | id | name | tel | +------+----------------+--------------+ | 1 | Mr. Tux | 408-282-1540 | | 2 | Ms. Tuxinette | 408-282-1540 | | 3 | Squishy Junior | 123-456-7890 | +------+----------------+--------------+ 3 rows in set (0.01 sec)
We could also have said:
mysql>select * from my_table;
gives the same answer, * stands for all the fields (= columns). This is the SELECT sql statement, the most used sql command. It retrieves data according to what you ask for.
As another example, if we want the telephone and name only:
mysql> select tel,name from my_table; +--------------+----------------+ | tel | name | +--------------+----------------+ | 408-282-1540 | Mr. Tux | | 408-282-1540 | Ms. Tuxinette | | 123-456-7890 | Squishy Junior | +--------------+----------------+ 3 rows in set (0.02 sec)
Let's get a bit more picky, we want only those whose name contains "ux":
mysql> select * from my_table where name like '%ux%'; +------+---------------+--------------+ | id | name | tel | +------+---------------+--------------+ | 1 | Mr. Tux | 408-282-1540 | | 2 | Ms. Tuxinette | 408-282-1540 | +------+---------------+--------------+ 2 rows in set (0.03 sec)
mysql> select * from my_table where id > 1; +------+----------------+--------------+ | id | name | tel | +------+----------------+--------------+ | 2 | Ms. Tuxinette | 408-282-1540 | | 3 | Squishy Junior | 123-456-7890 | +------+----------------+--------------+ 2 rows in set (0.00 sec)
mysql> select id from my_table where name='Squishy Junior'; +------+ | id | +------+ | 3 | +------+ 1 row in set (0.01 sec)
Now we have a small table.
The UPDATE sql command changes a row in a table. You can change the table structure itself with the ALTER TABLE command.