最全的MySQL数据库的常用指令

数据库   发布日期:2023年05月28日   浏览次数:255

数据库的创建

查询所有数据库:show databases;

创建数据库:create database <数据库名>;

删除数据库:drop database <数据库名>;

进入数据库:use <数据库名>;

数据表的操作

1)查询数据库下表:show tables;

2)创建表:create table student(id int(4) primary key,name char(20));

注释: id为表的第一列;

int数字类型;

primary key主键的意思,列不能重复。

Name为表的第二列名字。

char:类型;

创建表:create table score(id int(4) not null,class int(2));

注释: not null字段不能为空。

创建表:create table student1(id int(4) not null,name char(20));

Field (列名),Type(字段类型),null(是否为空),key(主键)

3)查看表结构:describe student; 或 desc student;

4)修改表名:alter table <表名> rename <表名>;

5)删除表:drop table <表名>;

6)修改表字段信息:alter table student change id id int(20);

7)增加表字段信息:alter table student1 add class int(4) not null after id;

表数据的增删查改

提示:在数据库导入表时,要修改列的字段类型并设置主键;

主键:表中经常有一个列或多列的组合,其值能唯一地标识表中的每一行。这样的一列或多列称为表的主键,通过它可强制表的实体完整性。当创建或更改表时可通过定义 PRIMARY KEY 约束来创建主键。一个表只能有一个 PRIMARY KEY 约束,而且 PRIMARY KEY 约束中的列不能接受空值。由于 PRIMARY KEY 约束确保唯一数据,所以经常用来定义标识列。

表数据新增格式:insert into 表格名(列名) values(值)

先导入student和score表,表为Excel,可以自己编写。

例子:

mysql> insert into student(id,class,number,name) values(81,4,19,'stu81');

mysql> insert into student(id,class,number) values(82,4,20);

mysql> insert into student values(83,4,21,'stu83');

mysql> alter table student change id id int(2) auto_increment;

注释:auto_increment以1为单位自增长的意思;

mysql> insert into student(class,number,name) values(4,22,'stu84');

mysql> alter table score change id id int(4) auto_increment;

注释:auto_increment自增长的意思。+1。输入该命令,表格会在新输入自动新增长新的一行,id也会成自增。

mysql> insert into score(class,number,maths,chinese,english) values(4,19,80,78,98);

mysql> insert into score(class,number,maths,chinese,english) values(4,20,98,88,68);

mysql> insert into score(class,number,maths,chinese,english) values(4,21,91,83,78);

mysql> insert into score(class,number,maths,chinese,english) values(4,22,67,83,88);

查询表数据格式:select * from <表名> where

注释:语句以逗号做分隔,*通配符,select是展示的意思,where是条件;

例子: 查询学生信息表中所有信息:select * from student;

查询成绩表中,列id,class,chinese的信息:select id,class,chinese from score;

分组与函数查询

温馨提示:分组之后查询其他函数结果是不正确的;

分组函数:group by

按班级分组,查询出每班数学最高分:select class,max(maths) from score group by class;

不分班级查询总人数最高分: select max(maths) from score;

注释: max:最大值;

按班级分组,查询出每班数学最低分:select class,min(maths) from score group by class;

注释:最小值min;

按班级分组,查询出每班数学总分:select class,sum(maths) from score group by class;

注释:sum:总分;

按班级分组,查询出每班数学平均分:select class,avg(maths) from score group by class;

注释:avg:平均值:

按班级分组,查询出每班学生总数:select class,count(*) from score group by class;

注释:count:有价值的;

语句执行顺序: from先执行,后执行where, 再接着执行having,limit等。

分组与函数查询

温馨提示:分组之后查询其他函数结果是不正确的;

分组函数:group by

按班级分组,查询出每班数学最高分:select class,max(maths) from score group by class;

不分班级查询总人数最高分: select max(maths) from score;

注释: max:最大值;

按班级分组,查询出每班数学最低分:select class,min(maths) from score group by class;

注释:最小值min;

按班级分组,查询出每班数学总分:select class,sum(maths) from score group by class;

注释:sum:总分;

按班级分组,查询出每班数学平均分:select class,avg(maths) from score group by class;

注释:avg:平均值:

按班级分组,查询出每班学生总数:select class,count(*) from score group by class;

注释:count:有价值的;

语句执行顺序: from先执行,后执行where, 再接着执行having,limit等。

例句:

select class,max(maths) from score where group by(分组) class having(所有) order by(排序) limit

from后面可以加兹查询,语句先执行后面再执行前面

运算符:数学运算符

mysql> select class,number,maths,maths+5 from score;

mysql>select class,number,chinese+maths+english from score;

mysql> select *,maths+chinese+english as total from score;

mysql> select *,maths+chinese+english as total from score order by total desc;

mysql> select class*2,number,maths+chinese+english as total from score order by total desc;

连接查询

左连接查询:

mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu left join score sc on stu.id=sc.id;

注释:stu:为别名。student stu left join score:student:为主表,score为副表显示。 left join:为左连接。 两表关联:其ID必须一一对应(stu.id=sc.id);

右连接查询:

mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu right join score sc on stu.id=sc.id;

内连接查询:两个表同时都有的内容才会显示。

mysql> select stu.*,sc.*,maths+sc.chinese+sc.english from student stu join score sc on stu.id=sc.id;


以上就是最全的MySQL数据库的常用指令的详细内容,更多关于最全的MySQL数据库的常用指令的资料请关注九品源码其它相关文章!