MySQL Quick reference - HaymonEdmur/DockerConfiguration GitHub Wiki

Create a new database

mysql> create database book;
mysql> show databases;

Delete a database

mysql> drop database book;

Use database

mysql> use book;
mysql> status;

Create a table

mysql> CREATE TABLE biography (
    ->     isbn INT(5) UNSIGNED ZEROFILL DEFAULT '00000' NOT NULL,
    ->     title       CHAR(40)                 DEFAULT ''     NOT NULL,
    ->     publisher   CHAR(30)                 DEFAULT ''     NOT NULL,
    ->     PRIMARY KEY(isbn));

List tables in database

mysql> show tables;
+----------------+
| Tables_in_book |
+----------------+
| biography      |
+----------------+
1 row in set (0.00 sec)

mysql> describe biography;
+-----------+--------------------------+------+-----+---------+-------+
| Field     | Type                     | Null | Key | Default | Extra |
+-----------+--------------------------+------+-----+---------+-------+
| isbn      | int(5) unsigned zerofill | NO   | PRI | 00000   |       |
| title     | char(40)                 | NO   |     |         |       |
| publisher | char(30)                 | NO   |     |         |       |
+-----------+--------------------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

Insert row to a table

insert into biography (isbn , title , publisher) values (1, 'Book1', 'Author1');
insert into biography (isbn , title , publisher) values (2, 'Book2', 'Author2');

select * from biography;
select * from biography where isbn = 2;
select publisher, title from biography where isbn in (2,3);
select publisher, title from biography where isbn not in (2,3);

Load a batch of values

mysql> insert into biography values
    -> (4,'Book4','Author4'),
    -> (5,'Book5','Author5'),
    -> (6,'Book6','Author6');

mysql> insert into biography(publisher,isbn,title) values
    -> ('Author7',7,'Book7'),
    -> ('Author8',8,'Book8');