mysql 的基本内容

今天老师带着学习了mysql数据库的基本内容,为即将到来的oracle做个预热,感觉理解起来不是很难,但还是要细心多练才行。

下面是整理的mysql数据库基本内容:

是否安装:计算机-管理-服务-mysql

mysql卸载:控制面板-程序卸载-mysql

mysql   文件   服务  界面

表:

字段名  数据类型 约束

约束:主键 外键  非空  默认值 ..

Mysql常用命令

win + r    cmd

输入命令:mysql -u root -p

输入命令注意:

1、命令结束符号是分号

2、所有的符号都是英文半角

3、只有遇到分号  mysql才认为结束

4、多个命令用分号隔开 create database stu; drop database stu;

5、引号要打全  不然分号都不让出

查看数据库命令

show databases;

错误:show databasesshow databases;

创建数据库

create  database 库名;

删除数据库

drop database  库名;      

进入数据库

use 库名;  

查看所有的表

show tables;

创建表  字段名  数据类型  约束

约束:主键: primary key   

非空: not null

默认值: default

创建表注意:

1、字段必须有字段名和数据类型

2、多个字段用逗号分隔

3、最后一个字段不要加逗号

create table 表名(

sno varchar(20) primary key,

sname varchar(20) default "老王",

ssex  int(10)

);

create table student(

sno varchar(20) primary key,

sname varchar(20) default "老王",

ssex  int(10)

);

删除表

drop table 表名;

查看表结构

desc 表名;

字段名 数据类型 是否为空  主键   默认值    说明

insert into 表名(sno,sname,ssex) values(1,2,3);

数据的增删改查

添加:添加 哪个表(哪些列) 值是(1,2...);

Insert into 表名(字段名1,...) values(1,...);

insert into 表名 values(1,2...),(1,2...);

添加全字段可以省略表名后面()

查询 查询 哪些字段 哪个表

select * from表名;

查询 学号字段,姓名 从 student

select sno from student;

修改:修改 哪个表 设置 哪个字段=

update 表名 set 字段1 = 1, 字段2 = 2...;

删除:删除 哪个表

Delete from 表名;

查询条件

(1)简单查询

select * from Info

select Code as '代号',Name as '姓名' from Info

(2) 条件查询

 Where后面跟条件  条件要写清楚

查询成绩表中成绩(degree)为92的信息

Select * from score where degree =”92”;

查询成绩表中课程号是3-245并且成绩是86的信息

Select * from score where cno='3-245' and degree=”86”

 或者用or  并且用and

(3) 模糊查询 like  not like

查找老师表中姓李的 名字是两个字老师

select * from teacher

 where tName like '%%'  

%代表任意多个字符  _代表一个字符

(4)排序查询 order by 字段 排序值(desc/asc

  select * from student order by class asc

(5)范围查询 关系运算符  between。。。and

select * from Car where Price>=40 and Price<=60

select * from Car where Price between 40 and 50

(6)离散查询 in   not in

select * from student where sname in ('张三','李四')

。。。where sname =“张三” or  sname =“李四”

(7)聚合函数,统计查询

select sum(Price) from Car #查询所有价格之和 sum()求和

select count(Code) from Car #查询数据条数

select max(Code) from Car #求最大值

select min(Brand) from Car #求最小值

select avg(Price) from Car #求平均值

(8)分页查询 limit 从第几条开始,取多少条数据

#每页显示5条数据,取第2页的数据

select * from student limit (pageSize-1)*5,5

(9)去重查询distinct

select distinct cno from score;

(10)分组查询 group by 字段  having 条件

select count(*),cno,group_concat(degree),sum(degree) from score group by cno ;

select cno,group_concat(degree),sum(degree) from score group by cno having count(*)>3

#分组之后根据条件查询使用having 不使用where

高级查询

  1. 连接查询,对列的扩展

   Select * from student as stu,score as sc

where stu.sno = sc.sno and sc.sno = “103” ;

2.联合查询,对行的扩展

    select Code,Name from Info

    union

    select Code,Name from Nation

3.子查询

(1)无关子查询

外层查询 (里层查询)

子查询的结果当做父查询的条件

子查询:select Code from Nation where Name='汉族'

父查询:select * from Info where Nation = ''

    select * from Info where Nation = (select Code from Nation where Name='汉族')

(2)相关子查询

    查询汽车表中油耗低于该系列平均油耗的所有汽车信息

    父查询:select * from Car where Oil<(该系列平均油耗)

    子查询:select avg(Oil) from Car where Brand = '某个系列'

    select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand )

原文地址:https://www.cnblogs.com/qianqian528/p/7756167.html