zoukankan      html  css  js  c++  java
  • MYSQL的一些基本操作

    #原创,转载请联系

    一、登录以及退出MYSQL

    • 登录:mysql -uroot -p
    • 退出:exit 或者 quit
    • 查看数据库版本:select version();
    • 显示当前的日期时间:select now();
    • 显示当前的年份 select year(now());
    • 显示当前的时间 select time(now());

    二、数据库操作

    • 查看当前在哪个数据库:select database();
    • 查看所有数据库:show databases;
    • 使用数据库:use 数据库名;
    • 创建数据库:create database 数据库名 charset=utf8;    (charset=utf8一定要加,不然表里用到中文可能会乱码,后续更改很麻烦)
    • 删除数据库:drop database 数据库名;

    三、数据表结构的操作

    • 查看当前数据库的所有表:show tables;
    • 删除表:drop table 表名;
    • 查看表结构:desc 表名;
    • 创建表结构:create table 表名(字段名称  数据类型 约束条件,*其他的格式和前面的一样)

    注意:主键说明可以放在字段中单独说明 也可以放在最后统一说明PRIMARY KEY(字段名称)

    示例:创建一个学生表

    create table student_tables(id int unsigned auto_increment not null primary key,name varchar(10) default'',age tinyint unsigned default 0,height decimal(5,2),gender enum('man','woman'),cls_id int unsigned default 0);

    也可以在最后统一说明PRIMARY KEY(字段名称)

    create table student_tables(id int unsigned auto_increment not null,name varchar(10) default'',age tinyint unsigned default 0,height decimal(5,2),gender enum('man','woman'),cls_id int unsigned default 0,primary key(id));
    • 修改表-添加字段:alter table 表名 add (字段名称 数据类型 约束条件,*其他的格式和前面的一样);
    alter table student_tables add (birthday datetime not null,app int not null);
    • 修改表-修改字段:alter table 表名 change 原字段名 新字段名 数据类型 约束条件(要改字段名就要用这个,数据类型改不改都行。)
    alter table student_tables change birthday birth datetime not null;
    • 修改表-修改字段:alter table 表名 modify 字段名 数据类型 约束条件(不需要改字段名,但是要改数据类型,约束条件用这个)
    alter table student_tables modify birth date not null;
    • 修改表-删除字段
    alter table student_tables drop app;
    • 查看表的创建语句-详细过程:show create table 表名;
    mysql> show create table student_tables;
    +----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | Table          | Create Table                                                                                                                                                                                                                                                                                                                                                                            |
    +----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
    | student_tables | CREATE TABLE `student_tables` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `name` varchar(10) DEFAULT '',
      `age` tinyint(3) unsigned DEFAULT '0',
      `height` decimal(5,2) DEFAULT NULL,
      `gender` enum('man','woman') DEFAULT NULL,
      `cls_id` int(10) unsigned DEFAULT '0',
      `birth` date DEFAULT '1997-12-11',
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
    +----------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

    拓展:InnoDB,MyISAM 数据库引擎。Mysql 创建表时,默认是InnoDB引擎


    两种类型有什么区别:

    MyISAM类型不支持事务处理等高级处理,而InnoDB类型支持。
    MyISAM类型的表强调的是性能,其执行速度比InnoDB类型更快,但是不提供事务等高级特性,而InnoDB提供事务支持,行级锁,高并发。一般开发中默认使用的是innodb引擎。

    四、表数据的操作

    • 查询所有列:select * from 表名;
    • 查询指定列:select 列1 列2 from 表名;
    select id name from student_tables;
    • 全列插入:insert into 表名 values(...)

    说明:如果主键字段是自动增长,在全列插入时需要占位,通常使用空值(0或者null) ; 字段默认值 default 来占位,插入成功后以实际数据为准。

    mysql> insert into student_tables values (null,'chichung',23,178.00,'man',2,default);
    mysql> select * from student_tables;
    +----+----------+------+--------+--------+--------+------------+
    | id | name     | age  | height | gender | cls_id | birth      |
    +----+----------+------+--------+--------+--------+------------+
    |  1 | chichung |   23 | 178.00 | man    |      2 | 1997-12-11 |
    +----+----------+------+--------+--------+--------+------------+
    • 部分值插入:insert into 表名 (列1,...) values(值1,...)
    mysql>insert into student_tables (name,birth) values ('vivian','1996-10-11');
    mysql> select * from student_tables;
    +----+----------+------+--------+--------+--------+------------+
    | id | name     | age  | height | gender | cls_id | birth      |
    +----+----------+------+--------+--------+--------+------------+
    |  1 | chichung |   23 | 178.00 | man    |      2 | 1997-12-11 |
    |  2 | vivian   |    0 |   NULL | NULL   |      0 | 1996-10-11 |
    +----+----------+------+--------+--------+--------+------------+
    • 全列多行插入:insert into 表名 values(...),(...),...

    一次性插入多行数据,这样可以减少与数据库的通信

    • 部分列多行插入:insert into 表名(列1,...) values(值1,...),(值1,...)...;

    原理同上

    • 修改表数据:update 表名 set 列1=值1,列2=值2... where 条件
    update student_tables set name='cong',height=177.55 where id=1;

    不加where [条件]就是修改全部列都修改!

    • 物理删除:delete from 表名 where 条件
    • 逻辑删除:本质是修改操作。(举个栗子,用户虽然注销了账号,但是我们可以留下历史记录。数据多宝贵啊。(奸笑.jpg))
    update students_tables set isdelete=1 where id=1;

     

  • 相关阅读:
    Java类加载器总结
    Java程序编译和运行的过程
    Spring+Struts2+Hibernate整合
    Spring+MyBatis+SpringMVC整合
    声明式事务管理
    Scala sbt 添加国内镜像
    持续天数算法
    idea run shell set user name
    java insert mysql 中文乱码
    Samba服务器 安装
  • 原文地址:https://www.cnblogs.com/chichung/p/9571004.html
Copyright © 2011-2022 走看看