数据库查询基础语句

#单行注释

/*

多行注释

*/

 

#语句建表

/*

create table 表名(

字段名1 字段类型 约束1 约束2,

字段名2 字段类型,

...

字段名n 字段类型);

*/

 

 

 

#创建学生表---xsb

/*

学号(主键),姓名(非空),性别(枚举),年龄,班级(默认188)

籍贯,身份证号(唯一),注册日期(默认系统时间)

*/

create table xsb(

xh char(10) primary key,

xm varchar(20) not null,

xb enum('男','女'),

nl int,

bj char(5) default '189',

jg varchar(100),

shzh char(18) unique,

zcrq timestamp default current_timestamp

 

 

);

#创建课程表

#课程号(主键)  课程名(非空)

create table kcb(

kch char(10) primary key,

kcm varchar(20) not null

);

 

#创建成绩表

#学号(外键) 课程号(外键)  成绩(非空)

#设置学号和课程号作为联合主键

create table cjb(

xh char(10),

kch char(10),

foreign key(xh) references xsb(xh),

foreign key(kch) references kcb(kch),

cj int not null,

primary key(xh,kch)

);

 

#增加一个自增列作为主键

create table cjb1(

xh char(10),

kch char(10),

foreign key(xh) references xsb(xh),

foreign key(kch) references kcb(kch),

cj int not null,

id int primary key auto_increment

);

 

 

 

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

 

 

#单表查询

/*

select 查询的内容

from 从哪里查询

where 查询条件

*/

 

 

/*

写SQL分析步骤

1.确定数据的来源,从哪张表中查询数据--from

2.明确查询的内容,需要查询的字段有哪些--select

3.明确满足哪些要求的数据,查询条件是什么--where

4.调整顺序,符合单表查询的语法

 

*/

#查询学生表中的全部信息

select * from xsb;

#查询课程表中的全部信息

select * from kcb;

#查询成绩表中所有信息

select * from cjb;

 

#查询李晨的全部信息

/*

1.明确数据来源:xsb

2.查询的内容:全部信息 *

3.查询的条件:李晨---xm=‘李晨‘

*/

select *

from xsb

where xm='李晨';

 

#查询学生表中所有男生的信息

select *

from xsb

where xb='男';

 

#查询学生表中年龄是23的全部学生信息

select *

from xsb

where nl=23;

 

#查询学生表中年龄大于23的全部学生信息

select *

from xsb

where nl>23;

 

 

#查询学生表中年龄小于20的全部学生信息

select *

from xsb

where nl<20;

 

#查询学生表中籍贯不是北京的全部学生信息

select *

from xsb

where jg!='北京';

 

select *

from xsb

where jg<>'北京';

 

 

#查询成绩表中所有的及格信息

select *

from cjb

where cj>=60;

 

 

#查询张三或李四的全部信息

select *

from xsb

where xm='张三' or xm='李四';

 

#查询189班所有女生的全部信息

select *

from xsb

where bj=189 and xb='女';

 

 

#查询18岁的张三或李四的信息

select *

from xsb

where nl=18 and xm='张三' or xm='李四';

 

#年龄都需要满足18岁

select *

from xsb

where nl=18 and xm='张三' or xm='李四' and nl=18;

#3*2+5*2

select *

from xsb

where nl=18 and (xm='张三' or xm='李四')

 

#查询来自北京,上海和广东的学生全部信息

select *

from xsb

 where jg='北京'or jg='上海'or jg='广东';

 

select *

from xsb

where jg in ('北京','上海','广东');

 

#查询不是北京,上海或广东的学生全部信息

select *

from xsb

where jg!='北京'and jg!='上海'and jg!='广东';

 

select *

from xsb

where jg not in('北京','上海','广东')

 

#查询年龄在20到25之间的学生信息

select *

from xsb

where nl>=20 and nl<=25;

 

select *

from xsb

where nl between 20 and 25

 

#查询成绩在70到90之间的成绩信息

select *

from cjb

where cj>70 and cj<90;

 

#包含70和90

select *

from cjb

where cj between 70 and 90;

 

#查询未填写籍贯的全部信息

select *

from xsb

where jg is null

 

#查询填写籍贯的全部信息

select *

from xsb

where jg is not null;

 

#查询学生姓名中包含‘冰’的学生信息

select *

from xsb

where xm like '%冰%';

 

#查询学生姓名中以'冰'字开头的学生信息

select *

from xsb

where xm like '冰%';

 

#查询学生姓名中以'冰'字结尾的学生信息

select *

from xsb

where xm like '%冰';

 

#查询学生姓名中第二个字是'冰'的学生信息

select *

from xsb

where xm like '_冰%';

 

#查询学生表中所有李姓的学生信息

select *

from xsb

where xm like '李%'

 

#查询学生表中姓‘王’,以‘强’字结尾的三字学生信息

select *

from xsb

where xm like '王_强';

 

#查询张三的年龄

 

select nl

from xsb

where xm='张三';

 

#查询李四的身份证号和籍贯

select sfzh as'身份证号',jg as'籍贯'

from xsb

where xm='李四';

 

select sfzh'身份证号',jg'籍贯'

from xsb

where xm='李四';

 

#查询学生表中的学生来自哪些省

select distinct jg

from xsb

 

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值