Mysql进入与退出
进入数据库:mysql -uroot -p
退出: exit
库级操作语句
显示所有的表:show databases;
创建库: create database 库名; (重复创建会报错,可在库名前加上[if not exists])
删除库: drop database 库名;(如果不知道数据库是否存在,记得在库名前加[if exists])
进入数据库:use 库名;
表级操作语句
显示所有的表: show tables;
创建表: create table 表名;(重复创建会报错,可在表名前加上[if not exists])
显示创建表的信息: show create table 表名;
删除表:drop table 表名;
MysSQL表中数据的操作
插入数据(create) insert into values
指定字段插入: insert into 表名(表头)value(插入信息);
全字段插入: insert into 表名value (插入整段信息);
多行插入: insert into 表名 value(插入信息),(插入信息),…;
查询数据 (read) select from where
指定字段查询: select 表头 from 表名;
2.全字段查询:select * from 表名;(select查询关键字, * 代表所有 )
带条件查询: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
修改所有数据:update 表名 set 表头字段 = 条件;
修改多个数据:update 表名 set 表头字段 = 条件, 表头字段2 = 条件2;
修改满足条件的数据:update 表名 set 表头字段1 = 条件1 where 表头字段2 = 条件2;
例:update student set id = 5 where name =某某;
(把学生表姓名为某某的id改成5)
删除数据 (delete)delete from where
删除表中所有数据: delete from 表头字段;
删除表中满足条件的数据: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 andage = 9;
or 例子:select * from student where id =5 orage = 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;
原文:https://www.cnblogs.com/gd000-/p/10651839.html