ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

Mysql教程:(一)数据库常用基础命令

2021-11-06 11:31:44  阅读:181  来源: 互联网

标签:教程 chinese 数据库 Mysql id score mysql class select


数据库常用命令

1、登录

  • 进入数据库,在win系统下,打开cmd,切换用户权限,进入root:
  • 沒权限,用root登录:
mysql -uroot
  • 如果root有密码:
mysql -uroot -p 

2、数据库创建

  • 查询所有数据库:
show databases;
  • 创建数据库:
create database  <数据库名>;
  • 删除数据库:
drop database  <数据库名>;
  • 进入数据库:
use  <数据库名>;

3、数据表的操作

1)查询数据库下表:

show tables;

2)创建表1:

create table student(id int(4) primary key,name char(20));
  • 注释: id为表的第一列;     
  • int数字类型;  
  • primary key主键的意思,列不能重复。
  • Name为表的第二列名字。  
  • char:类型;  

创建表2:

create table score(id int(4) not null,class int(2));
注释: not null字段不能为空。

创建表3:

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; 

8)删除一个表字段:

alter table student1 drop number;

 

4、表数据的增删查改

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

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

1) 表数据新增格式:

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);

2) 查询表数据格式:

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班的成绩信息: 
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; 

 

4、作业:

1,查询4班的成绩信息:
select * from score where class="4";
2,查询4班,语文成绩大于80小于90的成绩信息:

select * from score where class in (4) and chinese>80 and chinese<90;
3,查询学生表中5到10行的数据:
select * from student limit 4,6;
4,显示3班语文成绩为90,数学成绩为68,的class与number信息,:

select class, number from score  where class="3"  and  chinese=90 and  maths=68;
5,查询出4班成绩并且按语文成绩倒序排序:

select * from score where class="4" order by chinese desc;
6,查询2班与3班,语文成绩与数学成绩都大于80的class与number信息:

select class, number from score where class in (2,3) and  chinese>80 and maths>88;

 

标签:教程,chinese,数据库,Mysql,id,score,mysql,class,select
来源: https://www.cnblogs.com/yinzuopu/p/15516251.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有