数据库的操作
================================================================================================
部署数据库
[root@localhost ~]# yum -y install mariadb mariadb-server
[root@localhost ~]# systemctl restart mariadb
查看mariadb状态
[root@localhost ~]# systemctl status mariadb
mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since 三 2020-03-18 19:53:27 CST; 1min 34s ago
Process: 4148 ExecStartPost=/usr/libexec/mariadb-wait-ready $MAINPID (code=exited, status=0/SUCCESS)
Process: 4068 ExecStartPre=/usr/libexec/mariadb-prepare-db-dir %n (code=exited, status=0/SUCCESS)
Main PID: 4147 (mysqld_safe)
Tasks: 20
CGroup: /system.slice/mariadb.service
├─4147 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
└─4309 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64
================================================================================================
修改字符集
[root@localhost ~]# vim /etc/my.cnf //修改字符集utf8的字符集
character_set_server=utf8 //在下面条件一条这个utf8的字符集
[root@localhost ~]# systemctl restart mariadb //这里需要重启mariadb服务
================================================================================================
数据库修改密码
[root@localhost ~]# mysql //首次进去,空密码进入数据库
MariaDB [(none)]> exit //退出数据库
[root@localhost ~]# mysqladmin -uroot password 'pwd@123' //将空密码设置为pwd@123密码
[root@localhost ~]# mysql -p //安全的进入的数据库
Enter password: //输入正确的密码方可进入数据库
================================================================================================
查看数据库的操作命令如下
MariaDB [(none)]> show databases; //查看库信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
MariaDB [(none)]> exit //退出数据库
================================================================================================
切换mysql库,查看表信息:
MariaDB [(none)]> use mysql; //切换到mysql这个库下
MariaDB [mysql]> show tables; //查看mysql这个下有哪些表
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
================================================================================================
创建库:
MariaDB [mysql]> create database mnm; //创建库名为mnm
MariaDB [mysql]> show databases; //查看库名,发现有mnm这个库
+--------------------+
| Database |
+--------------------+
| information_schema |
| mnm |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
================================================================================================
删除库名:
MariaDB [mysql]> drop database mnm; //删除库名为mnm
Query OK, 0 rows affected (0.02 sec)
MariaDB [mysql]> show databases; //查看库名,发现里面没有mnm这个库名
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
================================================================================================
创建库,创建表:
MariaDB [(none)]> create databases ntd; //创建库
MariaDB [(none)]> show databases; //查看库信息
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| ntd |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> use ntd; //进入ntd库
MariaDB [ntd]> create table cntd(学号 char(13),姓名 varchar(20),性别 char(1),手机号 char(13),通信地址 varchar(48)); //创建cntd表
MariaDB [ntd]> desc cntd; //查看表格信息
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 学号 | char(13) | YES | | NULL | |
| 姓名 | varchar(20) | YES | | NULL | |
| 性别 | char(1) | YES | | NULL | |
| 手机号 | char(13) | YES | | NULL | |
| 通信地址 | varchar(48) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
5 rows in set (0.02 sec)
================================================================================================
录入信息
MariaDB [ntd]> insert into cntd values //录入1信息
-> ('NTD2020110001','郭靖','男','1212123','东海桃花岛');
MariaDB [ntd]> insert into cntd values //录入2信息
-> ('NTD2222','黄蓉','女','2332','东海桃花岛');
MariaDB [ntd]> insert into cntd values //录入3信息
-> ('NTD34323','华筝','女','2321231','蒙古大营');
MariaDB [ntd]> select * from cntd; //查看录入的信息
+---------------+--------+--------+-----------+-----------------+
| 学号 | 姓名 | 性别 | 手机号 | 通信地址 |
+---------------+--------+--------+-----------+-----------------+
| NTD2020110001 | 郭靖 | 男 | 1212123 | 东海桃花岛 |
| NTD2222 | 黄蓉 | 女 | 2332 | 东海桃花岛 |
| NTD34323 | 华筝 | 女 | 2321231 | 蒙古大营 |
+---------------+--------+--------+-----------+-----------------+
3 rows in set (0.00 sec)
================================================================================================
删除表中的数据
MariaDB [ntd]> delete from cntd;
Query OK, 3 rows affected (0.01 sec)
查看表中的信息,发现表中的信息没有了
MariaDB [ntd]> select * from cntd;
Empty set (0.00 sec)
表中的内容没有了,但是表还在,查看一下
MariaDB [ntd]> desc cntd;
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 学号 | char(13) | YES | | NULL | |
| 姓名 | varchar(20) | YES | | NULL | |
| 性别 | char(1) | YES | | NULL | |
| 手机号 | char(13) | YES | | NULL | |
| 通信地址 | varchar(48) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
================================================================================================
将整个表删除
MariaDB [ntd]> drop table cntd;
Query OK, 0 rows affected (0.01 sec)
MariaDB [ntd]> desc cntd; //查看表,发现表中的信息没有了
ERROR 1146 (42S02): Table 'ntd.cntd' doesn't exist
================================================================================================
重新建立cntd表,并录入数据记录
MariaDB [ntd]> create table cntd(学号 char(13),姓名 varchar(20),性别 char(1),手机号 char(13),通信地址 varchar(48)); //建立cntd表
MariaDB [ntd]> desc cntd; //查看cntd表信息
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 学号 | char(13) | YES | | NULL | |
| 姓名 | varchar(20) | YES | | NULL | |
| 性别 | char(1) | YES | | NULL | |
| 手机号 | char(13) | YES | | NULL | |
| 通信地址 | varchar(48) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
录入1条信息
MariaDB [ntd]> insert into cntd values
-> ('NTD2020110001','郭靖','男','1313423123','东海桃花岛'),
-> ('NTD21231','黄蓉','女','231233311','东海桃花岛');
录入2条信息
MariaDB [ntd]> insert into cntd values
-> ('NTD23231','华筝','女','231231','蒙古大营'),
-> ('NTD23221','洪七公','男','2141231','太湖北丐帮总舵');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
录入3条信息
MariaDB [ntd]> insert into cntd values
-> ('NTD323221','欧阳锋','男','212323123','西域白驼山庄');
MariaDB [ntd]> select * from cntd; //查看录入的信息
+---------------+-----------+--------+------------+-----------------------+
| 学号 | 姓名 | 性别 | 手机号 | 通信地址 |
+---------------+-----------+--------+------------+-----------------------+
| NTD2020110001 | 郭靖 | 男 | 1313423123 | 东海桃花岛 |
| NTD21231 | 黄蓉 | 女 | 231233311 | 东海桃花岛 |
| NTD23231 | 华筝 | 女 | 231231 | 蒙古大营 |
| NTD23221 | 洪七公 | 男 | 2141231 | 太湖北丐帮总舵 |
| NTD323221 | 欧阳锋 | 男 | 212323123 | 西域白驼山庄 |
+---------------+-----------+--------+------------+-----------------------+
================================================================================================
复制的操作如下:
MariaDB [ntd]> create table sntd like cntd; //从cntd表复制到sntd表
Query OK, 0 rows affected (0.02 sec)
检查表信息:
MariaDB [ntd]> select * from sntd; //检查表数据
Empty set (0.00 sec)
MariaDB [ntd]> desc sntd; //查看sntd表信息
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 学号 | char(13) | YES | | NULL | |
| 姓名 | varchar(20) | YES | | NULL | |
| 性别 | char(1) | YES | | NULL | |
| 手机号 | char(13) | YES | | NULL | |
| 通信地址 | varchar(48) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec)
MariaDB [ntd]> desc cntd; //查看cntd表信息
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| 学号 | char(13) | YES | | NULL | |
| 姓名 | varchar(20) | YES | | NULL | |
| 性别 | char(1) | YES | | NULL | |
| 手机号 | char(13) | YES | | NULL | |
| 通信地址 | varchar(48) | YES | | NULL | |
+--------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
================================================================================================
字符型字段应用:
创建zntd表
MariaDB [ntd]> create table zntd(id char(4),name varchar(4)); //创建zntd表
Query OK, 0 rows affected (0.03 sec)
插入表数据1条记录
MariaDB [ntd]> insert into zntd values('0001','张无忌');
插入表数据2条记录
MariaDB [ntd]> insert into zntd values('0002','柳如花似玉');
插入表数据3条记录
MariaDB [ntd]> insert into zntd values('0003','南宫啸天');
查看zntd数据内容
MariaDB [ntd]> select * from zntd;
+------+--------------+
| id | name |
+------+--------------+
| 0001 | 张无忌 |
| 0002 | 柳如花似 |
| 0003 | 南宫啸天 |
+------+--------------+
3 rows in set (0.00 sec)
================================================================================================
数值型字段应用:
创建工资表
MariaDB [ntd]> create table gongzi(工号 int,本月实发 float(8,2)); //创建工资表
录入工资表数据
MariaDB [ntd]> insert into gongzi values(1001,48750.85);
录入工资表数据
MariaDB [ntd]> insert into gongzi values(1002,36500);
录入工资表数据
MariaDB [ntd]> insert into gongzi values(1005,28850.6070);
MariaDB [ntd]> select * from gongzi; //查看gongzi表信息
+--------+--------------+
| 工号 | 本月实发 |
+--------+--------------+
| 1001 | 48750.85 |
| 1002 | 36500.00 |
| 1005 | 28850.61 |
+--------+--------------+
3 rows in set (0.00 sec)
================================================================================================