Tuesday, March 27, 2012

Indexes and Constraints in SYBASE T-SQL

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 toal index width <= 255.

Creating indexes: 
create unique clustered index name_index  on authors (au_lname, au_fname) 
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)
 

 

SELECT Clause in SYBASE TSQL

SELECT Clause :-

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"

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 
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"

agregates 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

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 

Tuesday, March 13, 2012

UNIX interview Questions

1.In shell scripting How to indentify that the previous command was run successfully?

2.How will you write a shell script to connect to SQL database?

3.What does UID and GID signify?

4.What are the different security mechanisms available in UNIX?

5.What are the different types of shells available in UNIX?

6.how many users have logged in and logged out in last five or 10 minutes

7.How do you search the string for vowel's occurrence and number of occurrences of each vowel

8.What is make used for? How is it different from a shell script?

9.How to compare floating point number in shell scripting ?

10.How to delete a word from a file using shell scripting???

11.How to extract the second row of a text-file?

12.How to compare two floating point numbers ?

13.How to compress files by using shell scripting

14.What are the steps to take files from unix server to windows?

15.How to find see the file which is created today,s date with time after 10 a.m to 5 p.m?

16.What is the basic difference u find between a shell script and perl.I mean the advantages of one over

17.What is use of "cut" command ?Give some examples. Can we use "awk" or "sed"

18.How Connect to a Database in Shell Programming?Please tell me Step by Step?

19.What is this line in the shell script do #!/bin/ksh?

20.Write a shell script to identify the given string is palindrome or not?

21.What is the difference between writing code in shell and editor?

22.What is INODE?

23.What is the difference between a 'thread' and a 'process'?

24.What does $# stand for?

25.What is $*?

26.If you have a string "one two three", Which shell command would you use to extract the strings?

27.What is the difference between a shell variable that is exported and the one that is not exported?

28.How will you list only the empty lines in a file (using grep)?

29.How do you schedule a command to run at 4:00 every morning?

30.How do u open a read only file in Unix?

31.What are the different kinds of loops available in shell script?