MySQL基本命令

本文介绍了在Windows系统下使用CMD进入MySQL数据库的方法,包括切换用户、创建/删除数据库、进入数据库的命令。接着详细阐述了数据表的操作,如查询表、创建表、修改表结构、删除表以及字段管理。还涵盖了数据的增删查改,如插入数据、查询数据、排序和条件查询。最后讲解了不同类型的连接查询,如左连接、右连接和内连接,以及多表查询的示例。
摘要由CSDN通过智能技术生成

数据库常用命令

进入数据库,在win系统下,打开cmd,切换用户权限,进入root。

沒权限,用root登录:mysql -u root

如果root有密码:mysql -uroot -p

数据库的创建

查询所有数据库:show databases;

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

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

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

数据表的操作

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

2)创建表:create table 表名 (id int(),name char(8))  #括号里面是自己需要的字段

int数字类型;

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

Name为表的第二列名字。

char:类型;

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

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

3)查看表结构:desc 表名

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;

8)删除一个表字段:alter table student1 drop number;

表数据的增删查改

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

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

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

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

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

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

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

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

3)表数据排序操作:升序:order by     降序:升序语句末尾加 desc

例子:查询成绩表中,列id,chinese的信息并且以列chinese排序

select id,chinese from score order by chinese;(升序)

select id,chinese from score order by chinese desc;(降序)

4)表数据查询操作:

(1)查询1班与2班的成绩信息:mysql> select * from score where class=1 or class=2;

(2)查询语文为77并且数学为88的成绩信息:

mysql> select * from score where chinese=77 and maths=88;

(3)查询1,2,3班的成绩信息:mysql> select * from score where class in (1,2,3);

查询不为4班的成绩信息: mysql> select * from score where class not in (4);

(4)查询不为4班的成绩信息: mysql> select * from score where class !=4;

注释: !在数据库里面为否定的意思:

(5) 查询1班到3班的成绩信息: mysql> select * from score where class between 1 and 3;

注释: between:在```之间,中间的意思:

(6) 查询不为3班与4班的成绩信息:mysql> select * from score where class not in (3,4);

(7)查询语文成绩大于等于80小于等于90的成绩信息

mysql>select * from score where chinese>=80 and chinese<=90;

(8) 统计成绩表的总数:mysql> select count(*) from score;

(9) 按照英语去重,显示英语成绩信息:mysql> select distinct English from score;

注释: distinct 去除重复的意思;

(10) 显示4到7行的数据:mysql> select * from score limit 3,4;

注释:数据库数据排列:0,1,2,3; 3显示第4行; 4,5,6,7共有4行; 3,4 ;

3表示第4行,4表示从第3行开始到第7行,共有4行;

(11) 按chinese排序,显示4,5行数据: mysql> select * from score order by chinese limit 3,2;

(12) 查询出学生姓名为stu10的学生信息:mysql> select * from student where name='stu10';

注释:只要不是数字,有汉字数字字母多种组成的形式都要加单引号,表示字符串。

(13) 查询出学生姓名为stu10或者stu15的学生信息:

mysql> select * from student where name in ('stu10','stu15');

(14) 分组查询每个班的人数:mysql> select class,count(*) from student group by class;

分组与函数查询

分组函数: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等。

连接查询

左连接查询:

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;

显示查询数据连接:把后表与前排合起来在一个表显示。

select id,name,class from student union select class,number,maths from score;

多表查询

select name,student.class,student.number,maths,chinese,english from student,score where student.id=score.id;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值