首页 > 数据库技术 > 详细

MySQL 命令操作

时间:2019-04-03 22:17:22      阅读:169      评论:0      收藏:0      [点我收藏+]

Mysql进入与退出

 

进入数据库:mysql -uroot -p

退出: exit

 

 

库级操作语句

 

  1. 显示所有的表:show databases;
  2. 创建库: create database 库名; (重复创建会报错,可在库名前加上[if not exists])
  3. 删除库: drop database 库名;(如果不知道数据库是否存在,记得在库名前加[if exists])
  4. 进入数据库:use 库名;

 

 

表级操作语句

 

  1. 显示所有的表: show tables;
  2. 创建表: create table 表名;(重复创建会报错,可在表名前加上[if not exists])
  3. 显示创建表的信息: show create table 表名;
  4. 删除表:drop table 表名;

 

 

 

 

 

MysSQL表中数据的操作

 

插入数据(create) insert into values

 

  1. 指定字段插入: insert into 表名(表头)value(插入信息);
  2. 全字段插入:  insert into 表名value (插入整段信息);
  3. 多行插入: insert into 表名 value(插入信息),(插入信息),…;

 

查询数据 (read) select from where

 

  1. 指定字段查询: select 表头 from 表名;
  2. 2.      全字段查询:select * from 表名;(select查询关键字,  * 代表所有 )
  3. 带条件查询:select 表头1 from 表名 where 表头2  条件;

例:select name from student where age = 19;

        (查询学生表里年龄等于19的姓名)

                      select * from 表名 where 表头字段 条件;

                      例:select * from student where age = 19;

                             (查询学生表里等于19岁的全部字段)

 

 

 

 

 

修改数据(update)update set where

 

  1. 修改所有数据:update 表名 set 表头字段 = 条件;
  2. 修改多个数据:update 表名 set 表头字段 = 条件, 表头字段2 = 条件2;
  3. 修改满足条件的数据:update 表名 set 表头字段1 = 条件1  where 表头字段2 = 条件2;

例:update student set id = 5 where name =某某;

       (把学生表姓名为某某的id改成5)

 

 

 

 

 

 

 

删除数据 (delete)delete from where

 

  1. 删除表中所有数据: delete from 表头字段;
  2. 删除表中满足条件的数据:delete from 表头字段 where 表头字段 = 条件;

例:delete from student where name = 某某;

        (删除学生表里某某整行值)

MySQL数据类型:

数值类型、字符类型、时间日期类型

 

数值类型常用举例

技术分享图片

 

 

字符类型常见举例

 技术分享图片

 

时间日期类型

 技术分享图片

案例

 技术分享图片

 

 

 

 

 

 

 

MySQL筛选条件

 技术分享图片

 

 

 

In null 例子

select * from student where id is null;

错误例子: select * from student where is = null;(这样是显示不了的,空值以这种方式查询是查不出来的)

in not null 例子

select * from student where id is not null;

 

 

 

 

 

 

与或非

and or not

 

and 例子:select * from student where id =5 and age = 9;

  • or  例子:select * from student where id =5 or age = 9;
  • 技术分享图片

 

 

 

 

 

 

 

 

 

 

 

 

排序(order by)

正序举例:select * from student order by age;

倒序举例:select * from student order by age desc;

 

 

限制(limit)

举例:select * from student limit 0,5;

(MySQL是 左闭右闭)

 技术分享图片

 

去重(distinct)

举例:select distinct * from student;

 技术分享图片

MySQL 命令操作

原文:https://www.cnblogs.com/gd000-/p/10651839.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!