Creation/view of mysql database/data table

Keywords: MySQL Database mysqladmin less

First: Create a database
Method 1:

[root@host]# mysqladmin -u root -p create database1
Enter password:******

mysql> show databases; 
+--------------------+ 
| Database           | 
+--------------------+ 
| information_schema | 
| database1          | 
| mysql              | 
| performance_schema | 
+--------------------+ 
4 rows in set (0.00 sec)

Method 2:

mysql> create database database2;
mysql> show databases; 
+--------------------+ 
| Database           | 
+--------------------+ 
| information_schema | 
| database1          | 
| database2          | 
| mysql              | 
| performance_schema | 
+--------------------+ 
5 rows in set (0.00 sec) 

II. Establishment of data sheets
mysql> use database1;
Database changed
mysql> show tables;
Empty set (0.00 sec)

It's time to decide what database tables you need and what columns are in each database table.
(If later relative data table formats are modified, columns are added, columns are deleted, column names are changed, etc.)
This involves some questions about data types in mysql
http://www.runoob.com/mysql/mysql-data-types.html
Among them, name, owner, species, we use VARCHAR variable length string, size less than 0-65535 bytes, sex uses single-byte fixed length string, birth, death uses DATE date type.

mysql> create table pet (name VARCHAR(20), owner VARCHAR(20), 
    -> species VARCHAR(20), sex CHAR(1), birth DATE, death DATE); 
Query OK, 0 rows affected (0.07 sec) 
mysql> show tables; 
+---------------------+ 
| Tables_in_database1 | 
+---------------------+ 
| pet                 | 
+---------------------+ 
1 row in set (0.00 sec)
mysql> describe pet; / desc pet;
+---------+-------------+------+-----+---------+-------+ 
| Field   | Type        | Null | Key | Default | Extra | 
+---------+-------------+------+-----+---------+-------+ 
| name    | varchar(20) | YES  |     | NULL    |       | 
| owner   | varchar(20) | YES  |     | NULL    |       | 
| species | varchar(20) | YES  |     | NULL    |       | 
| sex     | char(1)     | YES  |     | NULL    |       | 
| birth   | date        | YES  |     | NULL    |       | 
| death   | date        | YES  |     | NULL    |       | 
+---------+-------------+------+-----+---------+-------+ 
6 rows in set (0.00 sec) 

3. Fill in the contents of the data sheet (key points)
It can be completed by LOAD DATA and INSERT statements.
Method 1: load a text directly
(It seems that you can also import csv format files, which need to be studied.)
Basic grammar:

load data [low_priority | concurrent] [local] infile 'filename'
    [REPLACE | IGNORE]
    INTO TABLE tbl_name
    [CHARACTER SET charset_name]
    [{FIELDS | COLUMNS}
        [TERMINATED BY 'string']
        [[OPTIONALLY] ENCLOSED BY 'char']
        [ESCAPED BY 'char']
    ]
    [LINES
        [STARTING BY 'string']
        [TERMINATED BY 'string']
    ]
    [IGNORE number {LINES | ROWS}]
    [(col_name_or_user_var,...)]
    [SET col_name = expr,...]

Among them:
https://dev.mysql.com/doc/refman/5.5/en/load-data.html
(Authoritative explanations for each parameter are in the official documents, which I understand directly. Some of them I haven't used/don't understand will not be said.)
When reading a file located on the server, the file must be in the server directory or have access to the file.
load data is executed when no user reads this file.
Local means to read a file from client host. If local is not specified, the file must be located on server host.
Replace input line content will replace the existing line content.
ignore skips when it encounters a row with a unique key value.
FIELDS | COLUMNS keyword:
terminated by describes the delimiter of the field, which by default is the tab character (\t)
enclosed by describes the enclosed character of a field.
escaped by describes escaped characters. The default is backslash:)
LINES keyword:
The delimiter for each record defaults to'n'as a newline character.
The default format for this version is tab keys for each value, then enter keys for each set of data, and N for items without values (NULL is shown). The following is the result I entered in the txt file:

Fluffy  Harold  cat f   1993-02-04  \N 
Claws   Gwen    cat m   1994-03-17  \N 
Buffy   Harold  dog f   1989-05-13  \N 
Fang    Benny   dog m   1990-08-27  \N 
Bowser  Diane   dog m   1979-08-31  \N 
Chirpy  Gwen    bird    \N  1997-12-09  \N 
Whistler    Gwen    bird    \N  1997-12-09  \N 
Slim    Benny   snake   m   1996-04-29  \N

You can also define the format you feel used to later.
For example, I would like to try to use "" to represent fields with characters, commas to represent field separators, and line breaks to represent separators for each record.

mysql>load data local infile '/path/Documents/pet.txt' into table pet fields terminated by ',' enclosed by ' '' ' lines terminated by '/n'; 

Be careful:
At the same time, there is a problem:

mysql> load data local infile '/path/Documents/pet.txt' into table pet; 
ERROR 1148 (42000): The used command is not allowed with this MySQL version 

It seems to be a security issue:
https://stackoverflow.com/questions/18437689/error-1148-the-used-command-is-not-allowed-with-this-mysql-version
https://stackoverflow.com/questions/16285864/how-can-i-correct-mysql-load-error
Just exit and log in with the following commands

mysql -u root -p --local-infile  **Add--local-infile**

View the import results:

mysql> load data local infile '/path/Documents/pet.txt' into table pet; 
Query OK, 8 rows affected (0.03 sec) 
Records: 8  Deleted: 0  Skipped: 0  Warnings: 0 
mysql> select * from pet; 
+----------+--------+---------+------+------------+-------+ 
| name     | owner  | species | sex  | birth      | death | 
+----------+--------+---------+------+------------+-------+ 
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL  | 
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL  | 
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL  | 
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL  | 
| Bowser   | Diane  | dog     | m    | 1979-08-31 | NULL  | 
| Chirpy   | Gwen   | bird    | NULL | 1997-12-09 | NULL  | 
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL  | 
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL  | 
+----------+--------+---------+------+------------+-------+ 
8 rows in set (0.00 sec) 

Method 2: Use insert statement
The above is a method of importing data in batches, if you want to import a group or a new data at a time. (Of course, load data can also specify importing part of the content.)
The simplest form is to provide the values of each column in the same order as the columns in the CRATE TABLE statement. Assuming Diane names a new hamster Puffball, you can add a new record using the following INSERT statement:

mysql> insert into pet 
    -> values ('Puffball','Diane','hamster','f','1999-03-30',NULL); 
Query OK, 1 row affected (0.09 sec) 

mysql> select * from pet; 
+----------+--------+---------+------+------------+-------+ 
| name     | owner  | species | sex  | birth      | death | 
+----------+--------+---------+------+------------+-------+ 
| Fluffy   | Harold | cat     | f    | 1993-02-04 | NULL  | 
| Claws    | Gwen   | cat     | m    | 1994-03-17 | NULL  | 
| Buffy    | Harold | dog     | f    | 1989-05-13 | NULL  | 
| Fang     | Benny  | dog     | m    | 1990-08-27 | NULL  | 
| Bowser   | Diane  | dog     | m    | 1979-08-31 | NULL  | 
| Chirpy   | Gwen   | bird    | NULL | 1997-12-09 | NULL  | 
| Whistler | Gwen   | bird    | NULL | 1997-12-09 | NULL  | 
| Slim     | Benny  | snake   | m    | 1996-04-29 | NULL  | 
| Puffball | Diane  | hamster | f    | 1999-03-30 | NULL  | 
+----------+--------+---------+------+------------+-------+ 
9 rows in set (0.01 sec) 

Be careful:
Here the string and the date value are quoted strings. In addition, NULL can be inserted directly into the INSERT statement to indicate values that do not exist in the table. The N shown in LOAD DATA cannot be used.

Posted by FatalError on Fri, 14 Jun 2019 12:07:13 -0700