数据库基础语法

Navicat连接mgsql 数据库语法

use test;##注释 ctrl+/快速注释  ctrl+shift+/快速解除注释)

#切换数据库 use

use shopping ;     

Create database my_db;  新建名为my_db的数据库                                                               

select * from student;#student 里查询所有字段(*代表所有信息)

select name from student;#student 里查询name

select age ,name from student;#student 里查询nameage栏,字段之间用英文逗号隔开。

select * from student where name='周瑜';#查询周瑜信息

select age,name from student where name='周瑜';#查询周瑜的年龄信息

s

#模糊查 like

select * from student where name like '%';#首字是曹的

select * from student where name like '%';#尾字是操的

select * from student where name like '%%';#名字中带貂字的

select * from student where name like '_';#_下划线占位,一个下划线代表后面有一个字

select * from student where name like '__';

select * from student where tel like '%5670';#尾号为5670的人的所有字段

select * from student where tel like '%5670%';

#大于 小于 等于 不等于

select * from student where age ='21';#查找21岁的人的所有字段(id name age sex home tel bbb称为字段)

select * from student where age<='50' and age>='20';                                                                                                                           

select * from student where age<='20' or age>='50';

select * from student where age<'40' or age>'40';#年龄不是四十岁的人的所有字段

select * from student where age<>'40';#年龄不是四十岁的人的所有字段

select * from student where age!='40';#年龄不是四十岁的人的所有字段

select name,age,tel from student where name ='周瑜' or name='小乔';#查询周瑜和小乔的姓名 年龄 电话信息

select * from student where age ='40' and home='信阳';#查询信阳的40岁的人

select * from score where score<'80' and subject='语文';

#去重

select * from score;

select  COUNT(distinct name) from score ;#distinct 区别 用于,统计去重后有几个人

#默认不包含空值

select  count(*) from (select distinct age, name from score)  ss ;

select distinct subject from score;

select  distinct NAME,score from score;#name xcore 相同的数据去重。

#查询出名字是张飞,周瑜,小乔,关羽,孙权这些人的名字信息。

select * from student where name in ('张飞','周瑜','小乔','关羽','孙权');

select * from student where name not in ('张飞','周瑜','小乔','关羽','孙权');#查询不包含张飞.....的信息

select * from student where name in ('张飞');

select * from student where tel is null;

select * from student where tel is not null;#查询电话号码不是空值的所有信息

#BETWEEN and   ORDER BY

select * from student where age between '20' and '50';

select * from student where age between 20 and 50;#查找年龄在2050之间的人的所有信息

select * from student  where sex='' orDER BY age desc;#男的降序排列

select * from student  ORDER BY age asc,id desc;#先按照年龄升序排列,如果年龄一样则再按照ID降序排列

select * from student order by id desc ,age asc ;#如果di后不加desc,mysql默认升序排列

#取前几行limit

select * from student limit 5;#取前五行(LIMIT 限制)

select * from student limit 5,3;#取过滤到前五行的下三行(取原表的6.7.8行)偏移五个取三个

select * from student where sex='' limit 3;

select * from student where sex ='' orDER BY age asc limit 3;#将性别为男的按照年龄升序排列并取前三行

/*查询所有语法

1.select 字段1,字段2 from 表格名 where 条件  group by 字段 having 条件 order by 字段1 desc,字段2 asc  limit 3,3

use test;切换库

模糊查找:like

排序:order by ASC desc

去重:distinct

Group by  ,ORDER BY LIMIT 单独使用时,不加where

Select 字段1,字段2 from 表格名 where 条件 group by 字段 having 条件 order by 字段1,字段2 limit43

select name,subject,sum(score)from score  where name in('曹操','孙权','刘备','诸葛亮','孙尚香','貂蝉') GROUP BY name having sum(score)>100 orDER BY sum(score) desc limit 1,2;

*/

select id,age,sex,home from student

where age>'20'and age!='40' and sex='order by age desc,id  desc limit 3,3;

#聚合函数 

select *from student;

Select  sexavg(age) from student where sex='';

select sum(age) from student;

select min(age) from student;

select max(age)  from student;

select count(id) from student;#统计有多少个idcount后不能有空格

 /*临时重命名

#临时重命名(在本语句中起作用,可加as 也可不加)

对于字段太长时,可重命名,使结构看着简单易懂。

*/

select min(age)最小年龄 from student;

select max(age) as 最大年龄 from student;

select name 姓名,sex 性别  from student ;

select st.age,st.name from student st;#完整形式select student.age,student.name from student ;

show tables;#查询数据库里的所有表格

desc student; #显示student表格中所有字段信息。

/*分组 group by

score 表里的英语成绩的平均分

score 表里的各科成绩的平均分

score 表里的各科成绩的平均分并把平均分按照降序排列

score表中各科成绩总分大于500的科目及总和

将每个科目总分在411分以上的科目求出,并按照求出的科目总分进行升序排列

score 表中的每个人的成绩的平均分

*/

select * from score

select avg(score) from score where subject ='英语';

select  subject,avg(score) from score group by subject;

select  subject,avg(score) from score group by subject order by avg (score) desc;

Select  name from score group by name having min(score)>80

查询每科成绩都大于80的学生姓名

select  subject,sum(score) from score group by subject having sum(score)>500 ;

select subject,sum(score) from score group by subject having sum(score)>411 order by sum(score) asc ;

select name, avg(score) from score group by name;

use test;

/*创建表格(选修)

整形 INT  浮点型 float  

主键 不允许重复也不能为空 primary key 跟到需要定义的主键的后面  会显示一个小钥匙

插入数据:一行一行的更新

语法:insert into 表格名(字段1,字段2,字段3values'1','2','3')

*/

create table wwww( id char(4) primary key ,name char(12),sex char(4) )

create table 表名( 字段1  字符类型(长度), 字段2 字符类型(长度) ....)

insert into wwww(id,name,sex)values('001','张飞','');

Insert  into wwww  values('002','小乔','');如果插入的数据是表中的所有数据时,可省略前面字段的输入,按照原表字段顺序输入数据即可

Insert  into wwww(id,mame) values('003','大乔');sex字段 对应行为空值

create table wwew (name char(30),age int ,date_time char(30))

select *from wwew

insert into wwew values ('haode','18',NOW()) 注意now所在字段的字符类型及长度至少19

Now插入当前时间

/*更新表格

语法:update 表格名 set 字段1='新值',字段2='新值'  where;

注意!!!更新时一定要加where条件 否则会使整个字段更改

*/

update wwww set sex='' where id='001';

update wwww set sex='' ,name='张飞'  where  id='001';

/* 删除部分数据

where删除时一定要加where条件 否则会使整个表格删除。

使用场景不同delete 适用删除部分数据 truncate 清空表内数据 drop 表都不要了

删除速度不同 drop>truncate>DELETE

*/

delete from wwww where id=002 ;

Delete from 表名 where 字段=‘’

delete from student where id not in (select id from (select name,id from student GROUP BY name) ss);

truncate table LI# 慎重!!!清空表数据,速度很快,保留结构

drop table lll;#慎重!!!删除表,包括内容与结构

#创建视图

create view stu000 as select * from student where sex='';

#Create view 视图名 as select 字段 from 表格名 where 条件

Create view 视图名 as 需要生成表格的内容

视图:虚拟的表格,具有和物理表格相同的功能,可以对视图进行增删改查,对于视图的修改不会影响原表,但对原表的修改会影响视图

select *from student;

select * from score;

/*

多表联查

条件 两个表至少有一个字段相同(无论内外接)

join 内连接 把两个表格匹配条件相同的输出 ;

左外连接

以左表为基础,将左表全部输出,右表中输出与左表匹配的,不匹配的以空值形式输出

右外链接

以右表为基础,将右表全部输出,左表中输出与右表匹配的,不匹配的以空值形式输出

from后连接的第一个表始终显示在最左面(无论左右连接)

*/

select * from student join score on student .name =score.name;

select student.name from student join score on student .name =score.name;

select * from student join score on student .name =score.name and student.name='曹操';

select * from student join score on student .name =score.name where student.name='曹操';

select * from student  inner join score on student .name =score.name;#inner join join的完整写法

select * from student st join score sc on st .name =sc.name; #重命名

#左外连接

select * from student  left join score on student.name = score.name;

select * from student  left outer join score on student .name =score.name;#完整写法

#右外连接

select * from student  right outer join score on student .name =score.name;

/*

三表联查 找到一个中间表使,这个表分别至少有一个字段和另外两个表相同

只要有表格满足中间表格,可以进行很多表联查

*/

select * from student st  right outer join subject su on st.name =su.name

join score sc on su .subject =sc.subject;

select * from student;

select * from subject;

select * from score;

select * from student right join subject on student.name=subject.name join score on subject.subject=score.subject;

字段的增删改

select *from c111

alter table cat add column user_120 char(110);

#alter table 表格名 add  column新的字段 字符类型(定义长度)添加新的字段

alter table cat modify column user_tel int;

#alter table 表格名 modify  需改字段 新的字符类型(长度) 修改字段字符类型

alter table cat change column user_sex  sex char(20);

#alter table 表格名 change column 字段原名 字段新名 数据类型(长度))   修改字段

alter table cat drop column user_120; 

alter table  表名 drop column  要删除的字段          删除字段

alter table cat rename to catt;   

alter table 原表名 rename  to 新表名  文件重命名

Select *  from bao1,exelf;#拼接两个字段无关联的两个表格 笛卡尔积(如果表1有三行数据,表2有四行数据,则拼接后的表格有12行数据。)

/*

将时间格式化

筛选时间在某年,或是某月的

Y大写,月m,日d小写

*/

select *from mt;   

对时间格式化 MySQL数据库专用

year month day

select date_format(create_time,'%Y')from mt;

Select date_format(时间所在字段,%Y)from 表名

select date_format(create_time,'%Y_%m_%d')from mt;

select date_format(create_time,'%m')from mt;

select  * from mt  where date_format(create_time,'%Y')=2018;#select 字段 from 表格名 where date_format(时间所在字段,'%Y')=年份

select  * from mt  where

date_format(create_time,'%Y')=2018 and date_format(create_time,'%m')='04';   

select id+100 from mt;

select 10+12 from mt;

select id+status from mt;

#事务:是数据库中的一组逻辑操作,所有的操作要么全部成功要么全部失败

select * from account;

start  transaction;

update account set account=account+5000 where name='小白';

update account set account=account-5000 where name='小明';

commit;

#查询数据库正在执行的事务

select * from information_schema.innodb_trx;

      Scorma  /'skiːmə/ 概要 innodb/ in  No  d   b /

#查看数据库的执行时间

· show  profiles;/'prəufail/

#查看数据库当前执行的sql

show PROCESSLIST;  /  普柔赛斯 利斯特 /

/*

子查询 就是嵌套 一个SQL语句里面还有一个SQL语句。

子查询作为条件使用时无需重命名。

子查询作为一个表格使用时需要对子查询进行临时重命名。

*/

select count(name) from (select name from student_transcript  group by name  having min(score)>80 )ss ;

#一张表A 中有idhome两个字段查询出name 重复的所有数据

select * from a where name in (select name from  a  group by name  having count(name)>1 )

select count(distinct name) from student where age>40;

select  count(name) from (select age,name from student  group by name having age>40)cc;

select count(name) from student  where name in (select name from student where age>40 group by name);

#统计有多少人年龄在40岁以上

SQL server 数据库部分语法

use test ;

select * from student ;

select  top 3  *  from student ; 123

select  top 3  *  from student where id not in( select top 3 id from student );456

select * from (select top 3 id from student ) mm ;

select top 4 * from student where id not in(select top 3 id from student where id not in( select top 3 id from student )); 1237 四行

Oracle数据库

/*查询 表格字段信息 按住ctel 键 然后把鼠标放在表名上,左击*/

-- 单行注释

select* from student;

select * from student where rownum<=3;       /ne mu /

显示前三行

显示四到六行

select * from student where rownum<=3 and id not in (select id from student where rownum<=3);

select * from student where id not in (select id from student where rownum<=3)and rownum<=3;

select rownum t ,id,name,sex,tel,home from student st ;

select * from (select rownum t ,id,name,sex,tel,home  from student) m where m.t>=4 and m.t<=6;

--数据的增删改都需要一个commit;或者点一下提交commit按钮,如果删改过后没有提交可能会导致表锁,不能进行操作了

update student set  age='40' where id='12';

commit;

insert into studentid,name,age) values('12','高兴''20');

commit;

select *from student for update;对表进行更新

--对时间格式化Oracle

select * from scott.emp;

select to_char(hiredate,'yyyy')年份 from scott.emp;

select to_char(hiredate,'MM')月份 from scott.emp;

select to_char(hiredate,'dd')from scott.emp;

select to_char(hiredate,'yyyyMMdd')日期 from Scottemp

select *from 表名 where to_char(时间所在字段,'yyyy')='1981';

select sysdate from dual;--查询系统时间

--dualOracle数据库里的一个虚拟表格 只有一行数据

select to_char(sysdate,'yyyy_MM_dd')from dual;

猜你喜欢

转载自www.cnblogs.com/panda-yichen/p/10489309.html