IDEA数据库基本操作

首先进入自己的数据库操作进入idea的mySQL中连接数据库。

该操作仅针对于关系型数据库操作

进入数据库中创建表格以及删除表格

create table user  //创建表格user  create

drop from user;      //删除表格 user   drop

添加表格内容

insert into user()

values(内容)

修改表格内容操作

update user set  内容  //对已存在表内容进行修改

查询表格内容

select *from  ; //查询表格

查询方法:

对表添加别名:

引用关键词 “as” 后接表别名

限制查找结果输出自己想要的结果:

使用关键字“while”: 后接需要对所查询的事物进行限制;

        运算符 between 1 and 2   // 在1和2之间存在的事物

        in   //指定为该值

        like‘某%’  //含有还字的查询结果

        <>不等于  、is   null  in  等

关联两个表方法

inner join  :将两个表进行关联,所显示的内容为两表共同存在的内容

left join :将两个表进行关联,所显示的内容为左表存在的东西与右表共同存在的,没有的数值用空表示。

right join :将两个表进行关联,所显示的内容为右表存在的东西与左表共同存在的,没有的数值用空表示。

对表中某一列进行排列:

order by “表列名” 将表中数值从小到大排列

order by “表列名” + desc  将表中数值从大到小排列

限制表输出长度:

limit  num : 限制表输出总行数为 num
offset num1 :  限制表跳过num1行 所输出的表第一行因为 num1+1行

对表中内容进行整合输出

group by :整合某一列表的内容将其聚合

        聚合函数:count()、sum() 、max()、min() 、avg()

                having:对整合后的数值进行查找对比输出 作用类似while只针对于聚合后的值进行操作

eg:

select s.stu_sex,count(*),sum(b.money) 
from student s
         inner join s_card sc on s.stu_id = sc.s_id  //连接表 student 和 s_card
         inner join borrow b on sc.id = b.card_id     //连接表 student 和 borrow 此时已经将三个表关联了
group by s.stu_sex  //对student 表中 stu_sex内容进行整合
order by sum(b.money) desc   //对b表中的金额进行从大到小排列
limit 5  offset num1   //限制输出长度5 跳过第一行

while money > 3   //只输出金额大于三的
;

子查询

子查询就是查询语句的嵌套

在select 后面, 必须返回单行单列
在from后面, 做为一个表, 返回一个结果集

select *

(select b3.book_name from book b3 where b.book_id = b3.book_id) //在select后查询返回的结果为子查询的那一列的结果
as book_id,s.name
from borrow b inner join
        (select  sc.id as s_id,stu.stu.name as name from s_card sc:inner join student stu on sc.s_id = stu.stu_id) //from 后查询后返回所查询的关联表
;

在where 条件里面,根据条件要求给定
where book_id in
      (
          select b.book_id, count(*)
          from borrow b
                   inner join book b2 on b.book_id = b2.book_id
          group by b.book_id
          order by count(*) desc)   //所返回限定的条件要求

增加、删除、修改表补充使用

#distinct 去除重复的行
select distinct card_id,book_id from borrow;

#关联两个表上下  只要列能对上就能直接连接
select * from book
union all
select * from borrow
;
#重复添加
insert into borrow(card_id, book_id, borrow_date, expect_returnDate, money)
select card_id, book_id, borrow_date, expect_returnDate, money
from borrow;

#删除某一列
delete from borrow
#where card_id
where id in(select card_id);
#修改金额小于4的为4
update  borrow set money = 4
where  money<4;

#将book。type_id = 5的金额改为5
update borrow b,book bo set b.money = 4
where b.book_id = bo.book_id and bo.type_id = 5;

#事物锁
#select 共享锁
begin ;
#拍他锁   #for updect
update book set  book_name = 'fj' where book_id = 1;
#提交
commit ;
#回降
rollback ;
alter table windows modify money not null;

关联表两表方式:

 constraint fk_book_book_type_id
        foreign key (type_id) references test1.book_type (id)
)

设置表内容唯一标识

primary key,设置自增长

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值