• 数据库的简单操作


    本文测试使用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)
    
  • 相关阅读:
    Linux-第一天
    Hadoop学习10--常用命令记录帖
    C# asp.net repeater实现排序功能,自动排序,点击头部排序,点击列排序
    图片与字符之间的转换
    兼容浏览器 div固定浏览器窗口底部 浮动div
    解决QQ未启用状态,QQ留言图标未启用
    C#Cookie操作类,删除Cookie,给Cookie赋值
    vs2008bin下Debug bll Release文件 obj下的Debug bll Release文件区别
    asp.net限制了上传文件大小为..M,解决方法
    多文件上传ajax jquery
  • 原文地址:https://www.cnblogs.com/geoffreyone/p/9899753.html
Copyright © 2020-2023  润新知