2 - Table Structure | MySQL Key Value

table management

The process of the client storing data on the database server

  • The first step is to connect to the database
  • The second step is to create a library to store data (directory where tables are stored)
  • The third step is to create a table to store data (a table is a file that stores data)
  • Step 4: Insert table records (add rows to the file)
  • Step 5 Disconnect

1. Library operations

The basic command to create a database is create database. Library name (must be unique!!!)

[root@host50 ~]# mysql -uroot -pxxxx  //数据库管理员登陆
mysql> select user(); # 查看登陆的用户
+----------------+
| user()         |
+----------------+
| root@localhost |  用户@客户端地址 (在本机管理员登录)
+----------------+
1 row in set (0.00 sec)

mysql> show databases; # 查看已有的库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| tarena             |
+--------------------+
5 rows in set (0.00 sec)

mysql> select database(); # 查看当前所在的库
+------------+
| database() |
+------------+
| NULL       |  表示没有在任何一个库,此时所在的位置是/var/lib/mysql 目录
+------------+
1 row in set (0.00 sec)

mysql> create database db1 # 创建新库
Query OK, 1 row affected (0.00 sec)

mysql> show databases;  此时查看已有库
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |      刚创建的db1库 , 在数据库目录/var/lib/myql 下会有对应名称的db1目录
| mysql              |
| performance_schema |
| sys                |
| tarena             |
+--------------------+
6 rows in set (0.00 sec)

mysql> system ls -ld /var/lib/mysql/db1; # 使用system 命令 在登录状态下执行系统命令
drwxr-x--- 2 mysql mysql 20 97 13:52 /var/lib/mysql/db1

mysql>  create database if not exists  DB1; 加if not exists语句避免重名报错 
Query OK, 1 row affected, 1 warning (0.01 sec)

// 删除库 如果库里有表会一并被删除
mysql> drop database db1 ; 删除没有的库,会报错
mysql> drop database  if exists db1;  加if  exists 语句 避免报错

2. Table operations

Table creation command format
create table database name. table name (
header name data structure [constraints],
header name data structure [constraints]
);

Table creation and deletion

Use table creation paradigm to measure whether table creation is reasonable

  • 1NF The data under the header cannot be split anymore
  • 2NF only stores one type of data information in one table, and cannot store multiple types of data information in one table.
  • The data in the header of a 3NF table cannot depend on the data in other headers.
mysql> create table DB1.t1(姓名 char(10),班级 char(7)); # 在DB1库里创建t1表

mysql> desc DB1.t1; 查看表头信息
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| 姓名   | char(10) | YES  |     | NULL    |       |
| 班级   | char(7)  | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

mysql> drop table DB1.t1;  删除创建的表
Query OK, 0 rows affected (0.11 sec)

为了便于操作通常使用英文定义表头名和表名
mysql> create table DB1.t1 ( name  char(15) , class char(10));
Query OK, 0 rows affected (0.25 sec)
mysql> desc DB1.t1; 查看表头
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| name  | char(15) | YES  |     | NULL    |       |
| class | char(10) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

Table modification

If you find that the table is not properly built, you can modify the table.

  • Add new header add
  • Delete header drop
  • Modify the data type of data stored in the header modify
  • Modify the header name change
  • Modify the table name rename
    alter table database name. table name operation command
mysql> alter table  DB1.t1 add school  char(20) ; 添加1个新字段school
mysql> desc DB1.t1;  默认新添加的字段追加在已有列的末尾
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| name   | char(15) | YES  |     | NULL    |       |
| class  | char(10) | YES  |     | NULL    |       |
| school | char(20) | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

一起添加2个字段 分别是 mail 和  address 
mysql> alter table  DB1.t1 add mail char(50) , add address  char(80) ;
Query OK, 0 rows affected (0.37 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc DB1.t1;  查看新添加的表头,都在追加在末尾
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| name    | char(15) | YES  |     | NULL    |       |
| class   | char(10) | YES  |     | NULL    |       |
| school  | char(20) | YES  |     | NULL    |       |
| mail    | char(50) | YES  |     | NULL    |       |
| address | char(80) | YES  |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
5 rows in set (0.00 sec)

添加新表头number 在第1列的位置
mysql> alter table  DB1.t1 add number int first ;
Query OK, 0 rows affected (0.32 sec)
Records: 0  Duplicates: 0  Warnings: 0
添加新表头在city 放在name的后边
mysql> alter table DB1.t1 add  city  char(10) after name;
Query OK, 0 rows affected (0.42 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc DB1.t1;
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| number  | int(11)  | YES  |     | NULL    |       |
| name    | char(15) | YES  |     | NULL    |       |
| city    | char(10) | YES  |     | NULL    |       |
| class   | char(10) | YES  |     | NULL    |       |
| school  | char(20) | YES  |     | NULL    |       |
| mail    | char(50) | YES  |     | NULL    |       |
| address | char(80) | YES  |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
7 rows in set (0.00 sec)

mysql> alter table DB1.t1 drop city;  一次删除一个表头
Query OK, 0 rows affected (0.31 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table DB1.t1 drop class , drop school; 一起删除多个表头
Query OK, 0 rows affected (0.32 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc DB1.t1;  查看表头
+---------+----------+------+-----+---------+-------+
| Field   | Type     | Null | Key | Default | Extra |
+---------+----------+------+-----+---------+-------+
| number  | int(11)  | YES  |     | NULL    |       |
| name    | char(15) | YES  |     | NULL    |       |
| mail    | char(50) | YES  |     | NULL    |       |
| address | char(80) | YES  |     | NULL    |       |
+---------+----------+------+-----+---------+-------+
4 rows in set (0.00 sec)

// 修改表头信息
mysql> alter table DB1.t1 modify number tinyint after name; 修改类型和位置
mysql> desc DB1.t1;  查看修改,把类型修改为tinyint  并移动name表头的后边
+---------+------------+------+-----+---------+-------+
| Field   | Type       | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| name    | char(15)   | YES  |     | NULL    |       |
| number  | tinyint(4) | YES  |     | NULL    |       |
| mail    | char(50)   | YES  |     | NULL    |       |
| address | char(80)   | YES  |     | NULL    |       |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table DB1.t1 change address homedir char(80); 只修改表头名,类型原样抄下来
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc DB1.t1;查看修改,address 修改为了 homedir
+---------+------------+------+-----+---------+-------+
| Field   | Type       | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| name    | char(15)   | YES  |     | NULL    |       |
| number  | tinyint(4) | YES  |     | NULL    |       |
| mail    | char(50)   | YES  |     | NULL    |       |
| homedir | char(80)   | YES  |     | NULL    |       |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table DB1.t1 change email vachar(60); 表头名和类型一起修改
Query OK, 0 rows affected (0.44 sec)
Records: 0  Duplicates: 0  Warnings: 0
mysql> desc DB1.t1;查看修改表头名类型都变了
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| name    | char(15)    | YES  |     | NULL    |       |
| number  | tinyint(4)  | YES  |     | NULL    |       |
| email   | varchar(60) | YES  |     | NULL    |       |
| homedir | char(80)    | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> alter table DB1.t1 rename stuinfo; 修改表名为 stuinfo
mysql> show tables; 查看表名 已经改变
+---------------+
| Tables_in_DB1 |
+---------------+
| stuinfo       |
+---------------+

Copy table

  • Copy table structure only
mysql> create table DB1.t2 select * from tarena.user where 1=2 ; 复制表结构及数据
  • Copy table structure and data
mysql> create table DB1.t3 select * from tarena.user 
  • Copy table structure only
create table 库.表名 like 库.表名
# 原来的key也会复制给新表 复制不了数据

3. Management table records

Management operations for rows stored in the table include

  • Query: View rows in table select
  • Insert: Add new rows to the table insert into
添加1行给所有表头赋值
mysql> insert into DB1.stuinfo  values ("yaya",1,"[email protected]","beijing");
Query OK, 1 row affected (0.03 sec)

添加1行 ,给指定的表头赋值,
mysql> insert into DB1.stuinfo (name,homedir)values("nb","[email protected]");
Query OK, 1 row affected (0.04 sec)

没有赋值的表头number和email 没有数据 ,值是 null (空)
mysql> select  * from DB1.stuinfo;
+------+--------+--------------+-------------+
| name | number | email        | homedir     |
+------+--------+--------------+-------------+
| yaya |      1 | [email protected] | beijing     |
| plj  |      8 | [email protected]  | shanghai    |
| jing |      9 | [email protected] | beijing     |
| nb   |   NULL | NULL         | [email protected]  |
| nb2  |   NULL | NULL         | [email protected] |
| nb3  |   NULL | NULL         | [email protected] |
+------+--------+--------------+-------------+
6 rows in set (0.00 sec)

使用set语句插入记录 
mysql> insert into  DB1.stuinfo set name="plj" , number=10;
  • Update: Modify the data of a column in a row update
mysql> update DB1.stuniof set email="[email protected]" where email is null;

不加条件批量修改
mysql> update DB1.stuinfo set homedir="beijing";
  • Delete: Delete rows in the table delete
// 只删除与条件符合的行 
delete from DB1.stuinfo where name regexp '^nb';

//删除表里的所有行
delete from 库.表

也可用truncate table 库.表;
自增长列 truncate后从1开始 delete继续编号
truncate不能回滚 delete可以
效率略高于delete 

type of data

Including numeric types, character types, date and time types, enumeration types

Numeric type

  • integer type
    Insert image description here
  • floating point type
    Insert image description here

Character type (Chinese characters or English letters)

  • char fixed length (fixed length) 1-255 bytes
  • varchar variable length (unfixed length) 1-65535
    Insert image description here
    Insert image description here
    Insert image description here
    By default, it is not allowed to store Chinese in the header. To store Chinese, you must specify the character set to be used.
mysql> show create table db1.t3  # 查看字符集
ENGINE=InnoDB # 定义存储引擎
DEFAULT CHARSET =latin1 # 定义字符集

# 建表的时候指定使用的字符集
create table db1.t4(姓名 char(3),地址 varchar(10)) DEFAULT CHARSET utf8;

The date and time type header stores data in date and time format.

Insert image description here

mysql> create table db1.t6(
姓名 char(4),
生日 date,
上课时间 time,
家庭聚会 datetime,
聚会地点 varchar(10)
) DEFAULT CHARSET utf8;

mysql> alter table db1.t6 add 出生年份 year after 姓名;

Insert image description here
The difference between datetime and datestamp

  • The first storage range is different (datetime storage range is large and timestamp range is small)
  • The way of storing data is different. When the timestamp field value is not given, it is automatically assigned the current system time, and the datetime value is NULL.

enumeration type

When assigning a value to the header, the value must be selected within the range specified by the type.

  • Single selection enum (value 1, value 2, value 3...)
  • Multi-select set (value 1, value 2, value 3...)
create table db1.t8(姓名 char(10) , 性别  enum("男","女","保密") ,
爱好 set("帅哥","金钱");

Data batch processing

Store a lot of data in the table at one time or take out all the data in the table at one time
. Note: the file for data import or export must be in the directory required by the mysql service, called the retrieval directory.

To know the default search directory of the database,
you will modify the default search directory of the database.
Check the default search directory.

mysql > show variables like "secure_file_priv"

Insert image description here
Modify the default search directory

vim /etc/my.cnf
[mysql]
secure_file_priv=/myload # 手动添加
mkdir /myload
chown mysql /myload/ # 给权限
  • Data import (the content of the file must be regular)
    • Import the /etc/passwd file into the t3 table of the db1 library
    • File import format
    • mysql> load data infile "/retrieval directory/file name" into table library name. table name fileds terminated by "spacing symbol of column in file" lines terminated by "/n"
    • Data import steps:
        1. Create a library to store data
        1. Create a table based on the contents of the imported file
        1. Copy system files to the search directory
        1. Database management executes commands to import data
        1. View data
  • Data export does not include the table header name. Only the rows in the table are exported. The exported file name does not need to be created in advance and must be unique.
      1. select field name list from library.table where condition into outfile "/retrieval directory name/file name"
      1. select field name list from library.table where condition into outfile "/retrieval directory name/file name" fields terminated by "symbol"
      • fields terminated by specifies the interval symbol for exported columns in the file. More than the default width of a tab
      1. select field name list from library.table where condition into outfile "/retrieval directory name/file name" fields terminated by "symbol" lines terminated by "symbol"
      • lines terminated by specifies the spacing symbol for exported lines in the file. By default, one record is one row

Guess you like

Origin blog.csdn.net/shengweiit/article/details/135261495