数据库的简单操作

本文测试使用manjaro Linux+MariaDB环境

数据库的増删改查可以使用图形化管理工具,如Navicat,

1.数据库的登录

mysql -u用户名 -p密码 [-h主机名/IP地址] [-P=3306] 

 测试结果:

  • 连接本地计算机
➜  geoffrey mysql -uroot -p0 -P3306 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 46
Server version: 10.1.35-MariaDB MariaDB Server
  • 远程连接 
➜  geoffrey mysql -uroot -p123 -P3306 -h192.168.62.33
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.5.54 MySQL Community Server (GPL)

备注:

windows系统下应该把添加到系统变量,或者cd进入上述路径,才能使用上述命令。否则需要使用自带软件MySQL Commend Line Client。

2. 新建、删除数据库

数据库可以创建多个 ,使用命令:

create 数据库名 charset=字符集

MariaDB [(none)]> create database 测试 charset=utf8;
Query OK, 1 row affected (0.00 sec)

删除数据库:

drop 数据库名

MariaDB [(none)]> drop database 测试;
Query OK, 0 rows affected (0.00 sec)

查看数据库:

show databases

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| malajava           |
| mysql              |
| performance_schema |
| test1              |
| 测试               |
+--------------------+
6 rows in set (0.00 sec)

 使用数据库:

use 数据库名

MariaDB [(none)]> use 测试;
Database changed

3. 増删数据表

新建表

MariaDB [测试]> create table 测试表(
    -> id int not null key auto_increment,
    -> name varchar(10) not null,
    -> math int not null,
    -> chinese int not null,
    -> english int not null);
Query OK, 0 rows affected (0.13 sec)

 查看表字段列表

MariaDB [测试]> desc 测试表;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| math    | int(11)     | NO   |     | NULL    |                |
| chinese | int(11)     | NO   |     | NULL    |                |
| english | int(11)     | NO   |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)

插入数据

insert into 表名(字段列表) values(值列表)

MariaDB [测试]> insert into 测试表(name,math,english,chinese) values('Geoffrey',100,200,300);
Query OK, 1 row affected (0.00 sec)

MariaDB [测试]> insert into 测试表(name,math,english,chinese) values('Tom', 456,45,811);
Query OK, 1 row affected (0.02 sec)

MariaDB [测试]> insert into 测试表(name,math,english,chinese) values('Benjamin', 52,654,87);
Query OK, 1 row affected (0.02 sec)

 查看表

select 字段1,字段2.../*  from 表名

MariaDB [测试]> select * from 测试表;
+----+----------+------+---------+---------+
| id | name     | math | chinese | english |
+----+----------+------+---------+---------+
|  1 | Geoffrey |  100 |     300 |     200 |
|  2 | Tom      |  456 |     811 |      45 |
|  3 | Benjamin |   52 |      87 |     654 |
+----+----------+------+---------+---------+
3 rows in set (0.00 sec)

查询表

select 字段1,字段2.../*  from 表名 where 条件

MariaDB [测试]> select id,name from 测试表 where id=2;
+----+------+
| id | name |
+----+------+
|  2 | Tom  |
+----+------+
1 row in set (0.00 sec)

删除表

MariaDB [测试]> drop table t_table;
Query OK, 0 rows affected (0.40 sec)

猜你喜欢

转载自blog.csdn.net/u010359398/article/details/81901576