MySQL——基本操作、常识

MySQL

数据库基本知识

D:data

DB:database

DBMS:数据库管理系统

RDBMS:关系型数据库管理系统

DDL:数据定义语言

DML:数据操作语言

DQL:数据查询语言

DCL:数据控制语言

数据库中的基本库:
  • information_schema:主要存储系统中的一些数据库对象信息,比如用户表信息、列信息、权限信息、字符集信 息、分区信息等。每个用户都可以查看这个数据库,但根据权限的不同看到的内容不同。 • performance_schema:MySQL5.5引入的系统库,用于存储系统性能相关的动态参数表。
  • sys:MySQL5.7引入的系统库,本身不记录系统数据,基于information_schema和performance_schema之上,封 装了一层更加易于调优和诊断的系统视图。
  • mysql:存储系统的用户权限信息。
  • sakila和world:两个为样例数据库。
数据库的基本操作
  1. 连接数据库

    mysql -u root -p	
    
  2. 创建数据库 test1

    create database test1
    
  3. 查看数据库中的库名

    show databases;
    
  4. 使用当前数据库test1

    use dbname;	
    use test1;
    
  5. 查看当前数据库中所有的表

    show tables;
    
  6. 创建表

    create table 表名(
    	列名 数据类型(长度) 列的约束,
    	列名 数据类型(长度) 列的约束,
    	列名 数据类型(长度) 列的约束,
    );
    
    create table t_user(
    	userid int primary key ,
    	username varchar(40) ,  
        userpassword varchar(20), 
        phone varchar(11),
        email varchar(40),	
        role char(1),	
        statu char(1), 
        account decimal(10,2), 
        age int ,
        registime timestamp,
        imgpath varchar(255)
    );
    
  7. 查看表结构

    desc 表名; 
    
  8. 删除表

    drop table 表名;
    
  9. 复制表

    create table emp1 like emp;	-- 复制结构,复制约束
    create table 新表名 as select * from 原表名;--复制结构,不复制约束
    
  10. 删除

    drop database 数据库名;
    drop table 表名;
    truncate 删除表中全部数据,删除大量数据时建议使用
    
  11. 创建主键约束

    基本方式
    create table temp(
    	id int primary key,
    	name varchar(20)
    );
    
    组合方式
    create table temp(
    	id int ,
    	name varchar(20)
    	primary key(id,name);
    );
    
  12. 删除主键约束

    alter table temp drop primary key;
    
  13. 添加主键约束

    alter table temp add primary key(id,name)
    
  14. 创建唯一约束

    create table temp1(
    	id int not null,
        name varchar(20),
        password varchar(20),
        unique(name,password)
    );
    
  15. 操作唯一约束

    -- 删除唯一约束
    alter table temp drop index name;
    -- 添加唯一约束
    alter table temp1 add unique(name,password);
    -- 修改唯一约束
    alter table temp1 modify name varchar(50) unique;
    
  16. 非空约束

    -- 增加非空约束(创好的表内)
    alter table temp1 modify name varchar(20) not null;
    -- 取消非空约束
    alter table temp1 modify name varchar(20) null;
    -- 取消非空约束,增加默认值
    alter table temp1 modify name varcahr(20)  default '0';
    
  17. 自增约束

    id int auto_increment;
    

案例

在这里插入图片描述

-- 添加单列 在ename列后方添加  [first|after] 在某列之前或之后
alter table emp add  esex varchar(1) default '0' after ename;
-- 添加多列
alter table emp add (
    esex char(4) default '0',
    etel varchar(11) 
);
-- 删除列
alter table emp drop esex;
-- 删除多列
alter table emp drop esex, drop ename;
-- 添加主键约束
alter table emp add primary key(ename);
-- 删除主键约束
alter table emp drop primary key;
-- 修改列定义
alter table emp modify esex varchar(8) default '0';
-- 修改列名称
alter table emp change esex sex varchar(8) default '0';
-- 修改表明
alter table emp rename emptest;

数据库DML语言

-- 创建学生表
create table student(
    sno int primary key auto_increment ,
    sname varchar(20) not null ,
    sex char(1) ,
    age int ,
    phone varchar(11) unique not null 
);

-- 创建班级表
create table class(
	classid int primary key auto_increment ,
    classname varchar(40) 
);

-- 插入数据(全表插入)
insert into student values(null,'韩梅梅','女',18,'12312312311');

-- 插入部分数据
insert into student(sname,phone)values('小岳岳','11231231231');

alter table student add email varchar(50) null;

update  student set age=18 where sname='小岳岳';

update student set age = 20,phone = '13240099009' where sname='韩梅梅';
-- 修改年龄大于十岁的学生Email为空
update student set email is null where age>10;

-- 删除记录
delete from student where sname='小岳岳';

-- 全表查询
select * from student;

-- 根据条件查询
select * from student where sno = 1;

-- 查询指定字段名
select sname,phone from student;

AS 关键字

-- 为列名中的sal起别名 工资
select ename,sal '工资' from emp ;
-- emp 起别名 e
select e.ename,e.sal as '工资' from emp e ;

WHERE查询

关键字注释
select展示
where查询位置
group by分组
having分组后可以直接用聚合函数
distinct去重
order by排序
limit展示几条
regexp正则
like模糊匹配

在这里插入图片描述

-- 比较条件
select * from emp where age>18;

select * from emp where age>=18;

select * from emp where age!=18;

-- 判断空值
select * from emp where phone is null;

select * from emp where phone is not null;

select * from emp where deptno is null;

select * from emp where deptno  is not null;

-- and 和 or
select name , sex , phone from emp where sex='男' and deptno = '开发部门' ;

select * from dep where depname = '开发部' ; -dep表中如果开发部对应的depid为1
select name , sex , phone from emp where depid and sex = '男';

select * from emp where name='lucy' or name='jack'

select * from emp where age<20 and sex='1' and sal>5000;

select * from emp where (sex='1') and (deptno = '开发部门' or deptno = '财务部');

select * from user where username='admin' and password='123456';

-- 确认集合(or关系 关键字 or 或者 in)
select * from student where age in (15,17,19);

select * from emp where name in ('lucy','rose','jack');

select * from emp where name='lucy' or name='rose' or name='jack';

-- 确认范围 between 值1 and 值2
select * from emp where sal between 5000 and 10000;

-- 精确查询
select * from emp where name='lucy';

-- 模糊查询
select * from emp where name like '%o%';

select * from emp where name like '_u%';

案例

-- 创建数据库
create database mytest;

-- 创建表
create table admin(
    id int ,
    adminName varchar(15),
    password varchar(50)
);

-- 添加数据
insert into admin values (1,'root','123456');
insert into admin values (2,'admin','123456');
insert into admin values (3,'user','123456');

where name like ‘%o%’;

select * from emp where name like ‘_u%’;


案例

```mysql
-- 创建数据库
create database mytest;

-- 创建表
create table admin(
    id int ,
    adminName varchar(15),
    password varchar(50)
);

-- 添加数据
insert into admin values (1,'root','123456');
insert into admin values (2,'admin','123456');
insert into admin values (3,'user','123456');
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值