当前位置:首页 > 开发 > 数据库 > 正文

Sybase常用SQL语句

发表于: 2014-10-22   作者:darrenzhu   来源:转载   浏览次数:
摘要: SQL online sample http://sqlzoo.net/wiki/Main_Page 重命名字段的名字 sp_rename 'table1.column1', 'new_column_name' 添加字段 ALTER TABLE table1 ADD new_column varchar(10) NULL 删除字段 alter table ta
SQL online sample
http://sqlzoo.net/wiki/Main_Page


重命名字段的名字
sp_rename 'table1.column1', 'new_column_name'

添加字段
ALTER TABLE table1 ADD new_column varchar(10) NULL

删除字段
alter table table1 drop column_to_be_deleted

修改字段的数据类型
alter table table1 modify column_name date null

Insert "Space","new line","tab" characters into a field as value
There are times when you need to insert some special characters such as "newline","tab" etc as values into a field. Usually every one tries with '\n' or '\t', in vain. we can do this by using the ASCII value of the character. The steps are very simple. if you want to insert a special character find the ASCII value of that character like '9 for tab', '12 for new line' etc. you can find the lists of characters with its ASCII values at https://awesomesql.wordpress.com/2009/08/10/ascii-character-set-table. Once this is done, concatenate your string with the character of that particular ASCII value as

'my string and'+ char(12)+'someting'


this will be

my string and

something



Check table Primary Key/Reference Key/Constraints
sp_helpconstraint tableName


-How to determine a Sybase servers character set and sort order
sp_helpsort

-how to check permissions on a table in sybase
sp_helprotect proc_name

-单引号(')转义
sybase里面用来转义的符号不是"\",而是单引号,所以
So how do you escape quotes in Sybase? In fact, in Sybase SQL the single quote acts as the escape character.
See below for an example UPDATE statement in both “languages”:
MySQL
UPDATE Animals SET NAME = 'Dog\'s friends' WHERE uid = 12
Sybase
UPDATE Animals SET NAME = 'Dog''s friends' WHERE uid = 12

另外"(" ")" ","是不需要转义的


--Get all tables for the specified column
SQL: select b.name as tablename, a.name as columnname
from syscolumns a join sysobjects b on (a.id = b.id)
where b.type='U' and a.name = 'column'


--Get all columns for the specified table
SQL: select b.name as tablename, a.name as columnname
from syscolumns a join sysobjects b on (a.id = b.id)
where b.type='U' and b.name = 'table'


获取Sybase数据库版本信息
There are two ways to know the about Sybase version,

1) Using this System procedure to get the information about Sybase version
SQL: sp_version

2) Using this command to get Sybase version
SQL: select @@version



who am I?
SQL: SELECT user, user_id()

When you are in a database that you own the answer is dbo (database owner). If you USE someone else's database then you get a user id. (the machine being used to connect to MySQL)


--To get list of all users and alias in database
select 'user:' 'Type', t1.suid, t1.name 'dbuser', t2.name 
from sysusers t1, master..syslogins t2 
where t2.suid=*t1.suid 
union 
select 'alias:' 'Type', t1.suid, ' ' 'dbuser', t2.name 
from sysalternates t1, master..syslogins t2 
where t2.suid=*t1.suid



--To get list of all users in database
SQL: use database
     select name from sysusers where udi<16384
or
SQL: select name from databasename.dbo.sysusers  where udi<16384

--suid is 'server user id ' is reference to sybase login which recorded in master.dbo.syslogins.
--16384 is a magic id number from which group names are recorded in sysusers table. (all with id> 16384 are group names not users)

--There is also what called 'aliases'. To see all sybase login that are aliased to another login in current database:
SQL: select suser_name(suid),suser_name(altsuid) from sysalternates
--Which mean whan anyone named in first column try to access database it will be treated as login from second column.
use master
--Next query will give you sybase database user name and corresponding server login (may be different )
SQL: select l.name,d.name from master.dbo.syslogins l, databasename.dbo.sysusers d where l.suid=d.suid


获取table的lock scheme
1) select lockscheme('tableName')
2) SELECT sysobj.* FROM (
select name, lockscheme(name) lockscheme, type from sysobjects
) as sysobj
where sysobj.type='U' and sysobj.lockscheme != 'datarows'

3) select convert(varchar(30),name) as OBJECT_NAME,
       "LOCKING_SCHEME" = case (sysstat2 & 57344)
         when 8192 then "all pages"
         when 16384 then "datapages"
         when 32768 then "datarows"
       end
from sysobjects where type in ("U") order by name

SQL for changing the locking scheme for a table
alter table table_name lock {allpages | datapages | datarows}

-Default value
Actually the default value of a column is a constraint for the table, you can use sp_helpconstraint TABLENAME to check.
Firstly we should use create default and drop default to achieve our goal.
To alter a default you need to use replace rather than modify:
alter table downloads replace is_completed default 0
If you need to change the data type or the null/not null then you should use
alter table t modify c

-Identity gap
Managing identity gaps in tables
The IDENTITY column contains a unique ID number, generated by Adaptive Server, for each row in a table. Because of the way the server generates ID numbers by default, you may occasionally have large gaps in the ID numbers. The identity_gap parameter gives you control over ID numbers, and potential gaps in them, for a specific table.

By default, Adaptive Server allocates a block of ID numbers in memory instead of writing each ID number to disk as it is needed, which requires more processing time. The server writes the highest number of each block to the table’s object allocation map (OAM) page. This number is used as the starting point for the next block after the currently allocated block of numbers is used or “burned.” The other numbers of the block are held in memory, but are not saved to disk. Numbers are considered burned when they are allocated to memory, then deleted from memory either because they were assigned to a row, or because they were erased from memory due to some abnormal occurrence such as a system failure.

Allocating a block of ID numbers improves performance by reducing contention for the table. However, if the server fails or is shut down with no wait before all the ID numbers are assigned, the unused numbers are burned. When the server is running again, it starts numbering with the next block of numbers based on the highest number of the previous block that the server wrote to disk. Depending on how many allocated numbers were assigned to rows before the failure, you may have a large gap in the ID numbers.

Identity gaps can also result from dumping and loading an active database. When dumping, database objects are saved to the OAM page. If an object is currently being used, the maximum used identity value is not in the OAM page and, therefore, is not dumped.

Setting the table-specific identity gap
Set the table-specific identity gap when you create a table using either create table or select into.

This statement creates a table named mytable with an identity column:

create table mytable (IdNum numeric(12,0) identity)
with identity_gap = 10
The identity gap is set to 10, which means ID numbers are allocated in memory in blocks of ten. If the server fails or is shut down with no wait, the maximum gap between the last ID number assigned to a row and the next ID number assigned to a row is ten numbers.

If you are creating a table in a select into statement from a table that has a specific identity gap setting, the new table does not inherit the identity gap setting from the parent table. Instead, the new table uses the identity burning set factor setting. To give the new table a specific identity_gap setting, specify the identity gap in the select into statement. You can give the new table an identity gap that is the same as or different from the parent table.

For example, to create a new table (newtable) from the existing table (mytable) with an identity gap:

select IdNum into newtable
with identity_gap = 20
from mytable

Sybase常用SQL语句

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
SQL语言是操作数据库的基础语言,对于不同的数据库语言产品,SQL语言大多是通用的,本篇博客旨在介
创建表空间 CREATE TABLESPACE mydb DATAFILE 'D:\ORACLE\PRODUCT\10.2.0\DB_1\DATABASE\mydb ' SIZE
ASA功能本来非常强大, 可是不理解为什么没有一个功能强大的描述一个表定义的简单的SQL语句。 像Oracl
目录 1)基本 2)数学函数 3)rownum 4)分页 5)时间处理 6)字符函数 7)to_number 8)聚合函数 9
in(包含) SELECT * FROM `test` WHERE id in (1,2,4) 结果 not in(不包含) SELECT * FROM `test`
BETWEEN(范围内) SELECT * FROM `test` WHERE id BETWEEN 1 and 3 (1到3的范围内) 结果 not BETWE
SELECT * FROM `test` WHERE `remark` LIKE '%学%' 结果
一、数据库的基本操作 数据库是用来存储表的地方,在数据库开始创建的时候,要先创建好DATABASE然后
一、数据库的基本操作 数据库是用来存储表的地方,在数据库开始创建的时候,要先创建好DATABASE然后
1. select SELECT LastName,FirstName FROM Persons; SELECT * FROM Persons; 2. where SELECT * FR
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号