前言

大家好,我是 Vic,今天给大家带来讲解SQL数据库语句的概述,希望你们喜欢

数据库语句

create database teach;
use teach;
create table `teach`.`producttype`( `pt_id` int not null auto_increment primary key, `pt_name' varchar(20) not null unique);
create table `teach`.`client`(`cl_id` char(4) not null primary key, `cl_name` varchar(20) not null, `cl_type` char(6) not null, `cl_guimo` char(2) not null, `cl_tel` varchar(15) not null, `cl_duanjiao` bit(2) not null);
create table if not exists product (pr_id int auto_increment primary key not null);
alter table product add pr_typeid int not null;
desc product;
insert into client(cl_id,cl_name,cl_type,cl_guimo,cl_tel,cl_duanjiao) values (1,'vic','hh','xiao',34455);
//
create table `teach`.`orders`( `or_id` int not null auto_increment primary key, `cl_id` char(4) not null, `pr_id` char(4) not null, `or_price` int not null, `or_num` int not null, `or_date` datetime not null);
select * from producttype;
update producttype set pt_name='休闲食品' where pt_id=2;
delete from producttype where pt_id=4;
select cl_id,cl_name,cl_guimo from client where cl_guimo = '大'|| '小';
select * from orders where or_date = 2010;
select cl_id '编号' ,cl_name '姓名' from client order by cl_id desc;
select * from client where cl_name like '%c';
select pr_num from product group by pr_typeid;
select pr_id,or_num from orders where or_num > 2 group by pr_id;
select * from orders group by cl_id;
select * from client where cl_duanjiao = 1 && cl_guimo = '小' group by cl_id order by cl_id desc;
select or_id,cl_name,pr_name,or_price,or_num,or_date from client crass join orders join product;
select * from product crass join producttype join orders where producttype = '食品';
use information_schema;
show tables;
desc tables;
select table_name from tables;
create database teach;
use teach;
create table if not exists characters(id int auto_increment primary key,name varchar(20) not null, description text);
create table if not exists access(id int auto_increment primary key,name varchar(2) not null, description text);
create table if not exists c_a(cid int not null,aid int not null, primary key (cid, aid),foreign key(cid) references characters(id),foreign key (aid) references access(id));
alter table c_a drop foreign key c_a_ibfk_1;
create table if not exists c_b(cid int not null references characters(id),aid int not null,primary key(cid, aid),foreign key(aid) references access(id));
alter table accesses add username varchar(20) not null;alter table accesses modify mame varchar(30) not null;alter table characters drop mane;alter table accesses modify description varchar(50);  // 修改rename table acc to accesses;alter table accesses rename acc;
create table if not exists characters(id int auto_increment primary key, name varchar(20) not null, description text);create table if not exists access (id int auto_increment primary key, name varchar(2) not null, description text);create table if not exists  c_a(cid int not null  , aid int not null , primary key(cid, aid), foreign key(cid) references characters(id) , foreign key(aid) references access(id) ) ;create table if not exists  c_b(cid int not null oreign key(cid) references characters(id) , aid int not null , primary key(cid, aid), foreign key(aid) references access(id) ) ;alter table access modify description varchar(50);
select a.id ,a.name from characters a;create table if not exists employess(id int auto_increment primary key, first_name varchar(10) not null,last_name varchar(20),salary float);insert into employees (first_name,last_name,salary)values('junx','zheng',1000),('ting','xue',1300);select last_name,salary,salary*12 sum from employees;//select last_name || job_id from employees;select last_name 'fname' from employees; //创建名insert into employees(first_name, last_name, salary)values('wang','guang',1000);select distinct salary , id from employees;insert into employees value (4,'ting','cue',1300);select distinct first_name, last_name, salary from employees;select * from employees where salary > 1200;select * from employess first_name like '%j%';select * from employess where salary in (1200,100);select * from employess where salary is null;
select lower('aBx');select upper('polill');select length('lojol');select char_length('lsjlf');select replace('zzjjjp','zj','hello');select substring('zjxx',2,6);select curdate();select curtime();select now();select minute('15:34:21');select monthname('2017-4-1');select date_format('2009-10-04 22:23:00','%w %m %y');select database();select user();select version();select inet_aton('192.168.0.1');select password('adlfllsf');
SELECT * FROM pet WHERE name LIKE 'b%';为了找出以“b”开头的名字,使用“^”匹配名字的开始:
SELECT * FROM pet WHERE name REGEXP '^b';select * from pet where name regexp binary '^b';为了找出以“fy”结尾的名字,使用“$”匹配名字的结尾:
select * from pet where name regexp 'fy$';为了找出包含一个“w”的名字,使用以下查询:
SELECT * FROM pet WHERE name REGEXP 'w';SELECT * FROM pet WHERE name REGEXP '^.....$';SELECT * FROM pet WHERE name REGEXP '^.{5}$';
SELECT * FROM shop;列的最大值
SELECT MAX(article) AS article FROM shop;找出最贵物品的编号、销售商和价格。
SELECT article, dealer, price
FROM   shop
WHERE  price=(SELECT MAX(price) FROM shop);SELECT article, dealer, price
FROM shop
ORDER BY price DESC
LIMIT 1;任务:每项物品的的最高价格是多少?
SELECT article, MAX(price) AS price
FROM   shop
GROUP BY article;
(1)select s#(学号),SName(学生名字) from S where Age < 17 and sex='女生';(2)select C.C#(课程号),Cname(课程名) from S,SC,C where S.S#=SC.S# and SC.C#=C.C# and sex='男生';(3)select T.T#,TName from S,SC,C,T where S.S#=SC.S# and SC.C#=C.C#
and C.T#=T.T# and sex='男生';(4)select S# from SC group by S# having count(*)>1;(5)select distinct X.C# from SC as X, SC as Y where X.S#='S2' and Y.S#='S4' and X.C#=Y.C#;(6)select C# from C where C# not in(select C# from S,SC where S.S#=SC.S# and SName='wang');(7)select C#,Cname from C where not exists (select * from S where not exists(select * from SC where C.C#=SC.C# and SC.S#=S.S#));(8)select distinct S# from SC as X where not exists (select * from C,T where C.T#=T.T# and TName='liu' and not exists (select * from SC as Y where Y.S#=X.S# and Y.C#=C.C#));(9)select count(distinct C#) from SC;(10)select avg(age) from S where sex='女生' and S# in(select S# from SC where C#='C4');
脏读:到达广州结果走到一半
不可重复读:一样的语句,可能被人复读
幻读:同样两条语句,你在用别人也在用。
设有教学数据库中有4个关系
教师关系 T(T#,TName, Title)工号,名字,职称
课程关系 C(C#,Cname,T#)课程号,课程名,任课老师工号
学生关系S(S#,SName,Age,sex)
选课关系 SC(S#,c#,Score)(1)检索年龄小于17岁的女学生的学号和姓名Select s#,sname from S where age<17 and sex=’f’;
(2)检索男学生所学课程的课程号和课程名Select c#, cname from c where c# in (select distinct b.c# from s a inner join sc b on a.s#=b.s# where a.sex=’m’);
(3)检索男学生所学课程的任课老师的工号和姓名Select T.T#, T.Tname from T inner join C on T.T#=C.T# where C.C# in (select distinct b.c# from s a inner join sc b on a.s#=b.s# where a.sex=’m’);
(4)检索至少选修两门课程的学生学号
Select s# from sc  group by s# having count()>=2;
检索至少有学号为 S2和S4的学生选修的课程的课程号Select c# from sc where c# in (select c# from sc where s#=’s4’)  and c# in (select c# from sc where s#=’s2’);
(5)检索wang同学不学的课程的课程号
select distinct c# from sc where c# not in (select c# from s inner join sc on s.s#=sc.s# where s.sname=’wang’);
(6)检索全部学生都选修的课程的课程号与课程名Select c# from sc group by c# having count()=(Select count() from s);
(7)检索选修课程包含liu老师所授全部课程的学生学号select distinct sc.s# from T inner join c inner join sc on T.T#=c.T# and c.c#=sc.c# where T.Tname=’liu’;
(8)统计有学生选修的课程门数
Select count() from c where c# in (select distinct c# from sc);
(9)求选修C4课程的女学生的平均年龄
Select avg(age) from s where sex=’f’ and s# in (select s# from sc where c#=’c4’);
(10)求liu老师所授每门课程的平均成绩
Select avg(score) from sc where c# in (select c# from c inner join t on c.t#=t.t# where t.name=’liu’);
(11)统计选修每门课程的学生人数(超过10人的课程才统计)。要求显示课程号和人数,查询结果按照人数降序排列,诺人数相同,则按照课程号升序排列。
Select  c#, count()  number from sc group by c# having count()>=10 order by number desc, c# asc;
(12)检索学号比Wang同学大,而年龄比他小的同学的学生姓名。Select sname from s  where s#>(select s# from s where sname=’wang’) and age<(select age from s where sname=’wang’);
(13)在表SC中检索成绩为空值的学生的学号和课程号
(14)检索姓名以 L开头的所有学生的学号和课程号Select s.sname, sc.c# from s inner join sc on s.s#=sc.s# where s.sname like ‘L%’;
(15)求年龄大于女同学平均年龄的男同学的姓名和年龄
Select sname,age from s where sex=’m’ and age>(select avg(age) from s where sex=’f’);
声明光标
DECLARE cursor_name CURSOR FOR select_statement光标OPEN语句
OPEN cursor_name光标FETCH语句
FETCH cursor_name INTO var_name [, var_name] ...光标CLOSE语句
CLOSE cursor_name

数据库技术

示意图
示意图

数据库技术

create database teach;
use teach;
(1)
CREATE TABLE `teach`.`producttype` ( `pt_id` INT NOT NULL AUTO_INCREMENT  primary key, `pt_name` VARCHAR(20) NOT NULL unique );
(2)
CREATE TABLE `teach`.`client` ( `cl_id` CHAR(4) NOT NULL primary key, `cl_name` VARCHAR(20) NOT NULL , `cl_type` CHAR(6) NOT NULL , `cl_guimo` CHAR(2) NOT NULL , `cl_tel` VARCHAR(15) NOT NULL , `cl_duanjiao` BIT(2) NOT NULL );
(3)
create table if not exists product(pr_id int auto_increment primary key not null);
alter table product add pr_typeid int not null;
desc product;
insert into client(cl_id,cl_name,cl_type,cl_guimo,cl_tel,cl_duanjiao)values(1,'小 明','经销商','大',88810615,0);
insert into client(cl_id,cl_name,cl_type,cl_guimo,cl_tel,cl_duanjiao)values(2,'小 红','经销商','中',88815615,0);
insert into client(cl_id,cl_name,cl_type,cl_guimo,cl_tel,cl_duanjiao)values(3,'小 微','零售商','小',88825615,0);
(4)
CREATE TABLE `teach`.`orders` ( `or_id` INT NOT NULL AUTO_INCREMENT primary key, `cl_id` CHAR(4) NOT NULL , `pr_id` CHAR(4) NOT NULL , `or_price` INT NOT NULL , `or_num` INT NOT NULL , `or_date` DATETIME NOT NULL );
项目3
insert into producttype(pt_id,pt_name)values(1,'洗里理日用品'),(2,'食品'),(3,'家用电器'),(4,'肉食');
select * from producttype;
update producttype set pt_name='休闲食品' where pt_id=2;
delete from producttype where pt_id=4;
项目1
(1)select cl_id,cl_name,cl_guimo from client where cl_guimo = '大'|| '小';(2) select * from orders where or_date = 2010;(3) select cl_id '编号' ,cl_name '姓名' from client order by cl_id desc;(4) select pr_typeid from product;select distinct pr_typeid from product;(5) select * from client where cl_name like '%c';项目2
(1) select pr_num from product group by pr_typeid;(2) select pr_id,or_num from orders where or_num > 2 group by pr_id;(3)select * from orders group by cl_id;项目3
(1)  select * from client where cl_duanjiao = 1 && cl_guimo = '小' group by cl_id order by cl_id desc;(2)  select or_id,cl_name,pr_name,or_price,or_num,or_date from client crass join orders join product;(3)  select * from product crass join producttype join orders where producttype = '食品';

关系数据完整性是对关系的某种约束条件

  • 实体完整性:对主码进行限制
  • 参照完整性:对外码进行限制
  • 用户定义完整性 :对具体数据进行限制

函数依赖: R(X,Y)

(1)完全函数依赖:(学号、课程号) →f 成绩
(2)部分函数依赖 :(学号、课程号) →p 姓名
(3)传递函数依赖 :学号→所属系号,所属系号→宿舍楼号,学号→t宿舍楼号

关系数据库

关系数据库是因为采用关系模型而得名,它是目前数据库应用中的主流技术。

关系数据库的出现标志着数据库技术走向成熟。

关系数据库的特点

(1)数据结构简单。
(2)功能强。
(3)使用方便。
(4)数据独立性高。

关系模型的基本术语

(1)关系。
一个关系对应一个二维表,二维表名就是关系名。
(2)属性及值域。
二维表中的列称为关系的属性。
属性值的取值范围称为值域,每一个属性对应一个值域,不同属性的值域可以相同。
(3)关系模式。
二维表中的行定义、记录的类型,即对关系的描述称为关系模式。
(4)元组。
每一条记录的值称为关系的一个元组。
(5)键。
由一个或多个属性组成。

关系模式

关系模式是对关系的描述。

关系的完整性

有3类完整性约束:实体完整性、参照完整性和用户定义的完整性。

SQL的主要功能

(1)数据定义功能。
(2)数据操纵功能。
(3)数据控制功能。

数据库由3种类型组成:系统数据库、用户数据库数和数据库快照。

系统数据库

master 系统信息数据库
model 模板信息数据库
msdb 代理信息数据库
tempdb 临时信息数据库
resource 资源信息数据库

用户数据库

用户数据库包括用户自定义的数据库和系统的示例数据库。

数据库快照

数据库快照是一个数据库的只读副本和静态视图,它是数据库所有数据的映射,由快照被执行的时间点来决定它的内容。

数据库的储存结构

逻辑储存结构

数据库的逻辑储存结构是以用户观点看到的数据库的体系结构。

物理存储结构

数据库的物理存储结构是以数据库设计者观点看到的数据库的体系结构。

数据库文件划分为两类:数据文件和日志文件。

文件组是数据文件的逻辑集合。

如果觉得不错,那就点个赞吧!❤️

总结

  • 本文讲了讲解SQL数据库语句,如果您还有更好地理解,欢迎沟通
  • 定位:分享 Android&Java知识点,有兴趣可以继续关注

讲解SQL数据库语句相关推荐

  1. SQL数据库语句中escape的用法及含义

    SQL数据库语句中escape的用法及含义 escape是用来转译的 例如: 数据库中有个表 A字段abc 中存du了字符'%',那么我查询的时候如果只想查询第二位是 '%'的记录就需要进行模糊查询, ...

  2. 画图讲解SQL join 语句

    我认为 Ligaya Turmelle 的关于SQL联合(join)语句的帖子对于新手开发者来说是份很好的材料.SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看是很自然而然的.不过正如在她的帖子 ...

  3. sql 数据库语句中日期相减

    sql中两个日期相减 1.相差天数 select trunc(sysdate,'yyyy')-to_date('2009-01-04','yyyy-mm-dd') from dual; 2.相差月数 ...

  4. ASP.NET动态网站制作(15)-- SQL数据库(1)

    前言:数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,用户可以对文件中的数据进行增.删.改.查.数据库有很多种类型,从简单的存储有各种数据的表格到能都进行海量数据存储的大型数据库 ...

  5. MFC连接SQL数据库操作

    要操作SQL数据库要经过以下几个步骤: 0.建立数据库 1.配置数据源 2.数据库的连接 3.数据库的操作 4.类资料 [1]配置数据源 配置数据源一般有两种方法一种是通过注册表,一种是通过SQLCo ...

  6. sql 数据库前两列值乘_Sql语句常用关键字

    最近接触sql比较多,发现自己已经遗忘的也差不多,要用到的时候迟迟拿不出来,今天开始会在知乎上纪录一些sql语句学习的内容,内容重在说明查询语句的用法. 一.sql查询语句的初始介绍 1.查询语句的一 ...

  7. python调用sql数据库_Python3操作SQL Server数据库(实例讲解)

    1.前言 前面学完了SQL Server的基本语法,接下来学习如何在程序中使用sql,毕竟不能在程序中使用的话,实用性就不那么大了. 2.最基本的SQL查询语句 python是使用pymssql这个模 ...

  8. R语言构建仿真数据库(sqlite)并使用dplyr语法和SQL语法查询数据库、将dplyr语法查询语句翻译为SQL查询语句

    R语言构建仿真数据库(sqlite)并使用dplyr语法和SQL语法查询数据库.将dplyr语法查询语句翻译为SQL查询语句 目录

  9. oracle 哦【的【,清晰讲解SQL语句中的外连接,通用于Mysql和Oracle,全是干货哦

    清晰讲解SQL语句中的外连接,通用于Mysql和Oracle,全是干货哦 直入主题: 我们做一个操作,将员工SCOTT的部门去掉,再次通过内连接查看数据,看看会产生什么现象? 使用内连接,查询数据 问 ...

最新文章

  1. 比特币耶稣Roger Ver赠送中国著名经济学家巴曙松1枚比特币现金BCH
  2. (大纲)三小时学会openCV
  3. 给书配代码-电力经济调度(1):基于拉格朗日及运筹规划方法的经济调度算法
  4. 【mysql】str_to_date()字符串转化为日期类型
  5. oracle存储过程无效字符_Oracle中无效存储过程的重新编译方法
  6. Python机器学习库scikit-learn实践
  7. 程序员面试需要出示身份证和毕业证原件吗?
  8. 通信维修专用电源_万可PRO 2电源 | 开拓性通信功能,自信迈入数字化时代
  9. eclipse如何装php插件,Eclipse PHP插件(PHPEclipse)安装与配置图解
  10. Geodatabase
  11. 历年真题软件设计师下午考试题汇分析与技巧
  12. 电子电路计算机仿真应用,电子电路计算机仿真技术
  13. Linux 通配符 与 正则表达式 的区别与详解
  14. 问卷调查报告html,问卷调查报告格式优秀范文
  15. RSRP RSRQ RSSI SNR的含义和区别
  16. svn 添加忽略后解除被忽略的文件或文件夹
  17. Unity VR(PicoVR)
  18. 设计一个类代表二维空间的一个圆。_绝了!这是什么神仙花园设计! | 2020世界花园大会...
  19. 游戏2048源代码 - C语言控制台界面版
  20. 基于安卓android studio 的 菜谱食谱APP设计

热门文章

  1. 【转载】HBA卡知识介绍
  2. 建立私有CA实现证书申请颁发
  3. 快速云:了解混合云连接最佳做法
  4. 一文总结十大经典排序算法(思维导图 + 动图演示 + 代码实现 C/C++/Python + 致命吐槽)
  5. 嘿,这里有你想要的Javascript本源
  6. ubuntu16.04 笔记本 安装双显卡驱动GTX960M 可快捷切换
  7. 北京地铁背景音乐曲目
  8. js清除浏览器缓存的几种方法
  9. 牧场上的草泥马(游荡的奶牛)
  10. win7 计算机名称 ip6,Win7系统为什么会出现IPV6无网络访问权限?