sql语句(已在Oracle中测试,之后有添加内容放在评论中)
1增
1.1【创建一张表】
create table 表名(列名 类型);
例:
create table stu(姓名 varchar2(25),性别 varchar2(25),出生日期 date);
create table newstu(name varchar2(25),sex varchar2(25),出生日期 date);
1.2【插入单行】
insert [into] <表名> (列名) values (列值)
例:
insert into stu (姓名,性别,出生日期) values ('张三','男',to_date('1993-1-1','yyyy-mm-dd'));
1.3【将现有表数据添加到一个已有表】
insert into <已有的新表> (列名) select <原表列名> from <原表名>
列名可以不同,但是对应列的类型必须相同。
例:
insert into newstu(name,sex,出生日期) select 姓名,性别,出生日期 from stu;
--insert into newstu(name,sex,出生日期) select *from stu; --也是可以的
1.4【直接拿现有表数据创建一个新表并填充】
create table <新表> as select *from <现有表>
例:
create table nnstu as select *from newstu;
1.5【使用union关键字合并数据进行插入多行】
insert <表名> (列名) select <列值> union select <列值>
例:
在newstu表中多插入几条数据:
insert into newstu(name,sex,出生日期) values('李四','女',to_date('1995-12-25','yyyy-mm-dd'));
insert into newstu(name,sex,出生日期) values('王五','男',to_date('1996-3-8','yyyy-mm-dd'));
向stu表中插入多行数据:
insert into stu(姓名,性别,出生日期)
select '李四','女',to_date('1995-12-25','yyyy-mm-dd') from newstu union
select '王五','男',to_date('1996-3-8','yyyy-mm-dd') from newstu;
1.6【添加约束】
例:主键(id):alter table <表名> add constraint con_<表名>_id_pk primary key(id) ;
--添加主键
alter table tbl_account add constraint con_tbl_account_id_pk primary key(id);
--添加非空
alter table tbl_account add constraint con_tbl_account_uname_ck check(username is not null);
--添加范围>
alter table tbl_account add constraint con_tbl_account_bal_ck check(balance>=0);
--添加范围in
alter table tbl_account add constraint con_tbl_account_gender_ck check(gender in ('男','女'));
--添加范围and
alter table tbl_account add constraint con_tbl_account_age_ck check(age >= 0 and age < 170);
--添加唯一
alter table tbl_account add constraint con_tbl_account_age_uk unique key(age);
--添加外键
alter table tbl_account add constraint con_tbl_account_age_fk foreign key(age) references 关联的表(关联的字段)
--添加索引
alter table cp_record add index index_cp_record_mem_id ( `mem_id` );
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2删
2.1【删除<满足条件的>行】
delete from <表名> [where <删除条件>]
例:
delete from stu where 姓名='张三';
2.2【删除所有行】
truncate table <表名>
例:
truncate table stu;
注意:删除表的所有行,但表的结构、列、约束、索引等不会被删除;不能用语有外建约束引用的表
2.3【删除整张表】
drop table <表名>
例:
drop table stu;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3改
update <表名> set <列名=更新值> [where <更新条件>]
例:
update newstu set name='胡八',sex='女' where name='张三';
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4查
4.1条件查询
4.1.1【按条件查询,按顺序展示】
select <列名> from <表名> [where <查询条件表达试>] [order by <排序的列名>[asc或desc]]
例:
原字段为:name,sex,出生日期,更改列名as可加可不加
--性别为女且按出生日期降序排序
select name 姓名,sex 性别,出生日期 from newstu where sex='女' order by 出生日期 desc
说明:升序为asc
4.1.2【查询空行】
例:
select name 姓名,sex 性别,出生日期 from newstu where 出生日期 is null;
说明:查询表newstu中出生日期为空的所有行,并显示所有列;SQL语句中用is null或者is not null来判断是否为空行
4.1.3【在查询中使用常量】
例:
select name 姓名,sex 性别,出生日期,'浙江' as 地址 from newstu;
说明:查询表newstu,显示name列,并添加地址列,其列值都为'浙江'
4.1.4【查询返回限制行数(关键字:top percent)】
例:
--oracle中,前两条数据
select name 姓名,sex 性别,出生日期 from newstu where rownum<=2;
--oracle中,前60%数据
select name 姓名,sex 性别,出生日期 from newstu where rownum<=(select count(*)*0.6 from newstu);
--mysql中,前两条数据
select top 2 name 姓名,sex 性别,出生日期 from newstu;
select *from newstu limit 2
--mysql中,前60%数据
select top 60 percent name from newstu;
4.2模糊查询
4.2.1【使用like进行模糊查询】
注意:like运算副只用于字符串,所以仅与char和varchar数据类型联合使用
例:
select *from newstu where name like '李%';
select *from newstu where name='李四' and age=22;
select *from newstu where age=20 or age=21 or age=22;
说明:查询显示表newstu中,name字段第一个字为赵的记录
4.2.2【使用between在某个范围内进行查询】
例:
select *from newstu where age between 20 and 22;
说明:查询显示表newstu中age在20到22之间的记录
4.2.3【使用in在列举值内进行查询】
例:
select *from newstu where name in('李四','胡八');
说明:查询表newstu中name值为李四或者胡八的记录
4.3.分组查询
4.3.1【使用group by进行分组查询】
例:
select sex,avg(age) as 平均年龄 from newstu group by sex;
说明:在newstu表中查询,展示按sex分组的sex和平均age字段,在这个条件下不能展示其他字段
4.3.2【使用having子句进行分组筛选】
例:
select sex,avg(age) as 平均年龄 from newstu group by sex having count(age)>2;
说明:接上面例子,显示分组后count(age)>2的行,由于where只能在没有分组时使用,分组后只能使用having来限制条件。
4.4.多表联接查询
4.4.1内联接
4.4.1.1【在where子句中指定联接条件】
例:
select s.name,a.are from newstu s,neware a where s.name=a.name;
4.4.1.2【在from子句中使用join…on】
例:
select s.name,a.are from newstu s inner join neware a on(s.name=a.name);
内联与外联的区别:内联是两个表在连接的条件下,所有的字段相同才显示。
4.4.2外联接
4.4.2.1【左外联接查询】
例:
select s.name,a.are from newstu s left join neware a
on s.name=a.name where a.are='江苏';
说明:查询出江苏地区的搜有人的姓名、所在地,newstu表的字段为name,sex,出生日期,age,neware表的字段为name,are。
4.4.2.2【右外联接查询】
例:
select s.name,a.are from newstu s right join neware a
on s.name=a.name where a.are='江苏';
说明:查询出江苏地区的搜有人的姓名、所在地,newstu表的字段为name,sex,出生日期,age,neware表的字段为name,are。
左关联与有关联的区别:左联显示左表所有内容,右表有就显示,没有就显示null;右联显示右表所有内容,左表有就显示,没有就显示null。
neware表中name字段的值如果在newstu表中的name字段是没有的,那么左关联时左边中没有的,但是右关联可以查出左表中没有的。
以4.2.1和4.2.2为例:
newstu表:
name sex 出生日期 age
胡八 女 1993-01-01 00:00:00 24
李四 女 1995-12-25 00:00:00 22
王五 男 1996-03-08 00:00:00 21
赵六 男 20
陈七 男 32
neware表:
name are
胡八 江苏
胡八 苏州
李四 宿迁
李四 江苏
张三 江苏
左关联:select s.name,a.are from newstu s left join neware a on s.name=a.name where a.are='江苏';
结果:name are
胡八 江苏
李四 江苏
右关联:select s.name,a.are from newstu s right join neware a on s.name=a.name where a.are='江苏';
结果:name are
胡八 江苏
李四 江苏
null 江苏
sql语句(已在Oracle中测试,之后有添加内容放在评论中)的更多相关文章
- 【转】SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误
SQL Server -- 已成功与服务器建立连接,但是在登录过程中发生错误 最近在VS2013上连接远程数据库时,突然连接不上,在跑MSTest下跑的时候,QTAgent32 crash.换成IIS ...
- 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】
问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...
- ABAP中SQL语句,指定索引(oracle)
①常用的两种方法: 1.指定使用全表扫描:%_HINTS ORACLE 'FULL(table_name)' 表示扫描整个表 2.指定索引:%_HINTS ORACLE 'INDEX("ta ...
- MFC中给控件添加变量,DoDataExchange中
DoDataExchange函数其实是一项数据动态绑定技术.比如你在写动态按钮过程中须对按钮添加变量时,怎么添加?控件类已经写好了,其变量是已经固定的.你要添加新的变量就要用到DoDataExchan ...
- angularjs中向html页面添加内容节点元素代码段的两种方法
第一种方式:原生JS向html页面添加内容节点元素代码段: <!DOCTYPE html> <html> <head> <meta charset=" ...
- SQL Server 已提交读快照 测试
1. 打开数据库 已提交读快照 选项 2. 数据库 已提交读快照 模式下的测试 a) 测试表 Test b) 开启事务1,更新数据C2 = '200'(未提交) BEGIN TRAN ' WHERE ...
- SQL语句技巧:查询存在一个表而不在另一个表中的数据记录
方法一(仅适用单个字段)使用 not in ,容易理解,效率低 select A.ID from A where A.ID not in (select ID from B) 方法二(适用多个字段匹配 ...
- sql语句,多个表之间,通过相同内容字段的联合查询
1 , select j.id, jt.Name, j.ApproveType , j.ProductCode, j.CustomerCo ...
- C语言中对于结构的定义一般放在.h中还是.c中
单文件使用的结构体放.c文件中 本模块内部使用结构体,放模块内部头文件中 供外部使用的结构体,放模块对外头文件中.
随机推荐
- day05 模块学习
目录 1.模块简介 2.collections模块常见方法 3.random模块 4.time模块 5.pickle模块 6.json模块 7.os模块 8.sys模块 9.正则表达式 10.re模块 ...
- log4j根据包名 日志输出到不同文件中 , service层无法输出日志问题
1. service 层因为要配置事务,使用了代理 <aop:config proxy-target-calss=''true"> <aop:pointcut id=&qu ...
- win10 caffe GPU环境搭建
一.准备 系统:win10 显卡:gtx1050Ti 前期的一些必要软件安装,包括python3.5.matlab2016.vs2015.git, 可参考:win10+vs2015编译caffe的cp ...
- SolrJ的使用
SolrJ的使用 1.添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- <ROS> NodeHandle句柄
作者:jack_20 原文:https://blog.csdn.net/jack_20/article/details/70746736 1.句柄可以让你通过构造函数指定命名空间 ros::NodeH ...
- 17款提高编码效率的CSS工具
摘要:作为WEB前端开发人员,你的工作可能很大一部分都在编写CSS代码,为了提高前端开发人员编写CSS代码的效率,编程文库从 网上搜集了17款可以提高你CSS代码效率的CSS工具,它们可以帮助你快速生 ...
- sybase解决死锁
解决Sybase数据库死锁的方法 https://blog.csdn.net/lileizhang/article/details/45816509
- DOM知识点总结
今天简单整理了一下js三部曲之DOM部分的内容,二话不说先上笔记: 1.什么是DOM? Document Object Model,即文档对象模型,它是让JavaScript能够操作html和xml的 ...
- myeclipse2018安装教程(附下载地址)
这两天在网上找myeclipse2018的安装包和破解工具,很分散,并且破解失败很多,现在总结下 myeclipse2018下载地址: https://pan.baidu.com/s/1NV7cAsN ...
- 一个小白用 PhotoView 引起的学习记录
缘由(可跳过) 作为一个开发小白,有着各种各样想实现的功能, 最近想实现一个图片查看的功能(有放大,缩小等功能),发现了 PhotoView 这个开源框架, 用了它,腰不疼,腿不酸了 ... 添加依赖 ...