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文件中 本模块内部使用结构体,放模块内部头文件中 供外部使用的结构体,放模块对外头文件中.
随机推荐
- C 标识符, 数据存储形式(原码,反码,补码)
一. 标识符 第一个字母必须是英文字母或下划线 二. 数据存储形式(补码存储) 最高位是符号位 ---- 0表示整数 ; 1 表示负数 1. 正数:原码 = 反码 = 补码 例子 : (10) 原码 ...
- IO练习
#IO操作 import time; fileObj = open('log.txt','a'); while(True): data = input('请输入要写入的内容 : '); if data ...
- locate中使用variant
利用locate构造多字段查询,采用variant很方便,简介如下, //构造查询多字段,例如'编号;姓名'形式 aLookField := FieldByName ('关键字1').AsStrin ...
- 我的第二本译作《精通OpenStack》上架啦:前言、目录和样章
1. 前言 今天,随着新功能和子项目的增加,OpenStack已成为一个不断扩展的大型开源项目.随着数以百计大型企业采用并不断为OpenStack生态系统做出贡献,OpenStack必将成为下一代私有 ...
- struct2depth 记录
把效果图放在前面 03.28 handle_motion False architecture simple joint_encoder False depth_normalization ...
- myeclipse2018安装教程(附下载地址)
这两天在网上找myeclipse2018的安装包和破解工具,很分散,并且破解失败很多,现在总结下 myeclipse2018下载地址: https://pan.baidu.com/s/1NV7cAsN ...
- [UE4]虚幻引擎的C++环境安装
一.一般使用VS2017开发 二.需要勾选“使用C++的游戏开发”
- [UE4]Spline
Spline和Spline Mesh的区别: 1.Spline Mesh是有实体表现的,Spline Mesh可以拉伸弯曲实体模型,Spline Mesh是具象. 2.Spline 只有曲线,没有实体 ...
- C#类与结构体的小结
1.定义不同 类使用class关键字来定义: 结构体用struct: 2.使用时的注意事项 ->结构体是值类型,类是引用类型 ->结构体中声明的变量不能做赋值操作,但是类可以. -> ...
- WPF-------依赖项属性
http://www.cnblogs.com/Zhouyongh/archive/2009/09/10/1564099.html http://www.cnblogs.com/Zhouyongh/ar ...