好久没碰过MySQL命令了,之前学过的该忘的都忘的差不多了,今天开MySQL第一天,顺便整理一下,等下次再用到的时候省着一个个查了。
数据库的操作
- 创建数据库:create database 库名;
- 删除数据库:drop database 库名;
- 使用数据库:use 库名;
- 查看所有数据库:show databases;
表的操作
- 查看表:show tables;
- 查看表结构:desc 表名;
- 创建表:create table 表名(列名 数据类型); 例如:create table test(id int,name varchar(20));
- 删除表:drop table 表名;
- 往表里查数据:
- insert into 表名 values(); 这种要求列全部插入,且一一对应
- insert into 表名(属性列)values (值); 这种可以插入指定列的数据
- 查询表中的数据:
- select * from 表名; 查询全部信息
- select 属性列 from 表名; 查询指定的信息
基本的语句操作
一、模糊匹配:like
like 'a':值为a
like '%a':以a结尾
like 'a%':以a开头
like '%a%':包含a
like '__a':第三个单词是a (_表示一个占位)
二、没有、为空:is null
三、不为空:is not null;!=
四、不同 distinct
select distinct name from student;
五、not 对条件取反
查询不以a结尾的学生姓名
select sname from student not sname like '%a';
六、in
查询分数为100,99,90的学生学号
select sno from student where score in (100,99,90);
七、between…and
查询成绩在80到90之间的学生
select * from student where score between 80 and 90;