数据库操作

数据库操作

1.

1.1. 【插入单行】

insert [into] <表名> (列名) values (列值)
例:insert into Students (姓名,性别,出生日期) values (‘zyl’,‘男’,‘1988/04/16’)

1.2. 【插入多行】

insert into persons (id_p, lastname , firstName, city )values (200,'haha' , 'deng' , 'shenzhen'),

(201,'haha2' , 'deng' , 'GD'),

(202,'haha3' , 'deng' , 'Beijing');

1.3. 【将现有表数据添加到一个已有表】

insert into <已有的新表> (列名) select <原表列名> from <原表名>

例:insert into tongxunlu (‘姓名’,‘地址’,‘电子邮件’) select name,address,email from Strdents

 

2.

2.1. update <表名> set <列名=更新值> [where <更新条件>]

例: update Student set 年龄=18 where 姓名=‘zyl’

 

3.

3.1. delete from 表名 where <更新条件>

例: delete from afinfo where name="王芳芳";

3.2. 删除整个表里的内容

truncate table <表名>

例: truncate table tongxunlu

 

4.

4.1. select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]

例: select * from afinfo where name like '徐%';

    select name, age where sex=“女“ order by age desc

4.2. 【在查询中使用AS更改列名】

   例:select name as 姓名 from student where xingbie=‘男’

4.3. 【使用like进行模糊查询】like只适用于字符串

例:select * from student where name like ‘赵%

4.4. 【使用between在某个范围内进行查询】 

例:select * from student where age between 18 and 30

4.5. 【使用in在列举值内进行查询】

select name from student where address in (‘北京’,‘上海’)

4.6. 【使用group by进行分组查询】

  根据“By”指定的规则对数据进行分组

 select studentID as 学员编号, SUM(score) as 总成绩 from score  group by studentID order by SUM(score) desc

4.7. 【使用having子句进行分组筛选】

例:select studentID as 学员编号,AVG(score) as 平均成绩 (注释:这里的score是列名) from score (注释:这里的score是表名) group by studentID having AVG(score) >60

 说明:显示平均数在60分以上的学生,where只能在没有分组时使用

 

5. 多表联接查询

5.1. 内联接

【在where子句中指定联接条件】

例:select a.name,b.score from a,b where a.name=b.name

说明:查询表a和表b中name字段相等的记录,并显示表a中的name字段和表b中的score字段

【在from子句中使用join…on

例:select a.name,b.score from a inner join b

on (a.name=b.name)

5.2. 外联接

【左外联接查询】

例:select s.name,c.courseID,c.score from strdents as s

left outer join score as c on s.scode=c.strdentID

说明:在strdents表和score表中查询满足on条件的行,条件为score表的strdentID与strdents表中的sconde相同

【右外联接查询】

例:select s.name,c.courseID,c.score from strdents as s

right outer join score as c on s.scode=c.strdentID

说明:在strdents表和score表中查询满足on条件的行,条件为strdents表中的sconde与score表的strdentID相同

--------------------------------------------------

参考文章: 

原文链接:https://blog.csdn.net/qq_34979346/article/details/90727843

posted @ 2020-03-15 19:06  梦痕~  阅读(124)  评论(0编辑  收藏  举报