MySql基础

创建数据库:

create database 数据库名字;

切换数据库:

        use 数据库名字;

删除数据库:

         drop 数据库名;

数据表

创建表:

create table 表名(

        数据类型  数据名 是否为空 是否为主键 默认值 是否自增 ,

        ......

)

注意: 

        1.除了最基本的数据类型 和 数据名必不可少,其余的都可以不写

        2.不为空 not null 

        3.是否为主键 primary key :该列不空,且不重复

        4.是否自增 auto_increment

        5.无主键,mysql内核会自动分派一个伪列(_row_id)

增加表:

insert into 表名 (字段列表) values (对应字段的值);

    1.当表名后的字段列表不是默认列表的时候,values 的值序必须与其保持一致

    2.当表名后的字段列表省略不写的时候,values后的值序必须创建表时保持一致

    3.当表的列未给值,则默认使用null , 或者设置的default 中的值

批量添加数据 :

    insert into 表名 (字段列表)
         values (对应字段的值)
                 ,(对应字段的值)
                 ,(对应字段的值)
                 ......;

添加字段:
    
    alter table 表名 add 字段名 字段类型;

修改表:

update from 表名 set 字段名 = 值 , 字段名 = 值 where 条件语句 ;

    注意: 

        若没有where 则对修改的是整个表中所有数据对应字段的值
        
修改字段类型:
       
    alert table 表名 modify 字段名 数据类型;

修改字段:
        
    alter table student change 原来字段 新字段 新字段类型;

删除表:

delete * from 表名 where 条件;
    
    注意:
        
        当没有where的时候,删除的是整个表中的数据,会保留字段;
        
        truncate table 表名; 截断表,保留字段 , 删除全部数据 (适用于数据量较大的情况)
        
        
        drop table 表明; 删除整个表 , 包括字段;

查询表:

-- 切换数据库
use 库名
-- 复制表结构 , 带结构
	create table 表一 select * from 表二;

	create table student5 select * from student2;
	desc student5;
	select * from student5;
-- 注意: 
		当后边有where语句,且不成立的时候,不会复制表数据
		当select 后边有字段,则复制的只有当前字段
		复制表的时候,无法复制主键的约束
		
-- 将查询的结果插入某表中
	insert into student5 select * from student2;
	select * from student5;
	delete  from student5;
	
-- 将查询到的数据去重 distinct 
	select distinct id ,  from student;
	alter table student change name uname char(10);
	desc student;
	
-- 排序 , 将查询的数据按照某个规则进行排序 order by
	select * from student order by password desc;
	update student set password = '78' where uname = '图腾富贵';
	
	select count(distinct id) from student where id = 2;
	select count(id = 2) from student where id = 2;

-- limit 是对查询出来的数据进行分页管理 , limit 参数一,参数二
	参数一 : 第几页
	参数二 : 每一页的大小
	
	desc student;
	select * from student  order by uname limit 3;
	
-- 多条件查询
	desc student;
	select * from student where id > 7 and password = '123456';
	
-- 模糊查询 like '_' 代表一个字符 , '%' 代表后边可以有任何东西
	select * from student where uname like '_克';
	select * from student where uname like '%兀%';
	
-- 关于边界值的问题
	
	
-- 特殊语法: between 值一 and  值二
	select * from student where id between 6 and 8;
	
-- 特殊语法: in 查询 
	select * from student where id in (1,2,3);
	
-- 特殊语法: 关系运算 and or < > = 
	
-- 别名 + 计算列
	select uname 名字 , id as 编号 from student where uname like '%';
	
-- 字符串拼接
	select concat(id , uname) 编号和名字 from student ;

-- 分组函数 (聚合函数) count , sum , avg , min , max
	select count(*) 计数 , sum(id) 编号总和, avg(id) 平均数字, min(id) 最小编号, max(id) 最大编号 from student;
	
-- 日期类型 now() sysdate() 两种时间方式 , 理论差不多
	select now();
	select sysdate();
	
-- 重点(*) 联合查询 也称联表查询
	create table departMent(
		id int primary key auto_increment comment '部门编号',
		dname char(20) not null comment '部门名称',
		dress char(30) not null comment '部门地址'
	);
	select * from departMent;
	create table employee(
		id int primary key auto_increment comment '员工编号',
		ename char(20) not null comment '员工姓名',
		money double comment '员工薪资',
		did int not null comment '员工所属部门'
	);
	select * from employee;
	
	insert into departMent  values 
		(0 , '法务部' , '丽人科技园c') , 
		(0 , '销售部' , '丽人科技园B') ,
		(0 , '坑爹部' , '丽人可救援');
		
	select * from departMent;
	
	insert into employee values
		(0 , '张三' , 50000 , 1),
		(0 , '李四' , 30000 , 1),
		(0 , '王五' , 20000 , 2),
		(0 , '马六' , 5000 , 3),
		(0 , '芳芳' , 4000 , 1),
		(0 , '阿里' , 100 , 3);
		
	select * from employee;
	
-- 内连接

	 基于where

	select ename as 员工姓名 , did 部门名称 , money 薪资 from 
		departMent d, employee e where d.id = e.did;
	
	 基于inner join ... on 
	 
	select e.ename 员工姓名,e.did 部门名称 , d.dress 部门地址
	 from departMent d 
	 inner join employee e 
	 on (d.id = e.did);
	 
-- 左连接 (交集 + 左边有 而 右边没有的记录) 
	select e.ename 名字 , d.dname 部门名称 , d.dress 部门地址 , e.money 薪资 
		from departMent d left join employee e on (d.id = e.did);
-- 右连接 (交集 + 右边有 而 左边没有的记录)
	select e.ename 名字 , d.dname 部门名称 , d.dress 部门地址 , e.money 薪资 
		from departMent d right join employee e on (d.id = e.did);
-- 子查询 (查询出销售部门的所有员工)
					(查询出所有员工)
					select e.* , 
					(select d.dname from departMent d where d.id = e.did) 部门
					from employee e;
					(各部门工资最低的员工)
					(各部门工资 >= 8000 员工工资总额)
-- 场景16 - 4 :全外连接 ()

select d.deptno ,d.dname,d.loc,e.empno,e.ename,e.sal,e.deptno edeptno from department d left JOIN employee e on (d.deptno = e.deptno) UNIONselect d.deptno ,d.dname,d.loc,e.empno,e.ename,e.sal,e.deptno edeptno from department d right JOIN employee e on (d.deptno = e.deptno);

对字段的操作

增加列

alter table 表名 add 列名 primary key auto_incretement 数据类型;

注意 : 
    主键和自增可有可无

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值