zoukankan      html  css  js  c++  java
  • mysql 操作sql语句 操作数据表

    #2. 操作文件
        先切换到文件夹下:use db1

    查看当前所在的数据库
    mysql> select database();
    +------------+
    | database() |
    +------------+
    | db1        |
    +------------+
    1 row in set (0.00 sec)
    每创建一个表每个字段都有相应类型
    int 整数类型
    char 字符类型 增:create table t1(id int,name char);

    创建的数据库都在 在 /var/lib/mysql目录下


    [root@mysql mysql]# cat /etc/my.cnf 
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    default-character-set=utf8
    
    [client]
    default-character-set=utf8
    
    [mysql]
    default-character-set=utf8
    
    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid-file=/var/run/mysqld/mysqld.pid

    [root@mysql mysql]# cd /var/lib/mysql [root@mysql mysql]# ll 总用量 20492 drwx------. 2 mysql mysql 4096 10月 6 03:55 db1 -rw-rw----. 1 mysql mysql 10485760 10月 6 03:49 ibdata1 -rw-rw----. 1 mysql mysql 5242880 10月 6 03:49 ib_logfile0 -rw-rw----. 1 mysql mysql 5242880 10月 6 02:16 ib_logfile1 drwx------. 2 mysql mysql 4096 10月 6 02:16 mysql srwxrwxrwx. 1 mysql mysql 0 10月 6 03:47 mysql.sock drwx------. 2 mysql mysql 4096 10月 6 02:15 test

    db1 数据库下面有 新创建的 t1 数据表

    [root@mysql mysql]# cd db1/
    
    
    
    [root@mysql db1]# ll
    总用量 20
    -rw-rw----. 1 mysql mysql   61 10月  6 03:50 db.opt
    -rw-rw----. 1 mysql mysql 8586 10月  6 03:55 t1.frm
    -rw-rw----. 1 mysql mysql    0 10月  6 03:55 t1.MYD
    -rw-rw----. 1 mysql mysql 1024 10月  6 03:55 t1.MYI

     查看当前数据库所有表

       查:show tables

    查表结构 根据数据表名字
    show create table t1;
    mysql> show create table t1;
    +-------+---------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                        |
    +-------+---------------------------------------------------------------------------------------------------------------------+
    | t1    | CREATE TABLE `t1` (
      `id` int(11) DEFAULT NULL,
      `name` char(1) DEFAULT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
    +-------+---------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    
    
    
    
    modify 修改意思 只能改字段类型  字段名不能改
    改哪个字段

    alter table 表名 modify 要改的字段 数据类型 和长度
    
            改:alter table t1 modify name char(3);

    mysql> alter table t1 modify name char(3);
    Query OK, 0 rows affected (0.05 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> show create table t1;
    +-------+---------------------------------------------------------------------------------------------------------------------+
    | Table | Create Table                                                                                                        |
    +-------+---------------------------------------------------------------------------------------------------------------------+
    | t1    | CREATE TABLE `t1` (
      `id` int(11) DEFAULT NULL,
      `name` char(3) DEFAULT NULL
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
    +-------+---------------------------------------------------------------------------------------------------------------------+
    1 row in set (0.00 sec)
    
    

    另外一种方式查表 查看表结构

    desc 表名;

    desc t1;
    mysql> desc t1;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | YES  |     | NULL    |       |
    | name  | char(3) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    change 可以改字段名

    alter table 表名 change 要改的字段 改后的字段 数据类型 和长度
    alter table t1 change name name1 char(2); 删:drop table t1;
     
    mysql> alter table t1 change name NAME char(2);
    Query OK, 0 rows affected (0.02 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> 
    mysql> desc t1;
    +-------+---------+------+-----+---------+-------+
    | Field | Type    | Null | Key | Default | Extra |
    +-------+---------+------+-----+---------+-------+
    | id    | int(11) | YES  |     | NULL    |       |
    | NAME  | char(2) | YES  |     | NULL    |       |
    +-------+---------+------+-----+---------+-------+
    2 rows in set (0.00 sec)
    


    mysql> show create table t1; +-------+---------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+---------------------------------------------------------------------------------------------------------------------+ | t1 | CREATE TABLE `t1` ( `id` int(11) DEFAULT NULL, `NAME` char(2) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+---------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
    
    
    
    
    
    删:drop table t1;


     
  • 相关阅读:
    移动端touch与click区别--移动端开发整理笔记(五)
    移动端适配(rem & viewport)--移动端开发整理笔记(四)
    移动端事件(touchstart、touchmove、touchend)--移动端开发整理笔记(三)
    Flex弹性盒模型(新老版本完整)--移动端开发整理笔记(二)
    meta设置与去除默认样式--移动端开发整理笔记(一)
    react native ios 上架
    react16 路由按需加载、路由权限配置
    mpvue 页面预加载,新增preLoad生命周期
    mpvue 星星打分组件
    mpvue 签字组件
  • 原文地址:https://www.cnblogs.com/mingerlcm/p/9745721.html
Copyright © 2011-2022 走看看