read.pudn.comread.pudn.com/downloads152/ebook/663511/sybase 1.doc  · web viewsybase sql server...

59
Sybase SQL Server 11 Introduction Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database Engine - Includes a query optimizer Standard SQL: * DDL (Data Definition language), * DML (Data Modification language), * DCL (Data Control Language) Transact-SQL = SQL + flow-control (if and while), local variables, etc.. 4 main verbs: select, insert, update, delete. But even before these words - here are some often used commands (working with Sybase: FUC - Frequently Used Commands isql -Sserver -Uuser -Ppassword 1> use some_database 1> select db_name() 1> select @@servername 1> select user_name() 1> sp_helpdb 1> sp_helpdb dbname 1> sp_help 1> sp_help tablename 1> set rowcount 2 -- to limit number of rows 1> set rowcount 0 -- remove rowcount limitation 1> select * from sometable where 1=2 -- to see column names 1> vi -- to edit command 1>select name from sysobjects where type = "U-Table/P-SP/V-View" 1> quit Here are useful help commands: select name from sysobjects where name like "sp_help%" name 1 sp_help 2 sp_help_rep_agent 3 sp_help_resource_limit

Upload: others

Post on 20-Mar-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Sybase SQL Server 11

Introduction

Sybase SQL Server 11  - Has a Single-Process, Multi-threaded Database Engine  - Includes a query optimizer Standard SQL:     * DDL (Data Definition language),     * DML (Data Modification language),     * DCL (Data Control Language) Transact-SQL = SQL + flow-control (if and while), local variables, etc..

4 main verbs:  select, insert, update, delete.

But even before these words - here are some often used commands (working with Sybase:

 

FUC - Frequently Used Commands

isql -Sserver -Uuser -Ppassword 1> use some_database 1> select db_name() 1> select @@servername 1> select user_name() 1> sp_helpdb 1> sp_helpdb dbname 1> sp_help 1> sp_help tablename 1> set rowcount 2                  -- to limit number of rows 1> set rowcount 0                  -- remove rowcount limitation 1> select * from sometable where 1=2            -- to see column names 1> vi                   -- to edit command 1>select name from sysobjects where type = "U-Table/P-SP/V-View" 1> quit

Here are useful help commands: select name from sysobjects where name like "sp_help%"

name 1 sp_help 2 sp_help_rep_agent 3 sp_help_resource_limit 4 sp_helpartition 5 sp_helpcache 6 sp_helpconfig 7 sp_helpconstraint 8 sp_helpdb 9 sp_helpdevice 10 sp_helpextendedproc

Page 2: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

11 sp_helpexternlogin 12 sp_helpgroup 13 sp_helpindex 14 sp_helpjoins 15 sp_helpkey 16 sp_helplanguage 17 sp_helplog 18 sp_helpobjectdef 19 sp_helpremotelogin 20 sp_helprotect 21 sp_helpsegment 22 sp_helpserver 23 sp_helpsort 24 sp_helptext 25 sp_helpthreshold 26 sp_helpuser

 

SELECT

select <column-list> from <table-name> select au_lname, au_fname from authors select title_id, type, price, price * .1 from titles select * from publishers   (not recommended)

string concatenation: select au_lname + ", " + au_fname from authors

name ouput columns yourself - column alias select title_id, type, price "original price", price * .1 discount from titles select "Full Author Name" = au_lname +", " + au_fname from authors

remove duplicates with distinct : select distinct type from titles select distinct city, state from authors   (here distinct refers to a combination of city and state, so that each column by itself may have duplicate entries)

filtering rows with where select <column-list> from <table-name> where <condition> select au_lname, au_fname from authors where state="CA"

equality and inequality operators: = , <>   or != , > , >= , < , <= , !< , !> select type, title_id, price from titles where price * total_sales < advance (can be applied to string  comparison - default sorting order is ASCII)

logical OR and AND select au_id, city, state from authors where state="CA" or city="Salt Lake City"

Page 3: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

between and Ranges of Data: <expression> between <expression> and <expression> select title_id, price from titles where price between $5 and $10 equivalent to select title_id, price from titles where price >= $5 and price <= $10

not between: select title_id, price from titles where price not between $5 and $10 equivalent to select title_id, price from titles where price >= $5 and price <= $10

in (...) : select title_id, price from titles where type in ("mod_cook", "trad_cook", "business") equivalent to select title_id, price from titles where type = "mod_cook"   or type = "trad_cook" or type = "business"

not in (...) select title_id, price from titles where type not in ("mod_cook", "trad_cook", "business")

wildcards with like: % - any number (0 to many) of any characters _ (underscore) - any single character [ ]   any single character from those listed in the brackets (this is only for Sybase) [%] - actually match the % character [^A-C] - matches any character except A,B,C

select au_lname, au_fname, city, state from authors where city like "Spring%" select type, title_id, price from titles where title_id like "B_1342" select type, title_id, price from titles where title_id like "B[A-Za-z0]1342      Note - if you need to include the '_' character in your pattern - use 'escape' word, for example:             select * from titles where title_id like 'ABC\_%' escape('\')

ordering result sets with order by : select au_lname, au_fname from authors order by au_lname select au_lname, au_fname from authors order by au_lname, au_fname

order by position in the select list: select title_id, price, total_sales, price*total_sales "total dollar sales" from titles order by 4

Ascending and Descending Ordering select title_id, price, total_sales, price*total_sales "total dollar sales" from titles order by price*total_sales desc

default sort order is ascending. Example: sort by type (ascending) and then by total_sales (desc): select title_id, price, total_sales

Page 4: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

from titles order by type, total_sales desc

order by columns not Appearing in the Select List: select au_lname, au_fname from authors order by city, state

agregate functions: sum( ) - total numeric, avg( ) - average numeric, min( ) - lowest numeric or sorting string or earliest date, max( ) - highest numeric or sorting string or latest date, count( ) - returns the number of non-null expressions, count(*) - returns number of rows found

select avg(price) from title select avg(price) "avg" from titles where type = "business"

select avg(price) "avg", sum(price) "sum" from titles where type in ("business","mod_cook")

counting rows with count(*) : select count(*) from authors where state="CA"

Aggregates functions discard null values

sub-agregates with group by : select type, avg(price) "avg", sum(price) "sum" from titles where type in ("_business", "mod_cook") group by type

When two or more columns are included in group by statement, agregates are based on unique combinations of these columns: select type, pub_id, avg(price) "avg", sum(price) "sum" from titles where type in ("_business", "mod_cook") group by type, pub_id

In order for aggregates to properly subtotal (or subaverage or subcount) by non-aggregate values, all non-aggregate columns in the select list should be repeated in the group by clause (see color above).

Filtering results with having : where - selects rows before averaging: select type, avg(price) from titles where price > $10 group by type having select rows from the result set: select type, avg(price) from titles where price > $10 group by type having avg(price) > $20

Example: find duplicates of au_id: select au_id, count(*) from authors group by au_id having count(*) > 1

Page 5: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Worktable - a temporary table which is created before distinct, having or order by are applied.

select type, avg(price) from titles where pub_id="1289" group by type having avg(price) > $15 order by avg(price) desc

 

JOIN

JOIN Operations: join using common key (or join key): select title, pub_name from titles, publishers where titles.pub_id = publishers.pub_id

select title, pub_name from titles tinner join publishers p on t.pub_id = p.pub_id

select au_lname, au_fname, title from authors a, titles t, titleauthor ta where ta.title_id = t.title.id and a.au_id = ta.au_id and type="psychology"

select t.*, pub_name from titles t, publishers p where t.pub_id = p.pub_id

Outer join: left (*=) or right (=*): select pub_name, title from publishers p, titles t where p.pub_id *= t.pub_idand ...

select pub_name, title from publishers pleft outer join titles t on p.pub_id = t.pub_idwhere ...

This query retrieves all pub_names from publishers.  For any row that doesn't join successfully with titles, the title column will contain a null for that row in the output.

Subqueries - to be used instead of a constant: select title from titles where pub_id =   (select pub_id from publishers where pub_name = "Algodata Infosystems")(The above query assumes that subquery will return a single row with single column)

Subqueries with IN (or NOT IN ) can return multiple rows: select pub_name from publishers

Page 6: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

where pub_id in   (select pub_id from titles where type = "business")

subquery can be rewritten as a JOIN : select pub_name from publishers p, titles t where p.pub_id = t.pub_id and type = "business"

Subqueries with exists - the best way to eliminate duplicates: select pub_name from publishers p where exists   (select * from titles t where p.pub_id = t.pub_id and type = "business")

not exists and not in : select pub_name from publishers p where not exists   (select * from titles t where p.pub_id = t.pub_id and type = "business")

Unfortunately the "not exists" construct runs very slowly.A better (much-much faster) way (in 12.5) is to use the new syntax:

select pub_name from publishers p left outer join titles t on p.pub_id = t.pub_id and t.type = "business"where t.pub_id is null

This following syntax I don't like - create problems (char/varchar, nulls):

select pub_name from publishers where pub_id not in   (select pub_id from titles where type = "business")

Subqueries with agregates in the where clauses: select type, price from titles where price < (select avg(price) from titles)

union operations: select city, state from authors union select city, state from publishers (duplicates will be automatically removed from the result unless you use union all )

Here is one more example: select 95 "year", month, sum(dollar_sales) from salescurrent group by month union select 94 "year", month, sum(dollar_sales) from salescurrent group by month order by year, month

 

select into, insert, update, delete

Page 7: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Using select into : creates a table on the fly instead of returning the the output to the user: select distinct type into type_lookup from titles

Example: create an empty copy of an existing table: select * into new_salesdetail from salesdetail where 1 = 2

Adding rows with insert: insert authors (au_id, au_lname, au_fname, phone) values ("123-45-6789","Jones","Mary","212-555-1212")

Columns that are not specified will be set to their default values If you don't specify column names at all (which is very bad practice and dangerous) - then you have to provide values for ALL columns an in the right order:

insert publishers values ("1235","New World Printing","GA","Atlanta")  

inserting several rows with select: insert authors_archive (au_id, au_lname, au_fname, phone, city, state, zip) select au_id, au_lname, au_fname, phone, city, state, zip from authors where state="CA"

modifying rows with update: update publishers set pub_name = "Joe's Press" where pub_id = "1234"

Example: update based on a join: update titles set full_name = isnull(u.full_name,'') from db1..titles tit, db2..users u where tit.kid *= u.kid

Example: double all prices in the table: update titles set price=price*2

update several columns simultaneously: update contacts set address1 = "5 W. Main St.",      address2 = "Apt.3D",      state="CT",      zip="03838" where contract_id = 17938

Page 8: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

remove rows with delete: delete titles where type = "business"

Clearing the table: delete title   or truncate table titles

truncate is faster, but it doesn't log individual deletions - thus the ability to recover database is compromised.

 

Datatypes

Datatypes: Once you create a column of certain datatype - you can not change it without dropping and re-creating the table

create table my_table (    id int not null    value float not null    description varchar(30) null )

Storing strings: char - fixed length strings (max length - 255) varchar - variable-length strings (max length - 255) nchar - multi-byte character strings nvarchar - variable-length multi-byte char strings text - for very large strings (up to 2 GB of text per row)

Storing binary data: binary - for fixed-length binary strings varbinary - for variable-length binary strings image - for storing large binary strings (up to 2 GB of image data per row)

insert b_table (id, bin_col) values (19, 0xa134c2ff)

using timestamp: create table timestamp_example  ( id int not null,    code char(3) not null,    ts timestamp not null )

insert timestamp_example (id, code) values (17, "AAA")

The server uses the unique transaction-log-row id as a timestamp. You can not specify this value.

Optimistic locking: using tsequal() function to check the locking

Page 9: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Storing BLOBs (binary large objects) using text and image datatypes: create table texts (   id numeric(5,0) identity,   item int not null,   textstring text null )

using insert:

insert texts (item, textstring) values (1, null) insert texts (item, textstring) values (2) insert texts (item, textstring) values (3, "some text") insert texts (item, textstring) values (4, "some very long text")

insert statement will not insert more than 1,200 bytes. string concatenation on strings longer than 255 chars is illegal

using writetext and readtext: The idea is to get the pointer to the text chain and to write directly to this chain without modifying the underlying row. To do this you should start with a non-null popinter stored in the row itself.

Here is how to retrieve the pointer for a row: select id, textptr(textstring) textptr,   datalength(textstring) datalength from texts

to write a new textstring value to the 3rd row: declare @pageptr varbinary(16) select @pageptr = textptr(textstring)   from texts   where id = 3 writetext  texts.textstring @pageptr "some very long string"

to read 50 bytes starting from position 4000: declare @pageptr varbinary(16) select @pageptr = textptr(textstring)   from texts   where id = 3 readtext  texts.textstring @pageptr 4000 50 textstring

Date/Time datatypes: datetime (It can store time up to 3 milliseconds, 8 bytes) smalldatetime (It can store time up to 1 min, 4 bytes)

create table date_example (   id int not null   dateval  datetime not null)

insert date_example (id, dateval) values (19, "September 25, 1996 3:15PM")

you can use other forms of format, for example:  4/15/99

Page 10: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

select * from date_table where date = "9/3/95" select * from date_table where date >= "9/3/95" and  date < "9/4/95"

timestamp - a binary, ticks of some sort, it is not datetime

drop table #test

create table #test (  id numeric(18,0) identity,  ts timestamp,  dt datetime,  sm smalldatetime )

 insert #test(dt,sm) values (getdate(), getdate())

select * from #test datediff(dd,"15-Apr-2000","15-Apr-2001") - returns '365' where LastActivityDate > dateadd(yy,-2,getdate()) where LastActivityDate > dateadd(dd, -90, getdate())  

Logical datatype: bit - can not be indexed and can not be null: create table bit_sample (id int not null, description varchar(30) null, active bit not null)

Numeric datatypes: int - 4 bytes signed smallint  - 2 bytes signed tinyint - unsigned byte (0..255) float - 8 bytes real  - 4 bytes numeric(7,2) - you can indicate total number of digits and number of digits after decimal point. Can be used with identity (automatic counter) decimal - similar to numeric, but can't be used with identity money - 8 bytes smallmoney - 4 bytes (+/-214,748.3647)

create table numeric_example (   id numeric(8,0) identity,   num_col numeric(7,2))

insert dollar_table (id, dollars) values (95, $12345.93)

An IDENTITY column contains a value for each row, generated automatically by Adaptive Server that uniquely identifies the row within the table. Each table can have only one IDENTITY column

create table table_name ( ...) drop table table_name

Page 11: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

sp_help sp_help table_name

object's full name: database_name.owner_name.object_name

pubs.dbo.authors

Columns can have following properties: Null, Not null, Identity

 

Views and Temporary Tables

- Views - logical way of looking at data (you can treat them as tables): create view author_name as   select last = au_lname, first = au_fname from authors

select * from author_name drop view author_name

* note: dropping a view doesn't change data * views - help security by limiting access to tables * views - help make queries simpler * views can contain aggregate functions and grouping, joins, other views and a distinct clause. * views CAN NOT include "select into" , a compute clause, a union clause, and "order by" clause. * You can insert into, update and delete from views (restriction: allowed to affect only one base table). * If view includes columns from several tables, you can't delete rows from view or update columns from more than 1 table in a single update statement * You can not update, delete or insert into a view containing the distinct clause.

sp_help - lists all objects (including tables and views) to get a list of just the views: select name from sysobjects where type = "V"

to see a list of columns in a view: sp_help  view_name

Renaming objects (tables, views, columns.) using   sp_rename : sp_rename old_name new_name sp_rename 'table_name.old_col_name', 'new_col_name'

Adding columns to a table using alter : alter table tt add   middle_name varchar(20) null,   fax varchar(15) null

Page 12: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

NOTE: You can only add columns with null values. NOTE: You can't remove columns using alter. You have to recreate the table and indexes and insert data again, for example:

create table lev1 (c1 int not null,c2 int null) CREATE UNIQUE CLUSTERED INDEX lev1_idx ON lev1(c1) CREATE NONCLUSTERED INDEX lev1_qib_idx ON lev1(c1,c2)

insert lev1 values (1,2) insert lev1 values (2,2) insert lev1 values (3,2)

-------  add columns  ----------------- alter table lev1 add c3 int null, c4 int null -------  remove columns  ------------- exec sp_rename 'lev1','lev1_tmp' -- sp_rename 'lev1_tmp.lev1_idx', 'lev1_tmp_idx' -- sp_rename 'lev1_tmp.lev1_qib_idx', 'lev1_tmp_qib_idx'

create table lev1 (c1 int null, c2 int null) CREATE UNIQUE CLUSTERED INDEX lev1_idx ON lev1(c1) CREATE NONCLUSTERED INDEX lev1_qib_idx ON lev1(c1,c2)

insert into lev1 select c1,c2 from lev1_tmp drop table lev1_tmp   -- this will automatically drop indexes

Temporary Tables - are real tables created in the tempdb database.  They have '#' before table name.  They exist only for the duration of a user session (or stored procedure) and only accessible to this session. These tables are not recoverable (in case of a crush). select au_lname, au_fname, title, pub_id   into #titles_and_authors   from authors a, titleauthor ta, titles t   where a.au_id = ta.au_id and t.title_id = ta.title_id

Creating permanent Temporary table in tempdb:    select * into tempdb..titles from pubs2..titles

 

Rules & Defaults

Rules: create rule order_quantity as   @quantity between 100 and 150000

create rule color_rule as   @color in ('black', 'brown', 'red')

create rule pub_id_rule as   @pubid like ('99[0-9][0-9]')   or @pubid in ('0736', '0877', '1389')

Page 13: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

create rule date_rule as   @date >= getdate()

drop rule key_rule

Binding rule to a column: sp_bindrule rule_name, 'table.column_name' sp_unbindrule 'table.column_name'

Defaults: create default country_default as 'USA' create default age_default as 16 create default time_default as getdate( )

binding default to columns in a table: sp_binddefault default_name, 'table.column_name' sp_bindefault country_default, 'demographics.country' sp_unbindefault  'demographics.country'

you can define default for a column when you create a table. later you can change it using later table: alter table items   replace price default null

alter table items   replace item_code default "N/A"

To list all rules and defaults in a database: select name from sysobjects where type in ("R", "D")

To examine the rules and defaults bound to the column in a table: sp_help table_name

sp_helptext object_name

Creating User-defined Datatypes: sp_addtype ssn_type, 'char(9)', "not null"

 

Indexes and Constraints

- Indexes: 2 types of indexes - clustered and nonclustered. Both are B-tree. Clustered - only one clustered index per table can exist (data is maintained in clustered index order). Nonclustered indexes - you can have 249 of them per table. They maintain pointers to rows (not data pages). An index can contain up to 16 columns, but total index width <= 255.

Creating indexes: create unique clustered index name_index  on authors (au_lname, au_fname)

Page 14: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

create index fname_index on authors (au_fname, au_lname) ***********************- Constraints: Primary Key and Unique Constraints Check Constraints Referential-integrity Constraints Primary-Key Constraints Foreign-Key Constraints Modifying Constraints Adding Constraints Removing Constraints Information on Constraints: sp_helpconstraint ***********************- Comparing Data-integrity Methods: Rules, Defaults, Indexes, Constraints, Keys (Primary Key, Foreign Key (primary key from another table), Common key)

 

T-SQL programming constructs

- T-SQL programming constructs: standard arithmetic operations ( + - * / ^) string functions: datalength(), substring(), right(), upper(), lower(), space(), replicate(), stuff(), reverse(), ltrim(), rtrim(), ascii(), char(), str(), soundex(), difference(), charindex(), patindex()

Note: to include quotes into string you can repeat it 2 times (or one time if the surrounding quote is of different type):

"    ""     " '     ''     ' "    '       " '    "       '    'I don''t know'      "I don't know"      " Title ""The Title""  was on sale"        ' Title "The Title"  was on sale'

math.functions: abs(), ceiling(), exp(), floor(), pi(), power(), rand(), round(), sign(), sqrt() date functions: getdate(), datename(), datepart(), datediff(), dateadd()

convert - to convert data types: select "Advance = " + convert (char(12), advance) from titles (can also convert dates)

system functions:  host_id(), host_name(), suser_id(), suser_name(), user_id(), user_name(), user, show_role(), db_id(), db_name(), object_id(), object_name(), col_name(), col_length(), index_col(), valid_name(), datalength(), tsequal()

compute, compute by: allows to include both detail and summary info in a single result set

Page 15: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

isnull - function tells to treat null values as zero or something else.

Batches:   several commands followed by 'go' command Comments:  /* */    or --Local variables:    declare @variable_name datatype    declare @myname varchar(30), @type int    select @myname=12    go

Note: name length is limited to 30 char (including @)

Global variables - can not be defined by users.  (examples:  @@rowcount, @@error, @@trancount, @@transtate, ...)

Message handling:   print "this is a message"   declare @msg varchar(30)   select @msg = "Hello " + user_name()   print @msg Error handling:    raiserror 52345 'Row not found' if .. else: if (select ...)   print "sometext" else   print "some_other_text" while: while(select ...)   begin     update ...

    continue     break

  end goto: metka: select ... ... goto metka waitfor: waitfor time "22:00:00" while 1<2 begin   waitfor delay "00:00:30"   exec sp_who end return: to exit the batch set: set rowcount 100 set statistics time on set nocount on

Page 16: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

... Cursors - allow custom processing for each row: declare leads_curs cursor for select cust_id, ets-sale  from leads for read only go

open leads_curs while () begin .. end close leads_curs deallocate leads_curs go

You can do also custom updating with cursors. In general you should avoid cursors - try to substitute them with complex select statements (usually will run x10 faster)

 

Triggers

***************create trigger titles_trigger   on titles   for insert as print "title inserted" return ***************- drop trigger trigger_name ***************- create trigger cascade_del_trigger   on publishers   for delete as if @@rowcount = 0 -- no rows deleted, exit trigger   return delete titles from titles t, deleted d where t.pub_id = d.pub_id

if @@error != 0   begin   print "Error occurred during deleting related titles"   end

return ***************- create trigger tr1 on ... for insert, update as declare @rows int select @rows = @@rowcount ... if (.. )   ... ***************- In triggers you CAN NOT do these:

Page 17: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

  create, drop, alter table/database, grant, revoke, select into, truncate table, update statistics, reconfigure, load database/transaction, disk init/mirror/reinit/refit/remirror/unmirror ***************- Triggers During transactions: ... begin tran add_titles insert ... insert ... commit tran ***************- rollback trigger - command to rollback the trigger

Nested Triggers - 16 levels of nesting max

 

Stored procedures

Stored Procedures: * faster (precompiled in memory) * reduced network traffic * modular programming * restricted access to tables * reduced operator error * enforced consistency * automated complex or sensitive transactions * security, especially for the banks ;)***************- create proc pub_titles as select .. from ... where ... return

To execute a stored procedure: call by name if it is a first command in the batch or use "exec" (or "execute" command):   exec proc_name

To delete the procedure: drop proc proc_name

To change the procedure: if exists (select * from sysobjects where name = "titles_for_a_pub" and type = "P" and uid=user_id())   drop proc titles_for_a_pub go create proc titles_for_a_pub as ...

To display the text of the procedure: sp_helptext proc_name

passing parameters: create proc tt (@pub_name varchar(40)) as select ...where ... return

Page 18: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

declare @my_pname   varchar(40) select @my_pname ="Some text" exec tt @my_pname

Default Parameter Values: create proc proc_name    p_name datatype = default_value    , .... as SQL Statements return ...

----- Output parameters: create proc tt (    @par1 varchar(80) = null,    @par2 int output) as -- some T-SQL statements go here return

declare @myoutput int exec  tt    @par1 = 'sometext',    @par2 = @myoutput  output go

----- Returning procedure status: return some_int_value 0 - no errors *1 .. -99 - detected errors (page 194 - list of error status codes) other values - whatever you like

declare @staatus_var int exec @staatus_var = proc_name

Note: A stored procedure may NOT: create views, defaults, rules, triggers or procedures, or issue the use statement Note: If stored procedure references a table, and the table was changed - you may have to drop procedure, change it and re-create it. When created, stored procedure is transformed into a cashed sequenced of SQL statements. If procedure has "with recompile" - it forces the optimizer to generate a new query plan for each execution.

----- Returning data from one SP into a caller-SP: ----------------------------------- ----- transparent passing the result value ----------------------------------- create proc test1 as select count(1) N from OE create proc test2 as exec test1 exec test2 ----------------------------------- ----- catch the output value in the caller SP: ----------------------------------- create proc test3(@a int, @b int output) as select @b = @a*@a

Page 19: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

create proc test4 as begin   declare @par_in int   declare @par_out int   declare @ret_status int   select @par_in = 3   exec @ret_status = test3 @par_in, @par_out output   if not @ret_status=0 begin select @par_out = 0 end   select @par_out   select @par_out+1 end

exec test4 ----------------------------------- ----- transparent passing the result set ----------------------------------- create proc test5 as select * from mytab create proc test6 as exec test5 exec test6

----------------------------------- ----- handling errors, transactions ----------------------------------- create proc mysp as begin    declare @tran_cnt int    declare @errs int    declare @rows int    select @tran_cnt = @@trancount

   if @tran_cnt = 0        begin transaction mytran    else        save transaction mytran

   -- do something useful    -- then check for errors    select @errs = @@error    if (@errs != 0)    begin      rollback transaction sbl_upd_ticker      return @errs    end

   select @loc=loc, @idx=idx from mytab    if not @@rowcount=1    begin       rollback transaction mytran       raiserror 30200       return 30200    end

Page 20: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

   if @tran_cnt = 0        commit transaction mytran

   select @my_id = (@loc * 10000000) + @idx    return 0 end

Objects referenced in a procedure: exec sp_depends proc_name exec sp_depends table_name

 

Transactions

Transactions - several statements grouped together and either commited or rollbacked all together (all individual steps are recorded into a transaction log for recovery in case of a crash) begin tran save tran rollback tran commit tran set chained    @@tranchained  0 - chained mode is off, 1 - chained mode is on

begin transaction    update something    insert something if ...   begin  ... commit transaction end else   begin ... rollback transaction end

Note: transactions put locks on pages (2K blocks of data)

savepoints inside transaction: save tran item999 ... rollback tran item999

Nested Transactions: @@trancount

stored procedure with a transaction inside

create proc p1 as declare @trncnt int select @trncnt = @@trancount     -- save value if @trncnt = 0   begin tran p1 else

Page 21: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

  save tran p1

/* do some work here */

if (@@transtate = 2)  -- or other error condition   begin   rollback tran p1   return 25    -- error code indicating rollback   end

/* some more processing if required */

if @trncnt = 0   commit tran p1

return 0  -- success

nesting transactions - batch with transaction calls a stored procedure with a transaction inside

  .....

declare @status_val int,  @trncnt int select @trncnt = @@trancount     -- save value if @trncnt = 0   begin tran t1 else   save tran t1

/* do some work here */

if (@@transtate = 2)  -- or other error condition   begin   rollback tran t1   return     -- exit batch    end

execute @status_val = p1  -- call procedure with transaction from inside transaction - thus nesting transactions

if @status_val = 25  -- if proc p1 performed rollback   begin   rollback tran t1   return   end

if @trncnt = 0   commit tran t1 -- decrements @@trancount to 0

return 

triggers may be part of a transaction:

Page 22: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

begin tran   update titles     set price=$99     where title_id = "BU1234" commit tran

rollback transaction in a trigger rollback trigger

Transation modes:

- chained mode - implicitly begins a transaction before any data retrieval or modification statement (delete, insert, open, fetch, select, and update). You must still explicitly end the transactionwith commit transaction or rollback transaction. - unchained mode (default) - requires explicit begin transaction statements paired with commit transaction or rollback transaction statements to complete the transaction.

 

Getting info from sys. tables

some examples using sys tables

-------------------------------------------------------- select name from sysobjects where sysstat & 7 = 3 order by name -------------------------------------------------------- select   rowcnt(t2.doampg),   (reserved_pgs(t2.id, t2.doampg) + reserved_pgs(t2.id,t2.ioampg)),   (t3.low), data_pgs(t2.id, t2.doampg), data_pgs(t2.id,t2.ioampg),   (      (reserved_pgs(t2.id, t2.doampg) + reserved_pgs(t2.id,t2.ioampg)) -      (data_pgs(t2.id, t2.doampg) + data_pgs(t2.id,t2.ioampg))   ) from sysobjects t1, sysindexes t2, master.dbo.spt_values t3 where t2.id = object_id('myTableName')   and t1.id = object_id('myTableName')   and t3.number = 1 and t3.type = 'E' ------------------------------------------------------- select t1.name, t2.name, t1.length, t1.status from syscolumns t1, systypes t2 where t1.id = object_id('my_name') and t1.usertype *= t2.usertype -------------------------------------------------------- select t1.indid, t1.name from sysindexes t1 where t1.id = object_id('my_name') and t1.indid > 0 and t1.indid < 255

Page 23: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

-------------------------------------------------------- select index_col('my_name', indid_buf, keyid_buf, ...) -------------------------------------------------------- select t1.name from master.dbo.spt_values t1, sysindexes t2 where t2.status & t1.number = t1.number and t2.id = object_id('my_name') and t2.indid = my_some_indid_buf and t1.type = 'I' and t1.number = 2 -------------------------------------------------------- select t1.name from syssegments t1, sysindexes t2 where t1.segment = t2.segment and t2.id = object_id('my_name') and t2.indid = my_some_indid_buf -------------------------------------------------------- select replstat = convert(bit, (t1.sysstat & -32768)) from sysobjects t1 where t1.id = object_id('my_name') --------------------------------------------------------

 

Stored procedure with a cursor

if exists (select 1 from sysobjects where name = "myname")    drop proc myname go

create proc myname as begin   declare @my_id int   declare @root_my_id int   declare mycursor cursor for (select my_id from MT)   open mycursor   fetch mycursor into @my_id   while ((@@sqlstatus = 0))   begin     exec @root_my_id = MT_get_root @my_id     insert into MT_root_map_tmp (my_id, root_my_id)     values (@my_id, @root_my_id)     fetch mycursor into @my_id   end   close mycursor   deallocate cursor mycursor end go  

 

Page 24: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

script to copy data between databases

You can copy data between databases using migrate utility in DBArtisan. Or you can use this script (it accepts table name as a parameter):

#!/bin/sh -v

if [ $# != 1 ]  then      echo "Usage: $0 <table>"      exit 1 fi 

table=$1 

PROD="-S SERV1 -U user1 -P pass1" DEV="-S SERV2 -U user2 -P pass2"

bcp mydb..$table out $table.dat $PROD -n > /dev/null

isql $DEV -w250 <<EOF use mydb go delete $table go EOF

bcp mydb..$table in  $table.dat $DEV -n > /dev/null

isql $DEV -w250 <<EOF use mydb go update statistics $table go EOF

 

misc

count distinct types:  

select dept, count(*) from mytable where dept is null group by dept union select distinct dept, count(*) from mytable where dept is not null group by dept

select distinct dept, count(isnull(dept,'0'))  from mytable group by isnull(dept,'0')

Page 25: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

when you joining tables - they join only on not null values.

The common situation is joining 2 tables through a middle table which maps keys between the side 2 tables. This works good for normal inner joins:  select ... where A.x = B.x and B.y = C.y

But what if you want all values from A - and just fill with nulls other columns. You try left join - but it doesn' work: select ... where A.x = B.x and B.y = C.y  -- doesn't work

You can either do it in 2 steps (make a temporary table) - or use union:

 

example of a union: select #t1.*  from #t1,#t2  where #t1.bb = #t2.bb union select t1.*  from #t1 t1 where not exists (     select *     from #t2 t2      where t1.bb = t2.bb   )

Getting date difference as part of the year in Sybase

-- select convert(numeric(6,2), datediff(mm, '4/1/02', '1/1/03'))/12.00 -- select convert(numeric(6,2), datediff( dy, '4/1/02', '1/1/03' ))/365.00 -- select convert(numeric(6,2), datediff( dy, '4/1/02', '7/1/02' ))/365.00

Catching special characters:

select count(*) from mytab where charindex(char(10), myname) !=0

The simple way to find all occurencies of special characters is to use a short perl script. Below is the code (do_query is a self-made subroutine returning the result set as an array of hashes):  

my ($sql,$result,$row,$oe_id,$name,@ar,%hh); $sql = qq{    select id, name from mytab    where name like '%[^0-9A-Za-z -_.,''""%]%' };  $result = &do_query($dbh, $sql); for $row (@$result) {   $id = $row->{id};   $name = $row->{name};   @ar = unpack('C*', $name);   %hh = ();   for(@ar) {if($_<32 or $_>127){$hh{$_}++}}   for(sort keys %hh){print"id <$id>: code $_ - $hh{$_} times\n"}

}

declare @a varchar(30)

Page 26: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

select @a = "a b" select @a = str_replace(@a, char(32), rtrim('')) select @a -- the above returns 'ab' in Sybase 12.5.1 and later, but returns NULL in 12.5.0, which is a bug -- The alternative is to use a while loop: declare @ss varchar(10) select @ss="a b" while (charindex(" ",@ss) > 0) select @ss = substring(@ss, 1, charindex(" ",@ss)-1) + substring(@ss, charindex(" ",@ss)+1, 255) select @ss ===========================

Using 'union' to do logical operations in single statement without creating temporary tables:

Let's say we have a table with 2 columns (a,b):

create table #t (a int null, b int null) insert #t (a,b) values (-1,6) insert #t (a,b) values (0,5) insert #t (a,b) values (1,4) insert #t (a,b) values (2,3) insert #t (a,b) values (3,2)

and we  want to return max(a) or max(b) depending on which one is larger. Here is how this could be done    

select max(b) mm from #t  having max(b) > (select max(a) from #t) union select max(a) mm from #t  having max(b) > (select max(b) from #t)

 

tune your SQL

Query Tuning Options set forceplan on  -  forces SQL Server to join the tables in a query in the order in which they are listed in the from clause. set table count  -  allows you to specify the number of tables that are considered at once as joins are optimized. sp_cachestrategy  -  disables and reenables cache strategies and large I/O for individual objects. specify the index, cache strategy, or I/O size for a query (for example: select * from mytab (index mytab_idx) where ...) set prefetch  -  allows you to enable or disable large I/O for a session.

Benchmarking your query by hand:    set statistics time on

Page 27: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

   set statistics IO on   

Help your optimizer to make sure it has correct information about your table and uses it for queries and stored procedures. This info may me incorrect, for example, after bulk loads:    update statistics  tabname    sp_recompile  tabname

Examine the showplan of your query:   set showplan on   go   set noexec on   go

or for a stored procedure    set showplan on    go    set fmtonly on   go

Note: do not mix set noexec   &   set  fmtonly

Run your code - examine the output - look where table scans are made instead of using indexes. Sometimes index is not used because:   -  it doesn't exist on the column used in the where clause or in a join   -  there is a datatype mismatch in the join (like integer joined to numeric)   -  if the query is not restrictive enough and returns significant portion of the table.  The optimizer then will choose to use table scan.   -  the indexes are very complex - so Optimizer decides that it is less costly to just read the table itself   -  some where cluases may cause problems: order by, group by, having, between, like, calculations in where clauses.   -  sometimes table scan is really more effective and you don't need to eliminate it.

Note, that database can cashes data, so repeating the query may get the results faster. Simple solution - vary the query.

dbcc traceon(302, 310, 3604) sp_sysmon etc.  

Dynamic SQL

-- "dynamic SQL" in Sybase is called "execute-immediate" (EI)

exec("select getdate()") declare @c varchar(25) select @c = "select getdate()" exec (@c) select @c = 'mycolumn'

Page 28: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

execute( 'select ' + @c + ' from mytable where 1=2' ) -- You can do line continuation like this: exec (' \ create procedure myproc \ @part1 int \ as \ select KeyCol, Attrib \ from MyTable \ where KeyCol = @par1 \ ') -- Note: -- temp tables (#mytemp) created inside are dropped on exit -- local variables created outside can not be used inside - and vice versa -- some commands are not allowed inside: use, set, dbcc, connect, begin transaction, commit, rollback, dump transaction -- can't call EI from inside another EI, inside a trigger, -- statements executed in EI can't access tables "inserted" and "deleted" -- and EI doesn't return the result status -- cursor usage is limited (and pointless) -- Passing info back from EI - via table -- EI sets @@rowcount and @@error as any other SQL

Real-life example (extracted from the middle of a long stored proc):

declare @mysql varchar(1024)select @mysql = "insert #client1cas (id_cas) "select @mysql = @mysql + "select w.id_cas "select @mysql = @mysql + "from jds_words_cas w (index IX_words) "select @mysql = @mysql + "where w.words like '" + @client_str + "%' "select @mysql = @mysql + " and w.id_cas != null and w.id_cas != '' "set rowcount @max_mid_rowsexecute(@mysql)select @myrowcount = @@rowcountset rowcount 0if (@mydebug = 1) print "________ inserted %1! rows", @myrowcountif(@rows_cas >= @max_mid_rows) begin select @many_clients_flag = 1 goto RETURN_RESULTS end

 

Getting Info About Rows and Sizes of Tables

select Table_Name=object_name(id), "Rows"=convert(int,sum(rowcnt(doampg))), "ReservedKB"=sum(reserved_pgs(id,doampg))*2+ sum(reserved_pgs(id,ioampg))*2, "Data"=sum(data_pgs(id,doampg))*2 , "Index"=sum(data_pgs(id,ioampg))*2 from sysindexes where id in (select id from sysobjects where type='U') group by object_name(id)

Testing Outer Joins

Page 29: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

When chaining joins, >> , =>, <> are OK, but >= is not allowed (even on a different key)

-- testing outer joins create table #t1 (c1 varchar(10) null, c2 varchar(10) null) create table #t2 (c1 varchar(10) null, c2 varchar(10) null) create table #t3 (c1 varchar(10) null, c2 varchar(10) null) go -- row.col insert #t1 values('1.1','1.2') insert #t1 values('2.1','2.2') insert #t1 values('3.1','3.2') insert #t2 values('1.1','1.2') insert #t2 values('2.1','2.2') insert #t2 values('4.1','4.2') insert #t3 values('1.1','1.2') insert #t3 values('2.1','2.2') insert #t3 values('5.1','5.2')

select t1.*, t2.* from #t1 t1, #t2 t2 where t1.c1 = t2.c1 select t1.*, t2.* from #t1 t1, #t2 t2 where t1.c1 *= t2.c1

-- t1 < t2 > t3 select t1.*, t2.*, t3.* from #t1 t1, #t2 t2, #t3 t3 where t1.c1 =* t2.c1 and t2.c1 *= t3.c1

-- t1 > t2 > t3 select t1.*, t2.*, t3.* from #t1 t1, #t2 t2, #t3 t3 where t1.c1 *= t2.c1 and t2.c1 *= t3.c1

-- t1 < t2 = t3 select t1.*, t2.*, t3.* from #t1 t1, #t2 t2, #t3 t3 where t1.c1 =* t2.c1 and t2.c1 = t3.c1

-- t1 > t2 < t3 - meaningless select t1.*, t2.*, t3.* from #t1 t1, #t2 t2, #t3 t3 where t1.c1 *= t2.c1 and t2.c1 =* t3.c1

-- t1 > t2 = t3 - not allowed select t1.*, t2.*, t3.* from #t1 t1, #t2 t2, #t3 t3 where t1.c1 *= t2.c1 and t2.c1 = t3.c1

-- t1 > t2 = t3 - not allowed even on a different key select t1.*, t2.*, t3.* from #t1 t1, #t2 t2, #t3 t3 where t1.c1 *= t2.c1 and t2.c2 = t3.c2

print, raiserror- ===============================================Example of using print with formatting string:===============================================declare @a varchar(20)select @a = convert(varchar(20),getdate(),112)

Page 30: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

print @a

declare @b varchar(20)select @b = 'I am b'print @b

print "MYMY %1! and %2! MYMY", @a, @b

===============================================Few words about raiserror===============================================raiserror is similar to print command, but it also sets the @@error global variable to a int nubmer.Sybase has its own errors with numbers <20000.(see their descriptions in table master..sysmessages)

select count(1) from master..sysmessagesselect count(1) from master..sysusermessages

You can create your own custom errors - either in sysusermessages - or on the flyExamples of usage

if @@error != 0beginraiserror 300004 , "some message"return -1end

raiserror 300002 , "MYMY %s MYMY ", @myvarraiserror 300002 , "MYMY %1! MYMY ", @myvar

-- error 300006 doesn't exist in the sysusermessages table.-- so we call it without a comma after the error number and specify the message

raiserror 300006 "MYMY %1! MYMY", @myvar

Empty strings:

select @country = isnull(rtrim(ltrim(@country)),"")

note that empty chars are expanded inside the char_length() function.Thuschar_length(""+"") = 2

A workaround this is to rtrim everything inside the char_length - and use isnull outside the char_length:isnull(rtrim(@nm_lst)),0)

Note here that rtim("") returns null, so char_length(rtrim("")) is null also, and not zero.Isn't Sybase great, intuitive and easy to understand !!!

Page 31: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Replacing in a string:select @nm_frst = str_replace(@nm_frst, "'", "''")

Example of Printing:

exec myprint @mydebug,63,"starting"if (@mydebug = 1) print "We limit the rowcount globally to %1! rows",@set_glob_rowcount

-

create procedure myprint (@myflag int = 1, -- 1 or 0 - to print or not@myline int = 0, -- the line number@mystring varchar(200) = "" -- the message string) asbegin------------------------------------ usage-- myprint 1,57,"some message"-- or in stored proc:-- myprint @mydebug, 57,"some message"----------------------------------if (@myflag != 1)returndeclare @mydate varchar(30)select @mydate=convert(varchar(30),getdate(),109)print "%1! === line ~ %2! === %3!", @mydate, @myline, @mystringend

Example of reporting a duration of some process:

declare @mysec datetime select @mysec = getdate()

-- do something

if(@mydebug=1) begin select @ss = "FINISHED, total run time = " + convert(varchar(20),datediff(ss,@mysec,getdate())) + " seconds" exec myprint @mydebug,1012,@ss end

Example populating a "suffix" table (chopping words from the beginning until nothing left). This table is used as an index table to speed up search of words/phrases inside the string. It allows to use "starts with" logic ( ... where ss like 'xxx%' ), which uses index - thus it is fast.

CREATE PROCEDURE dbo.ins_words (@words varchar(100) = null,@id varchar(32) = null,) as BEGIN

Page 32: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

-- clean the @wordsselect @words = upper(rtrim(ltrim(@words)))select @words = str_replace(@words, char(10), " ") -- \nselect @words = str_replace(@words, char(9), " ") -- \tselect @words = str_replace(@words, "|", " ") -- we do this to be able to bcpselect @words = str_replace(@words, "-", " ") -- dash is also a separatorwhile (charindex(' ',@words)>0) select @words = str_replace(@words, " ", " ") -- remove double spaces

-- we insert the whole string (even if it is empty - we still want to enter the id to make future updates easier)insert mytab (words, id) values (@words, @id_cas)if (rtrim(@words) = null)return

declare @pos intselect @pos = charindex(' ', @words)while (@pos > 0) -- have to insert substring(s)begininsert mytab (words,id) values (substring(@words,(@pos+1),200), @id)select @words = substring(@words,(@pos+1),200)select @pos = charindex(' ', @words)end

END

Update with variables technique:

-- Example 1declare @mycount int select @mycount=0update jds_contact set @mycount = @mycount + 1select @mycount

-- We can do more interesting things - for example, concatenate values for the same id.let's say we have 2 columns (id, name).first, we create a temporary work table with 4 columns:(id, id_prev, name, names)

This table should have only fixed-width fields (no varchar). This is necessary so that the table will be written to the same place - thus preserving th order of rows.Next we insert data into this table (order by id).Next we update the id_prev field:

select @prev = 0update #work set id_prev = @prev, @prev = id

Next we do concatenation:

select @ss=""update #workset names = case when id != id_prev then rtrim(name) else rtrim(@names)+',

Page 33: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

'+rtrim(name) end,@ss = case when id != id_prev then rtrim(name) else rtrim(@names)+', '+rtrim(name) end

Finaly select the result:

select id, max(names) from #work

Example of batch inserting using id:

declare @myrowcount int select @myrowcount=1declare @min_id int, @max_id int, @base_id int

select @min_id = min(id), @max_id = max(id) from sourceselect @base_id = @min_id - 1select * into #batch from dest where 1=2

set rowcount 5000

while (@myrowcount > 0)

begindelete #batch

insert #batchselect * from source twhere t.id > @base_id order by t.id

select @myrowcount = @@rowcount

select @base_id = max(id) from #batch

if (@myrowcount > 0)begininsert dest select * from #batchprint "inserted = %1! (max = %2!)", @base_id, @max_idend

end

set rowcount 0

Reducing the locking problem.

Read - put shared lock, update or insert - exclusive lock.

Table locks may be a result of lock promotion, but more often they happen because of your SQL / indexes. Normally table read puts a shared lock. But if you have an UPDATE or a DELETE with a WHERE clause which has a table scan - you will get an

Page 34: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

exclusive table lock without any lock promotion, regardless of locking scheme.

Switching to row level locking (DATAROWS locking scheme) may not solve your problem and is very expensive: your data takes up more space, UPDATES start causing a lot more fragmentation, and you need A LOT more locks. Lock promotion works by number of locks used, and since DATAROWS uses considerably more locks, you'll hit promotion threshold much sooner. Also if you run out of locks – you will also get a promotion to a table lock. Also, if you are committing more rows at a time than the HWM (high lock promotion threshold) – then you will still get lock promotion even with datarows locking scheme.

There are 3 locking schemes: - ALLPAGES (data and indexes)- DATAPAGES (only data)- DATAROWS (only data, row-level locking)

Use sp_lock and sp_objectstats to see if multiple connections/users attempt locks on rows on the same page. Use sp_sysmon to detect lock promotion.

Lock promotion thresholds :LWM = 200 - never attempt a promotion below 200 locksHWM = 200 - always attempt promotion once 200 are reached(HLW - High WaterMark, LWM - Low WaterMark)Number of locks = 35,000

Note: Switching to DATAROWS might help your blocking, but maybe the problem isn't that people are trying to read the same row, or even different rows on the same page. It might be that they're blocking on data vs index access. DATAPAGES is a good first step to try since it eliminates index locking and usually this is all you need. It has most of the benefits of DATAROWS without the extreme overhead. It's always a good idea to try DATAPAGES first.

Also, the solution to avoid table locks may be simply to increase the lock promotion thresholds.

Note: changing from ALLPAGES to DATAPAGES or DATAROWS is an expensive operation. Switching between DATAPAGES and DATAROWS is very easy and fast.

Note: if a WHERE clause has local variables – optimizer will not use index, thus causing a table scan.

Example:Modifying locking scheme:

alter table authors lock datarows

Isolation Level

You can change isolation level using a command like this:

         set transaction isolation level 0

Page 35: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

The value is stored in variable @@isolation

Possible values:    0 - read uncommitted changes to data (allow dirty reads).    1 - read committed only (default)    2 - repeatable read - The transaction can repeat the same query, and no rows that have been read by the transaction will have been updated or deleted.    3 - serializable read - The transaction can repeat the same query, and receive exactly the same results. No rows can be inserted that would appear in the result set.

You can override the isolation level by adding a hint to the end of SQL statement:

with holdlock - force level 3with noholdlock - force level 1

AutoCommit

set autocommit - the client-side commant to set autocommit on | off | 0 | 1 - turns autocommit on or off.

Duplicates

ignore_dup_key - hint to skip duplicate keys while doing insert or update. Note - you cannot create a unique index on a column that includes duplicate values or more than one null value, whether or not ignore_dup_key is set. If you attempt to do so, Adaptive Server prints an error message that displays the first of the duplicate values. You must eliminate duplicates before Adaptive Server can create a unique index on the column.

ignore_dup_row - allows you to create a new, nonunique clustered index on a table that includes duplicate rows. ignore_dup_row deletes the duplicate rows from the table, and cancels any insert or update that would create a duplicate row, but does not roll back the entire transaction.

Example: to delete duplicate rows:        create index index_name on table_name with ignore_dup_row

allow_dup_row - allows you to create a nonunique clustered index on a table that includes duplicate rows, and allows you to duplicate rows with update and insert statements.

tempdb, transaction log,

You may have many databases on the same server. But until ver. 12.* there was only 1 tempdb database. This was creating problems, as many sessions were competing for it. Starting with ver. 12 you may have several database - thus distributing the load.

Usuallythe size of tempdb is ~2 GBytes, however I've seen it as big as 12.5 GBytes -

Page 36: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

and completely buffered (mapped to memory).

When you have large transactions - you may want to increase the sizes of both tempdb and transaction log. But if you have replication - you want to limit the size of transactions to ~ 3,000 rows.

Checkpoint event

Approximately once a minute, the checkpoint task checks each database on the server to see how many records have been added to the transaction log since the last checkpoint. If the server estimates that the time required to recover these transactions is greater than the database’s recovery interval, Adaptive Server issues a checkpoint. The modified pages are written from cache onto the database devices, and the checkpoint event is recorded in the transaction log. Then, the checkpoint task “sleeps” for another minute. To see the checkpoint task, execute sp_who. The checkpoint task is usually displayed as “CHECKPOINT SLEEP” in the “cmd” column.

Working with big tables

don't create a clustered index, because when you will need to drop and create it - ASE will lock the whole table.use range partitions to load data (starting with ver 15), and use local indexes (index on a partition) instead of global indexes.

1. Compare IN and EXISTS?In allow duplicate values and sub query have one column, exists not allow duplicates and inner query can have multi columns

2. What is sp_help and sp_helpdb?sp_help use to display the objects in a database, sp_helpdb used to display the databases in your server

3. What is @@spid, @@rowcount, @@error?returns the no of rows affected by the preceding query

4. I want to see all locks in the server?sp_lock, syslocks

5. What is Checkpoint?The point at which all data pages that have been changed are guaranteed to have been written to the database device.

6. Explain Index and Performance?Clustered index not reusing available space on preceding pages which makes pages grow longer and the search takes long time to complete. It is better to use cluster index for data value that is somewhat randomly distributed throughout the table.

In non-cluster point of view, first check the impact on the performance of OLTP environment for addition of non-clustered index. It is better to use non-

Page 37: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

cluster where low number of duplicates are present. It is good for large number of duplicate values. It is more effective if less than 10 to 20% of data is to be accessed through non clustered. Non-clustered is useful for single row lookups, joins, queries on columns that are highly selective and for queries with small range retrievals

7. Compare Delete and Truncate?DELETE TABLE is a logged operation, so the deletion of each row gets logged in the transaction log, which makes it slow. TRUNCATE TABLE also deletes all the rows in a table, but it won't log the deletion of each row, instead it logs the deallocation of the data pages of the table, which makes it faster. Of course, TRUNCATE TABLE can [*can’t] be rolled back.

8. What is the use of timestamp? timestamp is a data type that exposes automatically generated binary numbers, which are guaranteed to be unique within a database. timestamp is used typically as a mechanism for version-stamping table rows. The storage size is 8 bytes

9. What are the Inbuilt User defined datatypes available in Sybase?sysnames, timestamps

10. How can I select the first 500 rows from a table having million rows & generate a flat file? set rowcount 500

11. Write a query to find out the Nth max and min?To find Nth max: select * from employee T1 where 1 = ( select count (distinct T2.sal) from employee T2 where T2.sal >= T1.sal)

To find Nth min: select * from employee T1 where 1 = ( select count (distinct T2.sal) from employee T2 where T2.sal <= T1.sal)

12. What system procedures are used to list all the indexes?sp_indexes

13. What are the modes of transaction? And how do you change from one mode to another?unchained mode: The default mode, called unchained or Transact-SQL mode, requires explicit begin transaction statements paired with commit transaction or rollback transaction statements to complete the transaction. chained mode: The SQL standards-compatible mode, called chained mode,

Page 38: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

implicitly begins a transaction before any data retrieval or modification statement. These statements include: delete, insert, open, fetch, select, and update. You must still explicitly end the transaction with commit transaction or rollback transaction. You can set either mode using the chained option of the set command.

14. How do u run a file with lot of queries on ISQL?isql -U -P -S –i input file

15. What is the error code of Deadlock and How will you eliminate Deadlock?errorcode for dead lock is @ERROR==1205 and for stored procedure is -3.

16. How do you handle Error in Sybase?The @@ERROR Function: SQL Server sets the @@ERROR object. If the statement was successful, @@ERROR is set to 0, otherwise it is set to the designate error code. All SQL Server error codes can be found within the master.dbo.sysmessages system table. One important thing to remember is that @@ERROR is cleared each time a statement is executed. It is a good practice to store the value within a local variable.

SP_ADDMESSAGE:Use the sp_addmessage feature to add your own customized messages. The following information is given about each parameter:

RAISERROR:You can also use the RAISERROR command to create an error message. RAISERROR can send the error message information back to a client application.

17. How do you handle error in BCP?bcp [-e error file]

18. Write a query to convert the date to dd/mm/yy?select convert(char,date_exp,101)

Style Output (yy)0 mon dd yyyy hh:miAM (or PM)1 mm/dd/yy2 yy.mm.dd3 dd/mm/yy4 yy.mm.dd5 dd-mm-yy6 dd mon yy7 mon dd, yy8 hh:mm:ss9 mon dd yyyy hh:mi:ss:mmmAM (or PM)10 mm-dd-yy11 yy/mm/dd12 yymmddStyle Output (yyyy)

Page 39: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

100 mon dd yyyy hh:miAM (or PM)101 mm/dd/yyyy102 yyyy.mm.dd103 dd/mm/yyyy104 yyyy.mm.dd105 dd-mm-yyyy106 dd mon yyyy107 mon dd, yyyy108 hh:mm:ss109 mon dd yyyy hh:mi:ss:mmmAM (or PM)110 mm-dd-yyyy111 yyyy/mm/dd112 yyyymmdd

19. When a query is sent to the database and an index is not being used, what type of execution is taking place?table scan

20.What is an advantage to using a stored procedure as opposed to passing an SQL query from an applicationfaster execution, check for constraints, avoid network traffic & security(Code is not visible)

21. What are defaults? Is there a column to which a default can't be bound? When you are inserting suppose forgot to enter the value for particular column than you will provide some constant value for that .identity ,timestamps column cannot be default bound

22. Where do you think the users names and passwords will be stored in sql server?syslogins

23. What are user defined datatypes and when you should go for them? User defined datatypes let you extend the base SQL Server datatypes by providing a descriptive name, and format to the database. Take for example, in your database, there is a column called Flight_Num which appears in many tables. In all these tables it should be varchar(8). In this case you could create a user defined datatype called Flight_num_type of varchar(8) and use it across all your tables. sp_addtype Flight_num_type, “varchar(8)”, "not null"

24. Forcing an index in a query (Asked for JP Morgan)sometimes it is necessary to force the index when optimizer has incorrect statistics.-- Example: Force the idx_emp index in this queryselect t1.emp_id, t1.emp_name, t2.statusfrom employee t1 (index idx_emp),audit_flags t2

Page 40: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

where t2.emp_id = t2.emp_idgo

25. What is bit datatype and what's the information that can be stored inside a bit column? Bit datatype is used to store boolean information like 1 or 0 (true or false). Until SQL Server 6.5 bit datatype could hold either a 1 or 0 and there was no support for NULL. But from SQL Server 7.0 onwards, bit datatype can represent a third state, which is NULL.

26. What are constraints? Explain different types of constraints?Constraints enable the RDBMS to enforce the integrity of the database automatically, without needing you to create triggers, rule or defaults.

Types of constraints: NOT NULL, CHECK, UNIQUE, PRIMARY KEY, FOREIGN KEY

27. What are “GRANT” and “REVOKE’ statements?

28. What are Wildcard operators in sybase?Selects documents that contain matches to a character string containing variables. The WILDCARD operator lets you define a search string with variables, which can be used to locate related word matches in documents.

29. If I want current database name?db_name()

30. How the Trigger differ from rules and constraints?

31. If I want to check my query syntax what shall I do?set parseonly on

32. Why we should go for Deallocating the cursors?

33. How do you call the Remote stored Procedures?exec server_name.db_name.proc_name

34. What is the Drawbacks of Normalization?Although most successful databases are normalized to some degree, there is one substantial drawback of a normalized database: reduced database performance. The acceptance of reduced performance requires the knowledge that when a query or transaction request is sent to the database, there are factors involved, such as CPU usage, memory usage, and input/output (I/O). To make a long story short, a normalized database requires much more CPU, memory, and I/O to process transactions and database queries than does a denormalized database. A normalized database must locate the requested tables and then join the data from the tables to either get the requested information or to process the desired data. A more in-depth discussion concerning database performance occurs in Hour 18, "Managing Database Users."

35. What is In & OUT in stored procedure?The output keyword may be specified in the sp creation statement if the

Page 41: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

parameters can be passed both in and out of the sp, when passing out you must specify output keyword in execution statement

36. Tell me the limitation of the Trigger?Any create command, drop command, alter table, grant, revoke, select into, truncate, update statistics are not permitted in trigger

37. Can I update view?Yes, but view had created using only one table than update is possible

38. What is difference between constraints and rules?Rules deals with constant and one rule can bound one column, constraints can bound many column and it can be compare with column in another table

39. How will you bind a rule?sp_bindrule

40. explain about @@sqlstatus?It returns the status of the pervious fetch statement in a cursor

41. What are the steps involved in creating cursor?Declaring cursors, declaring variables, opening cursors, fetching rows, main loop, closing the cursor, deallocating cursors

42. What is Transaction Log?It is a database level system table called syslogs, which contains a sequential list of all modification to every objects in the database.

43. compare Rollback transaction and Rollback Trigger?

44. How will bcp out the Identity column?-E

45. How to get the last month’s last Day?select datename(dw,dateadd(dd,30-datepart(dd,getdate()),dateadd(mm,-1,getdate())))

46. What is patindex()?returns the starting postion of the specified, else 0

47. How do I get the Duplicate rows from a table?select * from table name group by column1 having count(*)>1

48. Explain Stored Procedures on performance?Query optimizer generates a query plan for a st. proc based on the parameters parsed in the first time it is executed. To make st.prc to perform well, first the quires used in the procedure are to be checked. Also since it is compiled and stored in memory, procedure may become inefficient over time. Further execution used only if it is gain otherwise again the st. proc is recompiled. For example when it was first compiled, it may be fast as the table size is

Page 42: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

small and the table scan is better choice. But when the table grows into huge, index selection may be a better option than the table scans for that query. But the procedure will still do only table scan as it is pre-compiled, so as a general rule, if the table size changes by 20%, we have to run update statistics on the table and recompile the stored procedure. For stored proc in which, the parameter has the strong influence over the result set, we have to make it with recompile option, so that it compiles every time it is accessed.

What is sp_dboption?It is used to enable or disable db option for your database

49. Explain Denormalization techniques?Denormalization is used for performance boosting. In which normalized db is denormalized for the sake of performance. According to normalization all columns has to depend on the primary key, but some times creating a manipulated field.

Redundant column may be used in a table when we always join the first table to get a column data from the second table using foreign key. Data partitioning techniques like vertically splitting the tables, horizontally splitting the tables can be used to denormalize a db. Vertical split technique is typically used to minimize the size of the primary table or to move infrequently accessed columns to a separate table. Horizontal split technique involves splitting the table at row level into two or more tables. For ex, if we can split the table into two tables, so that the first table has the recent entries and a table that has historical data, we can drastically increase the size of the new table, whose columns are going to access frequently

50. Which is better Stored procedure or Trigger?If you intend to set or modify some column values in the proc/trig, a trig is the right place to do it. A BEFORE trigger can directly modify the row buffer before it is updated/inserted. A procedure would have to issue a UPDATE, thus a performance penalty. --------------------- 2. The trigger has all the row data automatically available to it. For the procedure to have the same information, you'll either have to pass all values to the proc via parms, or the proc will need to re-read the data, a performance penalty. ----------------------------- 3. If your application issues a single update, delete, or insert that modifies multiple rows, a FOR EACH ROW trigger will be fired for each row. It could be difficult to provide the SP the information that allows it to identify the set of rows that were modified.

51. I made two tables A B id1 age id1 age 1 Asked me to find out all the id1's which are in table A and do not exist in table bselect * from A where not exists(select id1 from B)

52. Compare left and Right Outer Join?For left join all row was selected in outer table and for inner table row which are not meet the join condition are filled with NULL. In Right join inner table all row was selected and for outer table the rows which not meet the join conditins will filled with NULL

Page 43: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

53. What is Join and Its Types?Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table.

Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS.

54. what are different global variables ? @@rowcount-returns no of rows processed by preceding command. @@error- returns the error code for last or current query. @@transtate- returns the current transaction state. @@tranchained-returns the current transcation mode. @@server- returns the server name. @@version-returns the version of the sql server and OS used in a system. @@spid- current process id. @@identity-returns the lastly incremented identity value. @@nestlevel-No of level in stored procedures/trigger. @@sqlstatus-status of previous fetch statement in cursor.

55. What version of Sybase have you worked on?Adaptive Server Enterprise: 12.5.4

56. Compare Union and Union all?union remove the duplicates and go for worktable creation where as union all allow duplicates and don't go for WT

57. Compare Subquery and Correlated Subquery?Sub query : A select statement that is nested inside another select, insert, update or delete statement, or inside another subquery.

Correlated subquery: A subquery that cannot be evaluated independently, but that depends on the outer query for its results. Also called a repeating subquery, since the subquery is executed once for each row that might be selected by the outer query.

58. Compare Primary key and Unique key?Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only

59. Advantages and disadvantages of cursors?

60. Compare Select into and Insert into?select into it will create default table and copy the value to that table from other, insert into use to insert the value only in existing table

61. What is “COMPUTE BY” clause in Sybase?When ever we use compute by, we must also include order byGenerates summary values that appear as additional rows in the query results

Page 44: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

62. Compare 2 nd and 3 rd Normal form?Second normal form-nonkey fields must depend on the entire pr. Key. Third normal form---nonkey fields must not depend on other nonkey fields.

63. If I want to round the value 999.99 as 1000 ?round(999.99,0)

64. How will you create Login name and User in Sybase?sp_addlogin & sp_adduser

65. What is difference between Count() and Count(*)?count(*) will include null rows and faster

66. How do u copy the output results into a file?isql -Usa -P -S –o os_file_name

67. what is stored procedure? what is the maximum parameter can pass through it? It is a db objects that exists independly of the table ,Which contain a set of query.maximum paraperters pass through it is 255

68. what is Trigger?how many trigger can a table have and what are?It is internal part of a statement that fired when insert,update,delete operation taken place.Three trigger,Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table

69. how will you add a column and drop a column?Alter table table name [add][drop] column name

70. explain about @@sqlstatus?It returns the status of the pervious fetch statement in a cursor

71. compare WHERE clause and HAVING clause?Where clause cannot have Aggeregate function ,and it select the row before the group by function .having select the row after the group by

72. What is sp_dboption?It is used to enable or disable db option for your database

73. What is CURSOR?It is a pointer, use for row by row operation

74. Why we should go for Deallocating the cursors?When we Deallocate the cursor so that server will clear the memory space occupied by the cursor we can use that space for some other action

75. What is Clustered and Nonclustered index?When you create a clustered index on a table, all the rows in the table are stored in the order of the clustered index key. So, there can be only one clustered index per table. Non-clustered indexes have their own storage separate from the table data storage. Non-clustered indexes are stored as B-tree structures (so do clustered indexes), with the leaf level nodes having the

Page 45: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

index key and it's row locater

76. How do I get the Duplicate rows from a table?select * from table group by column having count(*)>1

77. While creating index I forgot to tell what type what will happen?nonclustered index

78. What is BCP?and Types?It is used for import and export the values from table to os file and vice-ver. Fast bcp and Slow bcp

79. How to get Yesterday’s date?dataadd(dd,-1,getdate())

80. Compare Join and SubQuery in performance?Generally Join queries consume more memory than sub query. Sub query in turn involve intermediate table creation so affects performance, because of high I/Os fetches need for processing. If the RAM memory is more, then the Joins can be used instead of sub queries. Under memory constraints we can go for sub queries.Sometimes sub queries are flattened to join to improve the performance. Also making the outer query to compare inner query with equality option can materialize the subquery.

81. What are the different types of lock in Sybase?Shared locks :SQL server applies shared lock for read operations. All the processes can read, but no process can write.Update lock :allows many processes to read, but no other process can get an excl. or update lock. This lock is applied during update or delete but changes to excl lock when modification takes place and exists till the transaction is completed.

Exclusive lock: SQL server applies exclusive lock for data modification operations. When the transaction gets exclusive lock, other transactions cannot obtain any other type of locks until the exclusive lock is released at the end of the transaction.

82. List out all System procedure in your server?select name from sysobjects where type='P'

83. How do you check whether the rollback was correct?By checking the @@trancount value before rollback statement execute

84. What is normalization?It is a process of analysing the given relation schemas based on their Functional Dependencies (FDs) and primary key to achieve the properties

Ø Minimizing redundancyØ Minimizing insertion, deletion and update anomalies.

85. What is Functional Dependency? A Functional dependency is denoted by X Y between two sets of attributes X and Y that are subsets of R specifies a constraint on the possible tuple that

Page 46: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

can form a relation state r of R. The constraint is for any two tuples t1 and t2 in r if t1[X] = t2[X] then they have t1[Y] = t2[Y]. This means the value of X component of a tuple uniquely determines the value of component Y.

86. When is a functional dependency F said to be minimal?

Ø Every dependency in F has a single attribute for its right hand side.

Ø We cannot replace any dependency X A in F with a dependency Y A where Y is a proper subset of X and still have a set of dependency that is equivalent to F.

Ø We cannot remove any dependency from F and still have set of dependency that is equivalent to F.

87. What is Multivalued dependency?

Multivalued dependency denoted by X Y specified on relation schema R, where X and Y are both subsets of R, specifies the following constraint on any relation r of R: if two tuples t1 and t2 exist in r such that t1[X] = t2[X] then t3 and t4 should also exist in r with the following properties

Ø t3[x] = t4[X] = t1[X] = t2[X]

Ø t3[Y] = t1[Y] and t4[Y] = t2[Y]

Ø t3[Z] = t2[Z] and t4[Z] = t1[Z]

where [Z = (R-(X U Y)) ]

88. What is Lossless join property?

It guarantees that the spurious tuple generation does not occur with respect to relation schemas after decomposition.

89. What is 1 NF (Normal Form)?The domain of attribute must include only atomic (simple, indivisible) values.

90. What is Fully Functional dependency?It is based on concept of full functional dependency. A functional dependency X Y is full functional dependency if removal of any attribute A from X means that the dependency does not hold any more.

91. What is 2NF?A relation schema R is in 2NF if it is in 1NF and every non-prime attribute A in R is fully functionally dependent on primary key.

92. What is 3NF?

A relation schema R is in 3NF if it is in 2NF and for every FD X A either of the following is true

Ø X is a Super-key of R.

Page 47: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Ø A is a prime attribute of R.

In other words, if every non prime attribute is non-transitively dependent on primary key.

93. What is BCNF (Boyce-Codd Normal Form)?

A relation schema R is in BCNF if it is in 3NF and satisfies an additional constraint that for every FD X A, X must be a candidate key.

94. What is 4NF?

A relation schema R is said to be in 4NF if for every Multivalued dependency X Y that holds over R, one of following is true

Ø X is subset or equal to (or) XY = R.

Ø X is a super key.

95. What is 5NF?

A Relation schema R is said to be 5NF if for every join dependency {R1, R2, ..., Rn} that holds R, one the following is true

Ø Ri = R for some i.

Ø The join dependency is implied by the set of FD, over R in which the left side is key of R.

96. What is Domain-Key Normal Form?

A relation is said to be in DKNF if all constraints and dependencies that should hold on the the constraint can be enforced by simply enforcing the domain constraint and key constraint on the relation.

97. What are partial, alternate,, artificial, compound and natural key?

Partial Key:

It is a set of attributes that can uniquely identify weak entities and that are related to same owner entity. It is sometime called as Discriminator.

Alternate Key:

All Candidate Keys excluding the Primary Key are known as Alternate Keys.

Artificial Key:

If no obvious key, either stand alone or compound is available, then the last resort is to simply create a key, by assigning a unique number to each record or occurrence. Then this is known as developing an artificial key.

Page 48: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

Compound Key:

If no single data element uniquely identifies occurrences within a construct, then combining multiple elements to create a unique identifier for the construct is known as creating a compound key.

Natural Key:

When one of the data elements stored within a construct is utilized as the primary key, then it is called the natural key.

98. What is indexing and what are the different kinds of indexing?

Indexing is a technique for determining how quickly specific data can be found.

Types:

Ø Binary search style indexing

Ø B-Tree indexing

Ø Inverted list indexing

Ø Memory resident table

Ø Table indexing

99. What is system catalog or catalog relation? How is better known as?

A RDBMS maintains a description of all the data that it contains, information about every relation and index that it contains. This information is stored in a collection of relations maintained by the system called metadata. It is also called data dictionary.

100. What is meant by query optimization?

The phase that identifies an efficient execution plan for evaluating a query that has the least estimated cost is referred to as query optimization.

101. What is join dependency and inclusion dependency?

Join Dependency:

A Join dependency is generalization of Multivalued dependency.A JD {R1, R2, ..., Rn} is said to hold over a relation R if R1, R2, R3, ..., Rn is a lossless-join decomposition of R . There is no set of sound and complete inference rules for JD.

Inclusion Dependency:

An Inclusion Dependency is a statement of the form that some columns of a relation are contained in other columns. A foreign key constraint is an

Page 49: read.pudn.comread.pudn.com/downloads152/ebook/663511/Sybase 1.doc  · Web viewSybase SQL Server 11. Introduction. Sybase SQL Server 11 - Has a Single-Process, Multi-threaded Database

example of inclusion dependency.

102. What is durability in DBMS?

Once the DBMS informs the user that a transaction has successfully completed, its effects should persist even if the system crashes before all its changes are reflected on disk. This property is called durability.

103. What do you mean by atomicity and aggregation?

Atomicity:

Either all actions are carried out or none are. Users should not have to worry about the effect of incomplete transactions. DBMS ensures this by undoing the actions of incomplete transactions.

Aggregation:

A concept which is used to model a relationship between a collection of entities and relationships. It is used when we need to express a relationship among relationships.

104. What is a Phantom Deadlock?

In distributed deadlock detection, the delay in propagating local information might cause the deadlock detection algorithms to identify deadlocks that do not really exist. Such situations are called phantom deadlocks and they lead to unnecessary aborts.

105. What is a checkpoint and When does it occur?

A Checkpoint is like a snapshot of the DBMS state. By taking checkpoints, the DBMS can reduce the amount of work to be done during restart in the event of subsequent crashes.