前言

大家好,我是 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数据库语句

    1.说明:复制表(只复制结构,源表名:a 新表名:b) (Access可用)法一:select * into b from a where 1 <>1法二:select top 0 * i ...

  2. 常用的SQL数据库语句总结

    1as 的用处 as可以对表和列取别名 在开发过程中经常遇到开始给某一个的字段去field1的名称,但后来有感觉field1字段指定不确切,于是又把此字段改成了field2,由于开始认 为field1 ...

  3. SQL数据库语句练习题目

    一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的表(一)~表( ...

  4. 批量分离SQL数据库语句

    --sp_helpdb--查看可用数据库 declare @name sysname, @sql nvarchar(4000) DECLARE roy CURSOR FOR --排除不分离的数据库名就 ...

  5. SQL数据库语句

    on xxx --主文件 ( name=‘xxxx’, fliename='里面写文件放的路径\xxxx.mdf', size=xxMB, filegrowth=xxMB, maxsize=xxMB ...

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

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

  7. SQL数据库学习,常用语句查询大全

    数据库学习 sql server数据库基本概念 使用文件保存数据存在几个缺点: 1.文件的安全性问题: 2.文件不利于查询和对数据的管理: 3.文件不利于存放海量数据 4.文件在程序中控制不方便. 数 ...

  8. WordPress 常用数据库SQL查询语句大全

    在使用WordPress的过程中,我们少不了要对数据库进行修改操作,比如,更换域名.修改附件目录.批量修改文章内容等等.这个时候,使用SQL查询语句可以大大简化我们的工作量. 关于如何操作SQL查询语 ...

  9. C# 链接Sql和Access数据库语句

    1.sql数据库: 1.1.链接数据语句:server=localhost;database=Data; uid=sa;pwd=123; 或 Data Source=localhost;DataBas ...

随机推荐

  1. 阿里云最新Maven仓库地址 从此 我的maven依赖下载666~

    配置指南 maven配置指南 打开maven的配置文件(windows机器一般在maven安装目录的conf/settings.xml),在<mirrors></mirrors> ...

  2. Flutter 实现图片裁剪

    实现原理很简单 ,自己绘制一个裁剪框, 根据手势 选择到适合的位置 ,然后将选中的区域绘制到一个新的图片上,从而完成裁剪 裁剪框的绘制  这里我是根据点来连线的  因为每个点上会绘制一个拉伸的标识符 ...

  3. jq + 面向对象实现拼图游戏

    jq + 面向对象实现拼图游戏 知识点 拖拽事件 es6面向对象 jquery事件 效果图 html: <div class="wraper"> <div cla ...

  4. 深入理解React 组件状态(State)

    React 的核心思想是组件化的思想,应用由组件搭建而成,而组件中最重要的概念是State(状态),State是一个组件的UI数据模型,是组件渲染时的数据依据. 一. 如何定义State 定义一个合适 ...

  5. ORACLE主键ID的生成

    转自:https://blog.csdn.net/yh_zeng2/article/details/83477880 一般常用的方法有两种,使用Sequence和使用SYS_GUID(); 方法一  ...

  6. iOS 10.0前的Notification推送

    前言 推送为远程推送,一般由苹果APNS服务器发送给苹果设备(iPhone,iPad) 推送分在前台和后台.在前台时 用户可以在application 的代理回调接口中做相应处理:在后台时 系统会全权 ...

  7. iOS应用状态保存和恢复

    当应用被后台Kill掉的时候希望从后台返回的时候显示进入后台之前的内容 在Appdelegate中设置 - (BOOL)application:(UIApplication *)application ...

  8. linux基础命令之1

    1.创建文件夹:mkdir 文件夹名称 2.创建文件:touch  文件名称 3.编辑文件:vi 文件名称 4.保存文件::wq

  9. The Essential Burp Suite

    OK   we have download teh burp suite .let's begin start the tool 1.if  we  want to use the total mem ...

  10. Wireshark 分析Linux SSh 远程登录延迟问题

    1.PuTTy远程登录延迟的分析 现象问题描述:在使用kali linux 的时候喜欢在后台运行而在Windows主机系统上安装PuTTY来实现远程登录 发现每次输入密码的时候会存在延迟10s的情况, ...