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文件中 本模块内部使用结构体,放模块内部头文件中 供外部使用的结构体,放模块对外头文件中.
随机推荐
- 常用Linux终端命令行的快捷键列表
终端有很多快捷键,不太好记,常用的在这里 Ctrl+r 实现快速检索使用过的历史命令.Ctrl+r中r是retrieve中r. Ctrl+a:光标回到命令行首. (a:ahead) Ctrl+e:光标 ...
- 解决WIN7第一次开机冷启动QQ未响应的办法
为什么WIN7第一次开机冷启动QQ未响应?WIN10就没事? http://bbs.wuyou.net/forum.php?mod=viewthread&tid=409516&extr ...
- ES - 处理TooManyClause异常
1.TooManyClause 我们在使用terms query.prefix query.fuzzy query.wildcard query.range query的时候,一不小心就会遇到TooM ...
- MFC 解决中文乱码问题
新的编译工具默认的是unicode编码方式,许多在多字节下面显示中文的方法已经不再适用了按照道理说设置为unicode编码后应该会很好的支持中文, 但是实际情况很悲惨,显示的都是乱码. 看到网上的很多 ...
- linux中的查找命令find,locate,which,whereis
我们经常在linux要查找某个文件,但不知道放在哪里了,可以使用下面的一些命令来搜索.这些是从网上找到的资料,因为有时很长时间不会用到,当要用的时候经常弄混了. which 查看可执行文 ...
- 20175236 JAVA MyCP(课下作业)
具体描述: 编写MyCP.java 实现类似Linux下cp XXX1 XXX2的功能,要求MyCP支持两个参数: java MyCP -tx XXX1.txt XXX2.bin 用来把文本文件(内容 ...
- 通用mapper版+SpringBoot+MyBatis框架+mysql数据库的整合
转:https://blog.csdn.net/qq_35153200/article/details/79538440 开发环境: 开发工具:Intellij IDEA 2017.2.3 JDK : ...
- mysql ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrain fails
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constrain fails. 可能是MySQL在In ...
- Intellij IDEA超好用的快捷键
1.首推 Ctrl+Shift+Enter,自动跳出括号,在行尾添加分号,并自动回车. 2.Ctrl+Shift+J,将多行代码折叠成一行. 当你遇到的时候,这个快捷键非常实用.比如你在写painle ...
- 自定义项目启动初始化信息的listener报错
自定义初始化组件代码如下: @Component public class InitComponent implements ServletContextListener, ApplicationCo ...