sql语法复习:增删查改,各种数据库对象创建和函数使用
推荐工具:机子配置较低的话,可以装Gsql这个工具获得sql执行环境(可作为手册查看内置数据类型 函数和存储过程等)
--之前数据库的东西接触不多,虽然基本的语法是了解,但不是很熟悉
--最近项目一直在折腾存储过程(一些数据逻辑都通过存储过程在数据库端实现),
--复习了一遍sql的东东,在此记录一下。
/*
--创建数据库
create database test
use test;
--创建表 字段定义列表 主键 外键
create table score (id int primary key, student varchar(30), subjectId int foreign key references subject(sid) ,score int );
*/
/*
--添加 删除 查询记录
insert into score (id,student,subject,score) values(1,'zhangsan','math', 73);
delete from score;
select * from score
*/
/*
--聚合函数 avg(), group by分组, order by 排序, having 对分组进行过滤
select student, avg(score) as avgscore from score group by student order by avgscore;
select student from score group by student having min(score)>80; --选出各科成绩都在80分以上的学生
*/
/*
--distinct 不重复的,关系运算符:(not) between ..and.., like '%si', in ('zhangsan','lisi')
select distinct student from score;
select * from score where score between 86 and 99;
select * from score where score not between 86 and 99;
select * from score where score = 93;
select * from score where score >86;
select * from score where score >=93;
select * from score where student like '%四';
select * from score where student in ('张三', '李四');
select * from score where not student in('张三', '李四'); -- where not field in (list)
*/
/*
-- and, or 逻辑运算符
select * from score where student='张三' and score >80;
select * from score where student = '张三' or student='李四'
*/
/*
--order by f1,f2 多字段排序
select * from score where student = '张三' or student='李四' order by score asc
select * from score where student='张三' or student='李四' order by score desc;
select * from score where student='张三' or student='李四' order by student, score;
*/
--插入 更新 删除
--insert into score values(7, '小段', '数学', 87);
--update score set score=60 where id=7;
--delete from score where score<70;
/*
--select top 2 选择前面若干条记录
select top 2 * from score;
select top 50 percent * from score;--前50%的记录
*/
/*
-- like模式匹配 通配符 % _ [] [^] [a-c]
select * from score where subject like '%文';
select * from score where subject not like '%文';
select * from score where subject like '_语';
select * from score where subject like '_[学,文]';
select * from score where subject like '_[^学,文]';
select * from customers where city like '[a-c]%';
*/
--select * from score;
--select * from stdinfo;
/*
-- as 给字段别名或表别名
select id as 'No.', student as '姓名', subject as '科目' ,score as '分数' from score;
select id as 'No.', student as [学生姓名] from score;
select s.student , s.subject, s.score from score as s;
*/
--inner join tbl2 on tbl1.scoreId=tbl2.scoreId 连接查询 扩展字段并筛选记录
--select score.student, score.subject, score.score, stdinfo.height from score inner join stdinfo on stdinfo.name = score.student;
/*
--插入新记录 全字段值列表 则不用列出字段
insert into stdinfo values(4,'小新',90,'cm');
select * from stdinfo;
*/
/*
--连接查询 inner join 内连,left join 左连接,right join 右连接
select score.student, score.subject, score.score, stdinfo.height from score inner join stdinfo on stdinfo.name = score.student;
select score.student, score.subject, score.score, stdinfo.height from score join stdinfo on stdinfo.name = score.student;
select score.student, score.subject, score.score, stdinfo.height from score left join stdinfo on stdinfo.name = score.student;
select score.student, score.subject, score.score, stdinfo.height from stdinfo left join score on stdinfo.name = score.student;
select score.student, score.subject, score.score, stdinfo.height from score right join stdinfo on stdinfo.name = score.student;
select score.student, score.subject, score.score, stdinfo.height from stdinfo right join score on stdinfo.name = score.student;
select score.student, score.subject, score.score, stdinfo.height from score full join stdinfo on stdinfo.name = score.student;
*/
/*
--union 结果集合并,要求两个结果集的字段数和字段类型相同
select id, student from score union select id, name from stdinfo;
select id, student from score union all select id, name from stdinfo;
*/
--create database test2;
/*
--查询结果集存入新表中
select * into score_back from score;
insert into score_back(student,score) select student,score from score;
*/
/*
--创建数据库和表
create database family;
use family;
create table person(id int, lname varchar(30), fname varchar(40), address varchar(200) ,city varchar(20) );
select * from person;
*/
/*
-->>sql constraint:
not null, unique, primary key,foreign key,check, default
*/
--getdate()函数 返回当前的日期时间
--select GETDATE() as Date
--create index 创建索引
--create index idx_s on score(student);
/*
--create view view_name as select * from tbl 创建视图
create view myinfo as select * from score
select * from sysobjects where xtype='v'
*/
/*
-- getdate(), datepart()
select GETDATE() as date;
select DATEPART(d, GETDATE() );
*/
/*
--聚合函数: avg(), count(),first(),max(),min(),sum()
select avg(score) as 'avgScore' from score;
select count(*) as rsAmount from score;
select count(subject) as subCount from score;
select count(distinct subject) as subDiffCount from score;
select first(score) from score;
select max(score) as maxScore from score;
select min(score) as minScore from score;
select sum(score) as sumScore from score;
*/
/*
--修改表结构 加字段
alter table score add py varchar(10);
update score set py='yuwen' where subject='语文';
*/
/*
--字符函数 len(),upper(),lower(),mid() 数学函数:round()
select len(py) as py from score;
select *, upper(py) as py from score;
update score set py = upper(py);
select getdate() as date;
update score set py=lower(py);
select mid(student,1,1) as fname from score;
select round(score,2) as score_pre from score;
*/
/*
--查看数据库表 和 表结构
use test
go
select * from sysobjects where xtype='u'
exec sp_help testTbl
select * from sys.databases
select @@version
*/
/*
--object_id()查找对象的id(从sysobjects表中), objectproperty(objId,prop)
select object_id('ptest')
select * from sysobjects
select * from sysobjects where id = object_id('ptest') and OBJECTPROPERTY(id, 'IsUserTable') = 1
*/
-------------------------------------------------------
/*
--查看当前数据库的用户
exec sp_helpuser
--查看数据库(内置数据库和用户创建的)
exec sp_helpdb
--查看当前数据库的表(包括:系统表、用户表和视图)
exec sp_tables
--查看表的明细(字段 约束..)
exec sp_help 'binaryTbl'
--查看存储过程定义
exec sp_helptext 'myproc'
--查看指定表的索引
exec sp_helpindex score
--设置数据库兼容级别
exec sp_dbcmptlevel 80
*/
--设置数据库的选项
--exec sp_dboption 'test', 'quoted identifier' ,'on'
--exec sp_dboption
--set quoted_identifier on
--use test
/*
-- test decimal(3,2)
create table ptest (f1 decimal(3,2)); --f1为1位整数部分,2位小数部分的数值
insert into ptest values(2.234545);
insert into ptest values(34.4454); --报错 会溢出
select * from ptest
*/
/*
-- test bit
drop table bitTbl;
create table bitTbl (f1 bit , f2 bit, f3 bit);
insert into bitTbl values(12,5,2); --非0为真 则转换成1
select * from bitTbl
*/
/*
-- test money
create table moneyTbl (f1 money, f2 smallmoney);
insert into moneyTbl values($234, $23.29);
select * from moneyTbl;
*/
/*
-- test binary, varbinary, image
create table binaryTbl (f1 binary, f2 varbinary(30), f3 image);
insert into binaryTbl values(0x123, 0xffff, 0x12fff02);
select * from binaryTbl;
--修改字段的数据类型
alter table binaryTbl alter column f2 varbinary(50);
alter table binaryTbl alter column f1 binary(40);
exec sp_help 'binaryTbl'
*/
--数据类型转换 cast(@var as datatype)
--select cast('2003-03-23 12:02:23.443'as smalldatetime) as smdt
/*
--变量的声明 赋值 和打印
declare @nvar nchar(26);
set @nvar = N'this is an unicode string';
print @nvar;
*/
/*
-- newid()函数 和 uniqueidentifier数据类型 convert(datatype, @var)函数
print newid()
declare @uid uniqueidentifier;
set @uid= newid();
print 'value of @uid is:' + convert(varchar(255), @uid);
*/
/*
--自定义数据类型的存储过程 sp_addtype
exec sp_addtype telephone , 'varchar(25)', 'not null'
exec sp_addtype fax, 'varchar(24)','not null';
exec sp_droptype fax
*/
/*
--变量的使用
declare @mycounter int;
set @mycounter = 18%5;
print convert(varchar(255), @mycounter);
*/
--id字段值都为'用户'
--select id='用户', student,score from score;
/*
--位运算符
declare @a int, @b int;
set @a = 5;
set @b = 10;
select @a & @b, @a | @b, @a^@b;
*/
/*
--变量用在where子句中
declare @score int, @py varchar(20);
set @score = 88;
select @py = 'shuxue';
select * from score where score>@score and py=@py;
*/
/*
--变量赋值 select
select * from dbo.score
declare @sc int;
select @sc = score from score;
print @sc;
*/
/*
--全局变量
print @@connections
print @@cpu_busy
print @@datefirst
print @@dbts
print @@identity
print @@langid
print @@language
print @@max_connections
print @@max_precision
print @@nestlevel
print @@options
print @@version
print @@spid
*/
/*
--while控制流 begin .. end
declare @i int, @r int;
set @i =0;
set @r = 0;
while @i<=100
begin
set @r = @i + @r;
set @i = @i + 1;
end
print @r
*/
/*
--语句标签 goto labelName
declare @i int, @r int
set @i = 0;
set @r = 0;
myloop:
set @r = @r + @i;
set @i = @i +1;
if @i<=100
goto myloop
print cast(@r as varchar(20));
*/
--use test
--go
--删除存储过程
--drop proc myproc
/*
--获取存储过程的返回值
declare @stud varchar(20), @rst int;
set @stud = '张三';
exec @rst = myproc @stud --获取存储过程的返回值
if @rst = 1
print 'very good'
else
print 'should come on'
go
*/
--查看存储过程定义
--exec sp_helptext 'myproc'
/*
--创建函数
create function cubicVolume(@len decimal(4,1), @width decimal(4,1), @h decimal(4,1))
returns decimal(12,3)
as
begin
return (@len * @width * @h)
end
go
print cast(dbo.cubicVolume(20,2,5) as varchar(20));
go
*/
/*
create function searchStud (@score int)
returns @studInfo table(id int,student varchar(30),subject varchar(20),score int,py varchar(10))
as
begin
insert @studInfo select * from score where score > @score
return
end
go
--drop function dbo.searchStud
select * from searchStud(88);
*/
/*
update dbo.score set score = 85 where id = 3;
select * from score;
*/
/*
-- case when 值转换
select student as '学生' , subject as '科目',
case
when score >80 then '优秀'
when score >70 then '良好'
else '不及格'
end as '成绩'
from score
go
*/
--sysindexes系统索引表
--select * from dbo.sysindexes;
/*
use test
go
if exists(select * from dbo.sysindexes where name='score_studIdx')
drop index score.score_studIdx
go
create nonclustered index score_studIdx on score(student)
go
*/
--exec sp_helpuser
/*
--查看指定表的索引,删除索引
exec sp_helpindex score
drop index score.score_studIdx
*/
sql语法复习:增删查改,各种数据库对象创建和函数使用的更多相关文章
- 常用SQL语句(增删查改、合并统计、模糊搜索)
转自:http://www.cnblogs.com/ljianhui/archive/2012/08/13/2695906.html 常用SQL语句 首行当然是最基本的增删查改啦,其中最重要的是查. ...
- SQL语句的增删查改
一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...
- SQL基本之增删查改操作
1.为表添加主键 alter table <tablename> add primary key(col); 主键添加前: 主键添加后: 2.插入数据 insert into <ta ...
- sql基本的增删查改语句
1.增---用于向表中插入新的行/数据 语法:insert into 表名(值1,值2,值3...) values(值1,值2,值3,...) 或者 语法:insert [into] <表名&g ...
- 基本SQL 语句操作数据增删查改
1.创建数据库: create database <数据库名>. 如:create database student; 2.连接到一个已经存在的数据库: use <数据库名>: ...
- Mybatis 的动态SQL,批量增删查改
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 批量增删改的接口: public interface BookService { //批量增加 int ...
- JDBC终章- 使用 DBUtils实现增删查改- C3P0Utils数据源/QueryRunner runner连接数据源并执行sql
JDBC终章- 使用 DBUtils实现增删查改 1.数据库结构 Create Table CREATE TABLE `user` ( `id` ) NOT NULL AUTO_INCREMENT, ...
- ADO.NET教程(2)实现增删查改
声明一个类,在类中实现增删查改的方法 public class AdoNet { //声明连接字符串 public string Sqlstr = "data source={0};data ...
- C# SQLite 创建数据库的方法增删查改语法和命令
SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...
随机推荐
- 微信支付java版V3验证数据合法性
[TOC] 1. 微信支付java版V3验证数据合法性 概要:使用微信支付接口时,微信会返回或回调给商户XML数据,开发者需要验证微信返回的数据是否合法. 特别提醒:商户系统对于支付结果通知的内容一定 ...
- poj2407---欧拉函数应用
欧拉函数介绍: 在数论中,对正整数n,欧拉函数是少于或等于n你的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中 ...
- socket中的option
/// Set an option on the socket. /** * This function is used to set an option on the socket. * * @pa ...
- gdb 命令使用
1.gdb -x command.txt 每次重复输入命令很麻烦,可以使用上面的命令,把命令输入进command.txt里面,然后直接就可以执行gdb. 2.list 2.1 list functio ...
- poj3308
二分图的最小点权覆盖,选定点集,与该点集有关的边覆盖所有顶点,且该点集的点权值和最小. 有类似于匈牙利算法一样的带权匹配算法,但是这里就不介绍了.个人比较推荐,用最大流算法更好理解,写起来更容易. 题 ...
- Java Web 部署到Tomcat
1.在conf目录中,新建Catalina(注意大小写)\localhost目录,在该目录中新建一个xml文件,名字可以随意取,只要和当前文件中的文件名不重复就行了,该xml文件的内容为: <C ...
- CSS background-repeat 属性
###起因 >今天遇到一个问题,就是在给一个元素设置width 属性为100% 之后, 鼠标放上去之后,仍然只有部分是阴影状态,如下图所示: --- 经过一番思索,这TM 不就是,hover 上 ...
- Python 错误和异常
1.Python异常类 Python是面向对象语言,所以程序抛出的异常也是类.常见的Python异常有以下几个,大家只要大致扫一眼,有个映像,等到编程的时候,相信大家肯定会不只一次跟他们照面(除非你不 ...
- 【原创】JPEG图像密写研究(二) 哈夫曼树的建立
[原创]记录自己研究的过程,仅供参考,欢迎讨论... 在根据JPEG图像文件结构读取完文件后,提取出其中DHT段,利用其中内容建立哈夫曼树,便于之后译码工作.这里需要注意的是文件中的哈夫曼表数量不固定 ...
- OSG中的智能指针
在OpenSceneGraph中,智能指针(Smart pointer)的概念指的是一种类的模板,它针对某一特定类型的对象(即Referenced类及其派生类)构建,提供了自己的管理模式,以避免因为用 ...