⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 数据库语句.txt

📁 HTNL做的光晕特效
💻 TXT
📖 第 1 页 / 共 2 页
字号:
--语 句              功 能

--数据操作
select   --从数据库表中检索数据行和列
insert   --向数据库表添加新数据行
delete   --从数据库表中删除数据行
update   --更新数据库表中的数据 

--数据定义 
create table  --创建一个数据库表
drop table     --从数据库中删除表 
alter table   --修改数据库表结构
create view   --创建一个视图 
drop view   --从数据库中删除视图
create index  --为数据库表创建一个索引
drop index   --从数据库中删除索引 
create procedure  --创建一个存储过程 
drop procedure  --从数据库中删除存储过程
create trigger  --创建一个触发器 
drop trigger  --从数据库中删除触发器
create schema  --向数据库添加一个新模式
drop schema   --从数据库中删除一个模式
create domain  --创建一个数据值域
alter domain  --改变域定义
drop domain   --从数据库中删除一个域

--数据控制 
grant   --授予用户访问权限
deny   --拒绝用户访问 
revoke   --解除用户访问权限

--事务控制
commit   --结束当前事务
rollback   --中止当前事务 
set transaction  --定义当前事务数据访问特征

--程序化SQL 
declare   --为查询设定游标 
explan   --为查询描述数据访问计划
open   --检索查询结果打开一个游标
fetch   --检索一行查询结果 
close   --关闭游标
prepare   --为动态执行准备SQL 语句 
execute   --动态地执行SQL 语句
describe   --描述准备好的查询 

---局部变量
declare @id char(10) 
--set @id = ’10010001’ 
select @id = ’10010001’

---全局变量 
---必须以@@开头

--if else
declare @x int @y int @z int
select @x = 1 @y = 2 @z=3
if @x > @y
print ’x > y’ --打印字符串’x > y’
else if @y > @z 
print ’y > z’ 
else print ’z > y’

--case 
use pangu 
update employee
set e_wage =
case
 when job_level = ’1’ then e_wage*1.08
 when job_level = ’2’ then e_wage*1.07
 when job_level = ’3’ then e_wage*1.06
 else e_wage*1.05
end 

--while continue break
declare @x int @y int @c int
select @x = 1 @y=1
while @x < 3 
begin
 print @x --打印变量x 的值
 while @y < 3 
  begin
  select @c = 100*@x + @y
  print @c --打印变量c 的值
  select @y = @y + 1
  end
 select @x = @x + 1
 select @y = 1
end 

--waitfor
--例 等待1 小时2 分零3 秒后才执行SELECT 语句
waitfor delay ’01:02:03’
select * from employee
--例 等到晚上11 点零8 分后才执行SELECT 语句
waitfor time ’23:08:00’
select * from employee 
***select*** 

  select *(列名) from table_name(表名) where column_name operator value 
  ex:(宿主) 
 select * from stock_information where stockid  = str(nid)
   stockname = ’str_name’
   stockname like ’% find this %’
   stockname like ’[a-zA-Z]%’ --------- ([]指定值的范围) 
   stockname like ’[^F-M]%’  --------- (^排除指定范围) 
   --------- 只能在使用like关键字的where子句中使用通配符)
   or stockpath = ’stock_path’
   or stocknumber < 1000
   and stockindex = 24
   not stocksex = ’man’
   stocknumber between 20 and 100
   stocknumber in(10,20,30) 
   order by stockid desc(asc) --------- 排序,desc-降序,asc-升序 
   order by 1,2 --------- by列号
   stockname = (select stockname from stock_information where stockid = 4) 
   --------- 子查询 http://www.acnow.net/ oYIpsxSPAIC
   --------- 除非能确保内层select只返回一个行的值,
   --------- 否则应在外层where子句中用一个in限定符 
 select distinct column_name form table_name --------- distinct指定检索独有的列值,不重复
 select stocknumber ,"stocknumber + 10" = stocknumber + 10 from table_name 
select stockname , "stocknumber" = count(*) from table_name group by stockname 
        --------- group by 将表按行分组,指定列中有相同的值 
     having count(*) = 2 --------- having选定指定的组
 select * from table1, table2  
 where table1.id *= table2.id -------- 左外部连接,table1中有的而table2中没有得以null表示
   table1.id =* table2.id -------- 右外部连接 

 select stockname from table1 
 union [all] ----- union合并查询结果集,all-保留重复行 
 select stockname from table2

***insert***

 insert into table_name (Stock_name,Stock_number) value ("xxx","xxxx")
       value (select Stockname , Stocknumber from Stock_table2)---value为select语句

***update***

 update table_name set Stockname = "xxx" [where Stockid = 3]
     Stockname = default
     Stockname = null
     Stocknumber = Stockname + 4

***delete*** 

 delete from table_name where Stockid = 3 
 truncate table_name ----------- 删除表中所有行,仍保持表的完整性 
 drop table table_name --------------- 完全删除表

***alter table*** --- 修改数据库表结构 

 alter table database.owner.table_name add column_name char(2) null ..... 
 sp_help table_name ---- 显示表已有特征
 create table table_name (name char(20), age smallint, lname varchar(30))
 insert into table_name select ......... ----- 实现删除列的方法(创建新表)
 alter table table_name drop constraint Stockname_default ---- 删除Stockname的default约束
***function(/*常用函数*/)***

下列语句部分是MsSql语句,不可以在access中使用。

SQL分类: 
DDL—数据定义语言(Create,Alter,Drop,DECLARE) 
DML—数据操纵语言(Select,Delete,Update,Insert) 
DCL—数据控制语言(GRANT,REVOKE,COMMIT,ROLLBACK)

首先,简要介绍基础语句:
1、说明:创建数据库
Create DATABASE database-name 
2、说明:删除数据库
drop database dbname
3、说明:备份sql server
--- 创建 备份数据的 device
USE master
EXEC sp_addumpdevice 'disk', 'testBack', 'c:\mssql7backup\MyNwind_1.dat'
--- 开始 备份
BACKUP DATABASE pubs TO testBack 
4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)
根据已有的表创建新表: 
A:create table tab_new like tab_old (使用旧表创建新表)
B:create table tab_new as select col1,col2… from tab_old definition only
5、说明:删除新表
drop table tabname 
6、说明:增加一个列
Alter table tabname add column col type
注:列增加后将不能删除。DB2中列加上后数据类型也不能改变,唯一能改变的是增加varchar类型的长度。
7、说明:添加主键: Alter table tabname add primary key(col) 
说明:删除主键: Alter table tabname drop primary key(col) 
8、说明:创建索引:create [unique] index idxname on tabname(col….) 
删除索引:drop index idxname
注:索引是不可更改的,想更改必须删除重新建。
9、说明:创建视图:create view viewname as select statement 
删除视图:drop view viewname
10、说明:几个简单的基本的sql语句
选择:select * from table1 where 范围
插入:insert into table1(field1,field2) values(value1,value2)
删除:delete from table1 where 范围
更新:update table1 set field1=value1 where 范围
查找:select * from table1 where field1 like ’%value1%’ ---like的语法很精妙,查资料!
排序:select * from table1 order by field1,field2 [desc]
总数:select count * as totalcount from table1
求和:select sum(field1) as sumvalue from table1
平均:select avg(field1) as avgvalue from table1
最大:select max(field1) as maxvalue from table1
最小:select min(field1) as minvalue from table1
11、说明:几个高级查询运算词
A: UNION 运算符 
UNION 运算符通过组合其他两个结果表(例如 TABLE1 和 TABLE2)并消去表中任何重复行而派生出一个结果表。当 ALL 随 UNION 一起使用时(即 UNION ALL),不消除重复行。两种情况下,派生表的每一行不是来自 TABLE1 就是来自 TABLE2。 
B: EXCEPT 运算符 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -