Mysql数据库基本操作语法命令归纳

62 阅读6分钟

​持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第1天,点击查看活动详情

一、数据库相关概念:

  • DB Database 数据库
  • DBA Database Administrator 数据库管理员
  • DBS Database System 数据库系统
  • DBMS Database Managerment System 数据库管理系统

DBS=DBMS+DBA+DB+软件+硬件

三种模型:层次模型 网状模型 关系模型

关系型数据库:
设计数据库的三个步骤:
1.概念结构设计阶段:E-R 图: 实体(集) 属性 联系
一对一(1:1) 一对多(1:n) 多对多(m:n)
2.逻辑结构设计阶段
3.物理结构设计阶段

常见的数据库:
MySql
SQLServer
Oracle
DB2

约束:
primary key 主键约束:不能为空且不能重复:唯一区分记录的标志(表级约束)
foreign key 外键约束
not null 非空约束
unique 唯一约束 null 'a' null
default 默认约束
check 检查约束(在mysql中无效) set enum
auto_increment 自增 (主键)

主表 从表

二、数据库操作:

sql命令

    status;        查看当前状态
    show databases; 显示所有数据库
    use 数据库名;    使用/打开数据库

  create database bochy;
  create database [if not exists] bochy;(如果不存在)创建数据库
  drop database [if exists] bochy;  (如果存在)删除数据库(谨慎使用)

三、数据库中表的操作:

    use bochy;    先打开数据库

创建表:

    create table [if not  exists]  student(
    id int,
    name  varchar(6)  not null ,
    sex set('男','女'),
    ext text
    );

   create table student (
     id int primary key  auto_increment,
     name varchar(8) not null,
     age smallint default 18,
     address varchar(50) default '郑州市金水区',
     ext decimal(10,2)
     );

   create table score (
     id int ,
     cid int ,
     grade int,
     primary key(id,cid),
     foreign key(id) references student(id),
     foreign key(cid) references course(cid)
     );

  显示当前数据库的所有表:show tables; 
  查看表结构:desc student;

添加数据:

insert into student  values( 2015001,'张三','男','dfdsfbhfhw'  );

添加部分列数据:

    insert into student(id ,name)  value(2015005,'小明');

添加多条数据:

    insert into student values(2015006,'xiao王','男',null) ,(2015007,'ssss','男',null),(2015008,'xxx','女',null);

删除表中数据:

    delete from student ;(删除表中所有数据)
    delete from student where id=2015006;(删除满足条件的数据)
    delete from student where id  is null;
    delete from student where id!=2015005;
    delete from student where id <>2015005;

查看表中的数据:

  select * | 部分列
    from  表名
    | where (筛选记录的)条件
    | group by 字段名   having 筛选分组的条件
    | order by 字段名|聚合函数  asc|desc 
    | limit [起始下标,]长度 ;

    select * from Student;
    select id, name from student;
    select id as 学号, name  as  姓名  from student;
    select id  学号, name  姓名  from student;

查询满足条件的前10条数据

select id,name,sex,sum from student where sex='男'  order by sum  desc  limit 10 ;

从下标为2的记录开始,往后查询5条数据

 select * from student limit 2,5;

模糊查询

    select *   from student where name like '%a%';
    select *   from student where name like '%a_';

比较查询

 select * from student where sex='男' and sum>300 ;
 select * from student where sex='男' or  sum>300 ;

范围查询

    select * from student where id='2015001' or id='2015003' or id='2015005' or id='2015021'  ;
    select * from student where id in( 2015001,2015003,2015005,2015007,2015021,2015100,2015112);

统计查询

select count(*),sum(age),avg(age),max(age),min(age) 最小年龄 from student where age between 20 and 40;

+----------+----------+----------+----------+----------+
| count(*) | sum(age) | avg(age) | max(age) | 最小年龄 |
+----------+----------+----------+----------+----------+
| 9 | 222 | 24.6667 | 35 | 21 |
+----------+----------+----------+----------+----------+
1 row in set (0.05 sec)

select major,count(*) from student where major='信息工程';

+----------+----------+
| major | count(*) |
+----------+----------+
| 信息工程 | 2 |
+----------+----------+
1 row in set (0.00 sec)

分组查询

select sex ,count(*) 人数 from student group by sex;

+------+------+
| sex | 人数 |
+------+------+
| 男 | 7 |
| 女 | 5 |
+------+------+
2 rows in set (0.00 sec)

select major,count(*) from student group by major;

+--------------+----------+
| major | count(*) |
+--------------+----------+
| java软件开发 | 2 |
| 平面设计 | 2 |
| 网络工程 | 3 |
| 信息工程 | 2 |
| 英语 | 3 |
+--------------+----------+
5 rows in set (0.05 sec)

select major,count(*) 人数 from student where age between 20 and 40 
    group by major  having 人数>=2  order by 人数 desc  limit 2;

+--------------+------+
| major | 人数 |
+--------------+------+
| 网络工程 | 3 |
| java软件开发 | 2 |
+--------------+------+
2 rows in set (0.00 sec)

子查询:

查找所有年龄大于李四年龄的学生的信息

select * from student where age> ( select age from student where name='李四'   );

查找成绩大于网络工程专业最高成绩的学生的信息

select * from student where sum> (  select max(sum) from student where major='网络工程'  );

any:任意一个

select * from student where sum > any(   select sum from student where major='网络工程');

all:所有

 select * from student where sum > all(   select sum from student where major='网络工程');

some:一些

select * from student where sum > some(   select sum from student where major='网络工程');

查询既选修了1001课程又选修了1002课程的学生的信息

select id from score where cid='1002'  and id in(    select id  from score where cid='1001');

+---------+
| id |
+---------+
| 2015001 |
| 2015021 |
+---------+
2 rows in set (0.00 sec)

自查询:

 select a.eno ,a.ename 员工姓名 ,b.ename 经理名称 from employee a left join employee b on a.boss=b.eno;

多表查询:

1.全链接(笛卡尔积):

select * from1,表2

2.等值连接:

select * from1,表2 where1.字段=表2.字段;

3.内连接:

select * from1 [inner] join2  on1.字段=表2.字段  where 

4.外链接

左外链接:在保留左表所有数据的基础上进行等值连接

select * from1 left[outer] join2  on1.字段=2.字段  where

右外链接:在保留右表所有数据的基础上进行等值连接

 select * from1 right[outer] join2  on1.字段=2.字段 where

5.联合查询

select * from student where major='信息工程'
union [all | distinct] 
select * from student where id in(2015001,2015002,2015020,2015021);