常见SQL语句

sqlservlet语句

创建数据库

use mysql----进入数据库
create database 数据库名;

创建表

create table 表名(
   -- 字段名1 类型(字符长度),
    stu_name varchar(50),
    stu_sex varchar(50),
    sru_pwd varchar250)
)

添加数据

--表内添加字段
alter table stu add stu_addr varchar(200);
--执行添加单条数据语句
insert into stu(stu_id,stu_name,stu_sex,sru_pwd) values('999','嬴政','男',,'123');
--执行添加多条数据语句,本语句同样适用于2005版本sqlserver
insert into stu(id,name,sex,pwd)
select '0001','张三','男','123'
union all
select '0002','张三','男','123'
union all
select '0003','张三','男','123'

删除语句

--删除表字段
alter table stu drop column stu_add
--通过条件删除指定数据
	delete stu where stu_id='1000';
--删除表内所有数据
	delete stu
--删除整张表(包括表结构一起删除)
	drop table stu
--删除数据库
	drop database stu

修改语句

--通过指定条件(如stu_id)修改指定数据(如stu_name)
	update stu set stu_name='张山' where stu_id='1001';
--修改表结构
	alter table 表名 rename column  想改的字段 to 更改后的字段名;

查询语句

--查询全表
select· * from stu;
--条件查询
select stu_id,stu_name from stu where id='1001'
----不确定值,模糊查询
select * from stu where atu_addr like '%长安%'
-----范围查询
select * from stu where stu_name in ('李白','张三斤')
-----如果用or为或关系,如果用and为且,即两个条件都要满足
select * from stu where stu_name='李白' or stu_name='张三斤'
--分页
select top 3 * from stuinfo where id not in(---每页显示3个
	select top 5 id from stuinfo----从第5个数据往后开始
)

约束

--1---unique-----------唯一性
--2---not null---------不能为空
--3---primary ker------主键,唯一性,不能为空
--4---check------------校验---check (stu_sex='男'or‘女’)
--5---default----------默认值
--6---identity---------自增(默认第一个值,后面每个增加几个 )
--7---foreign key referonces--关联外键----外键约束---

连接查询

--内连接
    select * from A表 inner join B表 on A表.字段 = B表.字段
--外连接
----->左外连接
    select * from A表 left join B表 on A表.字段 = B表.字段
----->右外连接  
    select * from  A表 right join B表 on A表.字段 = B表.字段
----->完全外连接
     select * from  A表 full join  B表 on A表.字段 = B表.字段

子查询语句

这边给个连接,可以前往翻看
点击这里

mysql语句与sqlserver语句区别不大
点击跳转可以看看这个博客,区别总结的可以哦,理论上的就不说了

mysql编码----创表创数据库时跟上即可
创建数据库的编码集
CHARACTER SET utf8 COLLATE utf8_general_ci;

创建表的编码集
ENGINE=InnoDB DEFAULT CHARSET=utf8;

jdbc链接编码
?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull

oracle数据库

初步认识orcal数据库plsql语句

declare--声明
  maxrecords constant int:=100;--自定义常量名  常量  数据类型:= 值
  i int:=1;--自定义变量名 数据类型:= 值 
begin--程序开始
  for i in 1..maxrecords loop--for循环 i从1开始到常量100结束
     insert into stu(stu_id,stu_name,stu_sex,stu_buzhu,sru_pwd) values (1000+i,sysdate,'男',i,123);
     --插入语句sysdate为当前系统时间,注意字符串类型为单引号修饰
  end loop;--结束循环
  dbms_output.put_line('sucecss');--输出语句
  commit;--提交(所有增删改操作都需要commit)
end;--程序结束
  

declare--声明

  定义语句段;
  --1)定义部分
  --该部分定义程序中要使用的常量、变量、游标
  
begin--程序开始
  
  执行语句段;
  --2)执行部分
  --该部分是pl/sql必备的包含了对数据库操作语句和各种流程操作语句和各种流程控制语句
  
exception--异常
  异常处理语句段;
  --3)异常处理部分
  --该部分包含在执行部分里面,对程序中产生的异常情况进行处理
  
end;--程序结束


-->定义一个小数,限制小数位数(总位数,小数点后位数)
declare
  pi constant number(8,7):=3.1415926;
begin
  dbms_output.put_line(pi);
  commit;
end;

  -->查询特定条件放到变量里面,并输出这个变量
declare
  dns varchar2(30);
begin
  select stu_name into dns from stu where stu_id=1001;
  dbms_output.put_line(dns);
end;

-->定义变量数据类型与表中列数据类型一致
declare
  dns scott.stu.stu_name%type;
begin
  select stu_name into dns from stu where stu_id =1001;
  dbms_output.put_line(dns);
end;

-->多个数据捆绑在一起,定义用户自己记录的数据类型
declare
  type myrow is record (
       did scott.stu.stu_id%type,
       dname stu.stu_name%type,
       dsex stu.stu_sex%type,
       dbuzhu stu.stu_buzhu%type,
       dpwd stu.sru_pwd%type
   );
   myrecord myrow;
begin
  select * into myrecord from stu where stu_id=1001;
  dbms_output.put_line(myrecord.dname);
end;

--捆绑数据简便写法
declare
  myrecord stu%rowtype;
begin
   select * into myrecord from stu where stu_id=1001;
   dbms_output.put_line(myrecord.stu_name) ;
end;

-->if...then
-->else
----->avg 求平均值
declare
   avgbuzhu stu.stu_buzhu%type;
begin
  select avg(stu_buzhu) into avgbuzhu from stu;
  if(avgbuzhu>=50)then
         dbms_output.put_line('学生满意');
  else
         dbms_output.put_line('学生认同');
  end if;  
end;

--case 分支语句(注意:从上往下执行,一旦满足,结束case分支语句)
--CASE 测试变量
--     WHEN 目标值1 THEN …;
--     WHEN 目标值2 THEN…;
--    ELSE …;
--END CASE;
declare
   avgbuzhu stu.stu_buzhu%type;
begin
  select avg(stu_buzhu) into avgbuzhu from stu;
  case
    when avgbuzhu >=70 then dbms_output.put_line('学生认同+10086');
    when avgbuzhu >=50 then dbms_output.put_line('学生认同+3');
    when avgbuzhu >=40 then dbms_output.put_line('学生认同+2');
    when avgbuzhu >=20 then dbms_output.put_line('学生认同+1');
  end case;
end;

------》case在select查询语句中使用
select stu_id,stu_name,stu_sex,stu_buzhu,sru_pwd,
       case stu_id
         when '1001' then '新学生'
         when '1005' then '老学生'
         else 'pt'
        end tip--新列用于显示then后的类型
from stu;

---》loop...exit...end loop循环控制--9
declare
   number1 integer:=50;
   number2 integer:=60;
   i integer:=0;
begin
  loop
    number1:=number1+1;
    if(number1=number2)then
       exit;
     else
       i:=i+1;
     end if;
     end loop;
     dbms_output.put_line('共循环次数为:'||to_char(i)||'||用来字符串拼接连接');--\\用来字符串拼接连接
end;

---->loop...exit when...end loop循环控制--10
declare
   number1 integer:=50;
   number2 integer:=60;
   i integer:=0;
begin
  loop
    number1:=number1+1;
    i:=i+1;
    exit when number1=number2;
    end loop;
    dbms_output.put_line('共循环次数为:'||to_char(i)||'||用来字符串拼接连接');--\\用来字符串拼接连接
end;

--while<条件> loop..,end loop--10
declare
   number1 integer:=50;
   number2 integer:=60;
   i integer:=0;
begin
  while number1<number2 loop
    number1:=number1+1;
    i:=i+1;
    end loop;
    dbms_output.put_line('共循环次数为:'||to_char(i)||'||用来字符串拼接连接');--\\用来字符串拼接连接
end;

--for<循环范围>loop...end loop--10
declare
   number1 integer:=50;
   --number2 integer:=60;
   i integer:=0;
begin
  for i in 1..10 loop
    number1:=number1+1;
  end loop;
  dbms_output.put_line('number1:'||to_char(number1)||'||用来字符串拼接连接');--\\用来字符串拼接连接
end;



--游标,在初始状态下指向的首记录之前
declare
  cursor mycursor is select * from stu;
begin
  open mycursor;
end;

--定义游标 cursor 游标名
--打开游标 open 游标名
--提取数据 fetch 游标名
--关闭游标 close 游标名
declare
  cursor mycursor is select * from stu;
  currecord mycursor%rowtype;
begin
  open mycursor;
  fetch mycursor into currecord;
  dbms_output.put_line(to_char(currecord.stu_name));
  close mycursor;
end;

--使用for循环提取记录和数据,循环中隐含包括了open,fetch,close操作

declare
  cursor mycursor is select * from stu;
  currecord mycursor%rowtype;
begin
  for currecord in mycursor loop
    dbms_output.put_line(currecord.stu_name||'And'||to_char(currecord.stu_id));
  end loop;
end;

--------------------->
begin
  for currecord in (select * from stu) loop
    dbms_output.put_line(currecord.stu_name||'And'||to_char(currecord.stu_id));
  end loop;
end;

-------->游标名%isopen 判断游标是否被打开
--------->游标名%found 判断fetch是否正常提取到记录数据
--------->游标名%nofound 判断fetch没有正常提取到记录数据
--------->游标名%rowcount,游标提取数据时,被提取的数据的行数

----存储过程
存储是在大型数据库系统中,一组为了完成特定功能的sql语句集
第一次编译后再调用就不需要再编译了(数据库语句访问性能优化)
存储过程是数据库中的一个对象
优点:提高了访问的性能,安全性,复用性,执行速度快
缺点:可移植性,兼容性差(不是所有的数据库都有存储过程,
也不是所有数据库的存储过程都一样,一旦使用了oracle存储过程,
意味着不能随意的换数据库。)

--创建存储过程 根据id查询姓名
create or replace procedure my_proc1(
   stuid in varchar2,stuname out varchar2
)
as
begin
  select stu_name into stuname from stu where stu_id=stuid;
end my_proc1;

--测试
declare
    stuname stu.stu_name%type;
begin
  my_proc1(1001,stuname);
  dbms_output.put_line(stuname);
end;

--创建存储过程--查询所有学生记录
create or replace procedure selAll(
    cur_stu out sys_refcursor
)
as
begin
  open cur_stu for select * from stu;
end;
--测试方式
declare
  cur_stu sys_refcursor;
  st stu%rowtype;
begin
  selAll(cur_stu);
  loop
    fetch cur_stu into st;
    exit when cur_stu%notfound;
    dbms_output.put_line(st.stu_name);
  end loop;
  close cur_stu;
end;

---函数实际上一个特定的功能单位

---》定义函数
create or replace function my_fun1(s_name in varchar2)
return sys_refcursor
as
   my_cur sys_refcursor;
begin
   open my_cur for select * from stu
   where stu_name like '%'||s_name||'%';
   return my_cur;
end;

----------->创建包
create or replace package my_pkg1
as
  type refcur is ref cursor;
  procedure proc(v_name in varchar2,v_cur out refcur);
  function fun(v_name in varchar2) return refcur;
end my_pkg1;

--------》创建序列

create sequence seq_xl --创建序列名称
increment by 1--增长幅度
start with 1 --初始值
maxvalue 9999999999;  --设置增长的最大值

-----查询序列 每查询一次序列增长一次
select seq_xl.nextval from dual;

---删除序列
drop sequence seq_xl;

jdbc调用oracle几点注意

//url
URL="jdbc:oracle:thin:@localhost:1521:ORCL"
URL="jdbc:sqlserver://localhost:1433;databaseName=stu"
URL="jdbc:mysql://localhost:3306/shopping?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull"
//statement命令执行对象
private  CallableStatement cstmt=null;
public void getStmt(String prosql){
		try {
			//prosql={call my_proce3(?,?)}
			cstmt=conn.prepareCall(prosql);
			System.err.println("====stmt======");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//为oracle的存储过程输入参数赋值和输出参数注册返回结果
		cstmt.setString(1, "1");
		cstmt.registerOutParameter(2, Types.VARCHAR);
		//执行
		cstmt.execute();
		//取得返回结果值
		String name=cstmt.getString(2);


//如果是返回
//{call find(?)}
		getStmt(procsql);
		//注册返回结果为多个对象时
		cstmt.registerOutParameter(1, OracleTypes.CURSOR);
		//执行命令
		cstmt.execute();
		rs=(ResultSet)cstmt.getObject(1);
		rs2list(list);

		//调用函数的语句
		//String sql="{?=call fun(?)}";
		//调用包时的语句
		//String sql="{?=call scott.my_pkg1.fun(?)}";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值