Sybase常用SQL语句

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

你可能感兴趣的:(sql,DB,Sybase,database)