sybase常用命令

一、SQL Advantage使用
先点击server,里面点击connect连接到sybase服务端,开一个新窗口,写一些sql执行即可(ctrl+e)


二、启动和关闭数据库,查看表结构
1.看所有的库
sp_helpdb


2.使用一个库
use 库名(注意大小写)


3.查看用户下所有表名
select name from sysobjects where type='U'

sp_tables


4.查看表结构
sp_help 表名(注意大小写)


5.显示结果只显示一行
set rowcount 1
go
输入你想查询的sql语句,记得最后要
set rowcount 0
go


6.启动数据库
[sybase@rosan128main install]$ pwd
/opt/sybase/ASE-12_5/install
[sybase@rosan128main install]$ startserver -f RUN_rosantech

7.关闭数据库
先要看启动了什么服务
1> select srvname from sysservers
2> go
 srvname                        
 ------------------------------ 
 SYB_BACKUP                     
 loopback                       
 rosanljw_BS                    
 rosantech                      

(4 rows affected)

关闭备份服务
1> shutdown SYB_BACKUP
2> go

关闭
1> shutdown 
2> go

如果关不上
1> sp_lock
2> go
The class column will display the cursor name for locks associated with a cursor
for the current user and the cursor id for other users.
 fid    spid   loid        locktype                     table_id    page       
         row    dbname          class                         
         context                      
 ------ ------ ----------- ---------------------------- ----------- -----------
         ------ --------------- ------------------------------
         ---------------------------- 
      0     57         114 Sh_intent                       32000114           0
              0 master          Non Cursor Lock               
                                      

(1 row affected)
(return status = 0)
1> kill 57(spid只可以杀除自己会话以外的会话) 杀掉再shutdown就好了 

!!!如果关不动,可以用以下命令,慎用
shutdown with nowait



三、dml和ddl
1.建表
use test
go

create table table1
(test varchar(10))
go

--仿照table1建立table2
select * from table1 into table2
go
--------------------------------------------------------------------------------------

--如果不能仿照需要修改一些参数
sp_helpdb db_name    --查看SELECT into选项是否开启,没开需要手动开启
go
 
use master
go

sp_dboption test,'select into',true        --(test为db_name)
go

checkpoint
go

use test
go

select * from table1 into table2
go



四、用户管理
1.创建登陆用户(login)
1> sp_addlogin ljw1911,rosantech,test    --依次为用户名,密码,默认数据库
2> go
Password correctly set.
Account unlocked.
New login created.
(return status = 0)


2.删除登陆用户(login)
1> sp_droplogin ljw1911
2> go

3.创建数据库用户(user)
1> sp_adduser ljw1911
2> go
New user added.
(return status = 0)

4.删除数据库用户(user)
1> sp_dropuser ljw1911
2> go


2.4.5 第六页了




五、sybase ASE参数配置
(1)服务器级配置
使用存储过程sp_configure配置ASE (和oracle的show parameter很像)
1. 语法
sp_configure [configname[, configvalue] | group_name | non_unique_parameter_fragment]
2. 作用
查询服务器运行的当前值
设置服务器运行参数

用 "sp_configure mem" 可以查看带有mem关键字的参数的具体配置,然后用 "sp_configure 参数, 值" 来配置新


例1. 配置内存
1)数据库使用内存
指定ASE拥有的总的共享内存大小
sp_configure“max memory”,nnn(单位为2K)( HP平台推荐物理内存的75%,其他平台80%)
sp_configure‘allocate max shared mem’,1(数据库服务启动时就分配内存)(推荐为1)
2)配置缺省数据缓存的大小和分区
sp_cacheconfig‘default data cache’,’xxxM’
sp_cacheconfig‘default data cache’,’cache_partition=n’
(根据CPU个数以及内存大小确定,需为2的N次方)
3)配置存储过程的缓存大小
sp_configure“procedure cache size”,nnn(单位为2K)

例2. 配置CPU
sp_configure‘max online engines’,n(cpu个数> 2时推荐CPU数目减1)
sp_configure‘number of engines at startup’,n
3. 其他
1). number of devices (用户自己确定)
指定ASE的可创建和打开的数据库设备的最大号数
2). number of locks (用户自己确定)
指定ASE可同时打开的锁的最大数目
3). number of open objects (推荐8000-20000)
ASE的对象描述的缓存数
4)number of open indexes (推荐2000-5000)
ASE的索引描述的缓存数
5) stack size (如果有超长SQL和多层嵌套,推荐*2)
数据库堆栈的大小
6). default character set id(推荐使用cp936 –id 171)
数据库服务器使用的字符集
7). allow updates to system tables
指定系统管理员是否可以更改系统表中的数据
8)row lock promotion HWM,row lock promotion LWM
行锁升级为表锁的阀值
9)page lock promotion HWM,page lock promotion LWM
页锁升级为表锁的阀值
10) lock scheme
缺省的对表加的锁类型(缺省为页锁)


(2)数据库级配置
使用存储过程sp_dboption更改数据库的数据库选项
1. 语法sp_dboption[dbname, optname, {true | false}]
2. 作用
罗列出所有的数据库选项
更改某个数据库的数据库选项
3. 常见的数据库选项
trunclog on chkpt自动清日志(不能再做增量备份)
select into/bulkcopy允许快速BCP,selectinto
ddlin tran允许事务中创建对象

修改完成以后可以通过sp_helpdb查看到

例子:
1> use master
2> go

1> sp_dboptionuserdb, "trunclog on chkpt", true
2> go
数据库‘userdb'的数据库选项'trunclog on chkpt'被打开。
在被改变数据库中运行CHECKPOINT命令。
(return status = 0)

1> use userdb
2> go
1> checkpoint
2> go



六、修改字符集为中文
1.先进入到中文字符集的目录,把中文加载一下
[sybase@rosan128main cp936]$ pwd
/opt/sybase/charsets/cp936

[sybase@rosan128main cp936]$ charset -Usa binary.srt cp936
Please enter sa's Password: 
Loading file 'binary.srt'.

Found a [sortorder] section.

This is Class-1 sort order.

Finished loading the Character Set Definition.

Finished loading file 'binary.srt'.

1 sort order loaded successfully

2. 修改默认字符集为中文
首先查一下默认的中文字符集的id
1> select name ,id from syscharsets where name='cp936'
2> go
 name                           id
 ------------------------------ ---
 cp936                          171

查出id为171,最后修改字符集
1> sp_configure "default character set id",171
2> go
In changing the default sort order, you have also reconfigured SQL Server's
default character set.
 Parameter Name                 Default     Memory Used Config Value
         Run Value   Unit                 Type
 ------------------------------ ----------- ----------- ------------
         ----------- -------------------- ----------
 default character set id                 1           0         171
                   1 id                   static

(1 row affected)
Configuration option changed. Since the option is static, Adaptive Server must
be rebooted in order for the change to take effect.
Changing the value of 'default character set id' to '171' increases the amount
of memory ASE uses by 4 K.
(return status = 0)

最后重启数据库使参数生效


七、查看数据库信息
sp_helpdb        --(查看所有数据库信息)
go
use test
go
sp_helpdbtest         --(查看test数据库详细信息)
go


八、查看ASE的远程服务器
sp_helpserver
go


九、查看数据库对象的信息
sp_help            --数据库对象名字
sp_helpdevice        --设备名字
sp_helpdb
sp_helptext        --存储过程名字

sp_spaceused         --查看一个当前库(需要use 数据库)或表占所占用的空间
sp_recompile (+usertable)    --重新编译存储过程和触发器。与该表相关联的存储过程和触发器在第一次运行时,自动重新编译


十、常用dbcc命令
dbcctraceon(3604)                     --(随后的dbcc命令结果输出到屏幕)
dbcctraceon(3605)                     --(随后的dbcc命令结果输出到错误日志文件)
dbcccheckalloc[ (database_name[, fix | nofix] ) ]     --(检查分配页)
dbcccheckcatalog[ (database_name) ]             --(检查系统表)
dbcccheckdb[ (database_name[, skip_ncindex] ) ]     --(检查数据库)
dbccchecktable( { table_name| table_id}         --(检查表)
[, skip_ncindex] )
dbcctablealloc( { table_name| table_id}         --(检查表分配页)
[, { full | optimized | fast | null }
[, fix | nofix] ] ) |


十一、常用系统表
sysdatabases        --记录所有数据库基本信息
sysusages        --数据库空间分配情况
sysdevices        --数据库设备信息
syslogins        --数据库服务器登录信息
sysusers        --数据库用户信息
sysobjects        --数据库对象表(U 用户表,P 存储过程)
sysprocesses        --进程表


十二、日常常规维护(性能优化)
1.更新统计信息
update statistics (+usertable)    --不会对表上锁,不影响业务,但比较耗资源 (optdiag)

2.整理数据库垃圾空间
重建表的聚簇索引
对行锁表执行reorg
如何加快上述操作 (配置I/O,并行)
注意:以上操作对表上锁,另外,必须有足够的空间来执行(剩余空间必须为最大表的1.2倍左右) (sp_spaceusedusertable)

3.重新编译存储过程和触发器
sp_recompile (+usertable)    --与该表相关联的存储过程和触发器在第一次运行时,自动重新编译


十三、备份和恢复
系统管理员每天必须作数据库的备份

备份的命令
1.全库备份
dump database userdb to stripe_device1 
stripe on stripe_device2

2.增量备份并删除日志
dump transaction userdb to stripe_device

3.仅删除日志(没有备份)
dump transaction userdb with truncate_only

4、日志满且truncate_only无法删除日志时
dump transaction userdb with no_log(慎用)


恢复的命令
1. load database database_name from stripe_device
恢复整个数据库

2. load transaction database_name from stripe_device
恢复日志


十四、几个常用的存储过程和命令
sp_who            (查看用户进程)
sp_lock            (查看数据库锁状况)
sp_help            (查看对象信息)
sp_helpdb        (查看数据库信息)
sp_helpdevice        (查看设备信息)
sp_spaceused        (查看表占用的空间大小)
select @@version    (查看版本号)
dbcc sqltext(@spid)    (查看@spid执行的sql语句)
sp_showplan @spid,null,null,null        (查看@spid的执行计划)


================================================================================================
************************************************************************************************
================================================================================================
修改server启动地址及端口:dscp

$dscp
>> open
ok
Session 1 InterfacesDriver>> add test
Service: [ASE]
Transport Type: [tcp]
Transport Address: 158.77.123.200 5000   --注意端口号和ip地址之间用空格分开
Transport Type: [tcp]
Transport Address:
Security Mechanism [] :
HA Failoverserver:
Retry Count:
Retry Delay:
Added test

>> exit

退出后查看 $SYBASE/interfaces 能看到添加到信息

dscp一些基本操作命令
help    查看帮助
list    显示所有的服务名
read    看具体配置
add    加一个新的
del    删除已有的
mod    修改已有的

原文地址:https://www.cnblogs.com/jggz/p/3140317.html