The company organized the SQL proficiency test, read the teaching materials, and wrote a summary to share with you.
Our company uses Sybase ase12.5, so some of the following features are for Sybase ASE.
I. SQL bisic
1: Structured quary language features:
A: Standardization
B: non-procedural
C: optimizable
D: set-oriented
2: Data Type in ase
A: numberic
B: character
C: date/time
D: lobs
3: Convert (varchar, textcolumn). If varchar (n) N is not specified, the default value is 30.
4: Roles of where in SQL
A: filter data.
B: table join (before sql92)
C: select an index.
5: differences between Whare and having
The where statement inserts filtered data into the work table.
The having statement filters data from the work table to obtain the final result.
6: execution sequence of a SELECT statement
A: From clause
B: Where clause
C: group by clause
D: Select clause
E: Having clause
F: Order by clause
7: Union vs Union all
A: Union sorts two result sets and removes duplicate elements (efficiency is poor, so it is easy not to use)
B: Union all only sets two results without sorting or removing duplicate elements (high efficiency)
2. Index and query parameters
1: There are three access data methods in ase
A: clustered Index
B: nonclustered Index
C: Table Scan
2: covered Query
A covered query only obtains data from the index and does not need to scan the database table. This is the fastest data query method.
Limit 1: only valid in selece
Limit 2: all referenced columns must be in the same nonclustered index.
3: Functional Index
It is supported only after ase15.0. That is to say, in versions earlier than ase15.0, the following statements may not use indexes.
SQL code
- Select column1
- From Table1
- Where upper (column2) = 'ivanl'
4: How to view the execution plan SQL code
- Set showplan on
- Go
- Your SQL
- Go
- Set showplan off
- Go
5. View iosql code
- Set statistics Io on
- Set statistics time on
- Go
- You SQL
- Go
- Set statistics Io off
- Set statistics time off
- Go
6: advice on Using Index
A: Use the fields frequently used in the where statement for index.
B: Make the index contain fewer fields, the better.
C: Drop useless Indexes
Iii. Table join
1: What is table join?
A table connection queries data from multiple tables or retrieves data from a table multiple times.
(A join is a transanct-SQL operation than access rows from multi-tables or from a single talbe multi-times)
2: table join category
A: inner join
B: Outer Join
C: cross join (full join)
3: Full join is not supported in ase, but full joinsql code can be simulated through Union.
- Select t1.colu1, t2.column2
- From T1, T2
- Where t1.id * = t2.id
- Union
- Select t1.colu1, t2.column2
- From T1, T2
- Where t1.id = * t2.id
(Not recommended, inefficient)
4: ASE supports up to 50 tables for table join. The ASE query optimizer does not do well. Sybase recommends that you join no more than 4 tables (-_-~!)
5. There are three ways to connect tables in the database.
A: nested loop join
B: Merge join
C: Hash join
(You can use show plan to check which join statement is used in the database to implement the join statement)
6. Suggestions for table connection:
A: The join method is used in showplan.
B: add an index to the join column.
C: Multi-table join is divided into several small tables.
D: avoid generating the flute product.
4. Use Case statements
1: two forms of SQL code for case statements
- A:
- Case
- When search_condition then expression
- [When search_condition then expression]
- [Else exproestion]
- End
- B:
- Case expression
- When expression then expression
- [When exproession then expression]
- [Else expression]
- End
2: Use Case
A: Decoding columnsql code
- Select cust_id, cust_name
- Case cust_type
- When 'r 'then' relation'
- When 'I 'then' International'
- When's 'then' small'
- Else 'other'
- End as customer_type
B: conditionally displaying columns or valuessql code
- Select title_id, total_sales,
- Case
- When total_sales> 5000 then 'higint'
- When total_sales <100 then 'low'
- Else''
- End as 'column'
C: horizontal frequency table and summary calculationsql code
- Select sum (Case type when 'adv 'Then 1 else 0 end) as adv
- , Sum (Case type when 'cues' then 1 else 0 end) as cuz
- From customer
D: updating on variable conditionssql code
- Update customer
- Set cust_charge = cust_charte + case cust_type
- When 'd 'then 1
- When 'C' then 2
- When 'E' then 3
- Else 0
- End
- [/Code]
- E: Rules and check Constraints
- [Code]
- Create Table cust_order_info
- (
- Order_num int,
- Order_taker int,
- Order_date char (7) Default
- Case
- When datepart (DW, getdate () between 2 and 6 then 'weekday'
- Else 'weekend'
- End
- )
V. Transactions and locks
1: There are two transaction modes in ase
A: chained Mode
B: unchained mode (Sybase default)
Unchained mode indicates the start of a transaction, and chained implicitly starts a transaction.
Unchained mode uses 'commint Tran ', 'rollback Tran'
Chained mode uses 'commint work' and 'rollback work'
Unchained mode supports nested transactions, but chained mode does not.
2: locking Schema
A: All pages table, will lock data and index as they are accessed (you can have clustered index)
B: A datapages table will lock datpages as they are accessed, index will not be locked (no clustered index)
C: A datarow table will lock datpages as they are accessed, index will not be locked (no clustered index)
3: locking type
The three most important lock types in ASE are
A: Shared locks (select, fetch)
B: Update locks (fetch, update, delete)
C: exclusive locks (insert, update, delete)
4: isolation level
There are four isolation levels in ase
A: isolation level 0 (read uncommited), allowing bloat read
B: isolation level 1 (read comminted) (ASE default), cannot be expanded
C: isolation level 2 (Repeatable read), Repeatable read
D: isolation level 3 (serializable). The Phantom is not allowed to read SQL code.
- SET transaction isolation level {0 | 1 | 2 | 3}
- Or
- Select...
- At isolation {0 | 1 | 2 | 3}
5: How to Write efficient transaction
For OLTP transaction
A: Make transaction as short as possible
B: use index to randomly access data
C: Use transaction only when necessary
D: select the appropriate lock type and isolation level.
E: Use optimistic locks
Vi. Data Processing
1: divided by 0
Use coalesce () and nullif ()
First, use nullif () to convert 0 to null, and use coalesce () to process the null SQL code.
- Select coalesce (total_sales/nullif (sales, 0), 0)
- -- Coalesce (ex1, ex2, ex3. ..) returns the first non-null expression.
-- Nullif (expre, value) If expre = value, null is returned.
2: Find the duplicate data SQL code
- Select Type, count (*)
- From table
- Where ..
- Group by type
- Having count (*)> 1
3: Find the SQL code that contains the most repeated data
- Select Type, count (*)
- From table
- Where ..
- Group by type
- Having count (*) = max (count (*))
4: data accumulation Java code
- Select t1.title _ id, t1.advice, sum (t2.advice) as cumulative_total
- From Title T1, title T2
- Where t1.title _ id> = t2.title _ id
- Group by t1.title _ id, t1.advice
5: Ranking datasql code
- Select Rank = identity (10), title_id, total_sales
- Into # top from titles
- Where ..
- Order by total_sales DESC
- Go
- Select * from # top
- Go
- Drop table # top
- Go
6: conver between Julian Date and Gregorian datesql code
- Select datepart (YY, @ date) * 1000 + datepart (dy, @ date) as julina_date
- Select dateadd (DD, juliandate % 1000, '2014/1/'+ convert (char (4), juliandate/12/31-1) as gregorian_date
7: calculate the number of days of SQL code this month
- Datepart (DD,
- Dateadd (DD,-1 -- last day of this month
- Datead (mm, 1 -- add a month
- Dateadd (DD --
- ,
- 1-datepart (DD, getdate () -- 1-today
- Getdate () -- get today
8: whether the SQL code is a leap year
- Select datepart (dy, '2014/1/'| convert (char (4), datepart (YY, getdate ())))
- -- = 61 is a leap year
- -- = 60 is not a leap year