Derived table vs subquery

There are several ways to combine datasets:
  • Subquery: SELECT clause, correlation
  • Join (cross/left/right/inner/full): FROM clause, no correlation
  • Cross apply: FROM clause, correlation
Datasets retrieved in the FROM clause are called "derived tables".

The "cross apply" combines the best features of the subquery and join, but is performance-wise not always the best choice.

SQL Developer thick/OCI driver

The following error messages may appear when using SQL Developer to select data from a SQL Server table via a (heterogeneous or DG4ODBC) database link on an Oracle 11g DB:

Bigger type length than Maximum
OALL8 is in an inconsistent state

Solution: switch to the thick driver to work around this.

In SQL Developer: tools -> preferences -> database -> advanced -> use oci/thick driver = checked.

If this does not work or when getting the error messages:

no ocijdbc11 in java.library.path
vendor code 0

then:

- Download Oracle Instant Client 11.1.0.7.0 basic lite (easily found with Google, register with Oracle to download)

Note: version 11.2 does not work with SQL Developer!

Note: take the 64-bits version on a 64-bits OS, like Windows7 x64.

- Extract files to c:\ora\instantclient_x64_11.1.0.7.0_basiclite
- Add to environment variable PATH: c:\ora\instantclient_x64_11.1.0.7.0_basiclite
- Create a batch file called sd.bat in the rootfolder of the SQL Developer installation, with the following body:
set ORACLE_HOME="c:\ora\instantclient_x64_11.1.0.7.0_basiclite"
start sqldeveloper.exe
- Double-click sd.bat to start SQL Developer
- Add a connection with Connection type = basic

To make a connection with Connection type = TNS:

- Create folder c:\ora\instantclient_x64_11.1.0.7.0_basiclite\network\admin
- Put these files in the admin folder:

sqlnet.ora
tnsnames.ora

- In SQL Developer: tools -> preferences -> database -> advanced -> tnsnames folder: c:\ora\instantclient_x64_11.1.0.7.0_basiclite\network\admin
- Add a connection with Connection type = TNS

Note the following:
  • The solution above even (or especially) works when another Oracle client is already installed. For example, if the OracleXE DB (which, at the time of writing, is 10g) is installed locally, then the Oracle Client 10 is also installed. But this client might not work when trying to connect to a remote Oracle 11g DB
  • There is no need to set the environment variables ORACLE_HOME or TNS_ADMIN variable on the system. It does not matter what their values are
  • The order of folders in the variable PATH does not matter
  • Most likely Help -> About -> Properties -> oracle.home does not point to the correct Oracle home folder. So not to c:\ora\instantclient_x64_11.1.0.7.0_basiclite. It does not matter what the value is
  • Unfortunately, the ORACLE_HOME trick does not work with the PATH variable; it is no use adding that to the batch file
  • The error "Bigger type length than Maximum" is related to a bug in the implementation of the thin part of the JDBC driver. Not to heterogeneous services, Oracle DB, SQL Server, SQL Developer or the thick part of the JDBC driver. (Note: the same driver, e.g. ojdbc6.jar, contains the thin and the thick classes)

Oracle JDBC connections

The following JDBC drivers exist for Oracle:

  • classes111.jar: Java 1.1
  • classes12.jar: 1.2 and 1.3
  • ojdbc14.jar: 1.4 and 1.5 (no JDBC3 and 4 features)
  • ojdbc5.jar: 1.5
  • ojdbc6.jar: 1.6

Java 1.5 = Java JRE/SE 5 and Java 1.6 = Java JRE/SE 6. Additional jar files are required for some features.

All the classes to support basic functionality for the Thin and OCI drivers are inside the same jar. In short, for simplification, the same file contains both the thin and the thick (=OCI) classes.

Joe Weinstein on forums.oracle.com:
The jar is all of the thin driver and the java portion of the OCI driver. In order to
run in OCI mode, you would use this same jar, giving it the OCI URL instead of the thin
URL, and you would have to have the whole Oracle client installed on your machine,
and your OS environment set up to point to the OCI DLLs as well as the standard C
libraries. Note that the version of the OCI libraries that works with one driver jar
may not work with another driver jar, so if you go the OCI route, you may have to
install a whole new OCI client just to get a particular driver fix.
Thick URL: jdbc:oracle:oci8:@<database_name>
The database_name is the Net Service name of a mapping in tnsnames.ora.

Thin URL: jdbc:oracle:thin:@<server>:1521:<database_name>
The database_name is the database name part of the database service on the (remote) server. E.g. Windows service "OracleServiceORCL", then the database_name is "ORCL".

Oracle released different versions for each driver, eg. 11.1.0.6.0 or 11.1.0.7.0.

To get the different versions, download the Oracle Instant Client of choice from the Oracle website and extract the ojdbc.jar files to one folder. Rename them using the version. This makes testing or switching easy:

ojdbc6_11gR1_11.1.0.6.0.jar
ojdbc6_11gR1_11.1.0.7.0.jar
ojdbc6_11gR1_11.2.0.1.0.jar
etc

Databases

Row-oriented:
Oracle DB
SQL Server
SQL Server CE
MySql: InnoDB
Sybase Advantage Database Server (ADS)
MS Access

Column-oriented:
Extreme-DB
InfiniDB Enterprise Edition
Infobright
MonetDB
ParAccel Analytic Database
Sybase IQ
Sybase Adaptive Server Enterprise (RDBMS & column-oriented)
Vectorwise
Vertica

Appliance:
Aster Data (MPP, MapReduce)
Greenplum (MPP, MapReduce)
Netezza (MPP)
Teradata (MPP)

In-process:
SQLite

No login prompt for website

Suppose you want to login with Internet Explorer to an intranet website, that uses Windows Authentication as the authentication mechanism. When you do this, a prompt is displayed, that requires you to enter the account's username and password.

This prompt can be skipped:
  • Note the webserver name in the title bar of the prompt, e.g. wbsvr01.acme.lan
  • Start the Credential Manager: Control Panel -> Credential Manager
  • Add a windows credential:
    a. internet or network address: wbsvr01.acme.lan
    b. username: domain\usr
    c. password: pwd
Note: if the website is on the internet, the webserver name has to be added to the trusted sites of the intranet zone:
  • Internet Explorer -> Tools -> Internet Options -> Security -> Select "Local Intranet" -> Sites -> Advanced
  • Add this website to the zone: http://wbsvr01.acme.lan

Run SSMS using different Windows account

You can log on to a SQL Server database using SQL Server Management Studio (SSMS) with a Windows Authentication login / NT account / Windows login.

However, the Active Directory domain account to do this with cannot be entered. It is greyed out and always the account of the user that is currently logged on.

There are several ways to bypass this and make a connection:

1) Run SSMS under different privileges
  • Create a shortcut to SSMS with the following command:
    runas /netonly /user:domain\usr "g:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe"
  • Double-click the shortcut and enter password for the runas account
Note: SSMS can be located somewhere else, depending on the choice made for the location when installing or the operating system. When using Windows 7 x64, then the tool is by default located as specified above.

Note: The greyed-out login screen in SSMS still displays the logged on user, not the runas account. This might be confusing.

1a) Run SSMS under different privileges, without needing to enter a password
  • Download "runasspc" (can be easily found with a search engine)
  • Extract "runasspc.exe" from the zip file and copy to the windows folder
  • Create a shortcut to SSMS with the following command:
    runassprc "g:\Program Files (x86)\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\SqlWb.exe" /domain:domain /user:usr /password:pwd
2) Create a network connection to the remote server
  • Open a command windows (Win+R, cmd.exe)
  • Delete all mappings: net use * /delete /y
  • Create mapping to SQL Server server: net use \\servername /user:domain\usr
  • Start SQL Server Configuration Manager via Programs (or locate SQLServerManager.msc directly)
  • Open SQL Native Client Configuration
  • Enable Named Pipes protocol and move to top in the order
  • Add a new alias. Use:
    a. "Named Pipes" as network library
    b. Server name: fill in remote server name. The pipe field will display: \\[servername]\pipe\sql\query"
  • Start SSMS
    a. Server name: fill in remote server name
    b. Authentication: Windows Authentication
    c. Connection Properties -> Network Protocol, set to Named Pipes
Note: SSMS automatically uses the account name from the connection to the remote server.

3) Set credentials for a remote server using Credential Manager
  • Control Panel -> Credential Manager
  • Add the domain\account and password for the remote server
Note: use the fully qualified server name and port, e.g. remoteserver.domain.com:1433

4) Set terminal services Active Directory rights
Get the "allow logon to terminal services" right for the remote server for the domain account. Then make a terminal services connection to the server with that account and start SSMS within that session.

Links:
http://www.olegsych.com/2009/05/crossing-domain-boundaries-windows-authentication/

Change column position

In Oracle, it is not straightforward to change the position of a column. Let alone one that contains data.

Without resolving to manually changing metadata tables, but using SQL, the following trick can be applied:

- Create a new table called "newtable" with the desired column order, based on "oldcolumn"

Note: generate DDL script for "oldtable" and only change the position of the columns. Do not apply constraints, defaults, primary keys, foreign keys, etc. They are named and the names cannot exists twice in a schema

- Insert all data from "oldtable" into "newtable" using a DML INSERT-statement
- Drop "oldtable"
- Rename "newtable" as "oldtable"
- Apply constraints, defaults, etc to "oldtable"

Note: if columns are deleted or newly added to "newtable", then this is not straightforward and each constraint, default, etc should be inspected before applying

Or you can use the "dbms_redefinition" package (bhaski on www.geekinterview.com):

But you should have these previleges -->
# execute access on the dbms_redefinition package
# create any table # alter any table
# drop any table
# lock any table
# select any table

create table tab1(col1 char,col3 number);

alter table tab1 add(col2 char);

create table tab2 as select col1,col2,col3 from tab1; exec

dbms_redefinition.can_redef_table('schema_name','tab1');

begin
dbms_redefinition.start_redef_table('schema_name','tab1','tab2','col1,col2,col3');
dbms_redefinition.finish_redef_table( 'schema_name', 'tab1', 'tab2' );
end;

select * from tab1;
col1 col2 col3

Change column data type

In Oracle, it is not straightforward to change the data type of a column that contains data. Without resolving to manually changing metadata tables, but using SQL, the following trick can be applied:

- Add a new column to the table called "new column" with the new data type
- Copy the values of "old column" to "new column" using a DML UPDATE statement
- Remove the "old column" from the table using a DDL ALTER statement

Most recognizable samples

Breakbeats
Al Green - I’m Glad You’re Mine (0:00)
[Eric B. & Rakim - Mahogany, The Notorious B.I.G. - I Got a Story to Tell, RZA - My Lovin' Is Digi]

Bad Bascomb – Black Grass (1:41)
[Doug E. Fresh - Everybody Got 2 Get Some, Monie Love - Monie In The Middle]

Billy Squier – Big Beat (0:00)
[Ice Cube – Jackin’ For Beats, Big Daddy Kane – Get Down, A Tribe Called Quest – We Can Get Down, EPMD – Get Wit This]

Booker T. & The M.G.'s - Born Under A Bad Sign
Booker T. Jones & William Bell - Born Under A Bad Sign

Bobby Byrd - Hot Pants (0:00)
[Sir Mix-a-Lot - One Time's Got No Case, Das EFX - Straight Out the Sewer, Ini Kamoze - Here Comes the Hotstepper, Run-DMC - What's It All About]

Chuck Brown and The Soul Searchers - Ashley’s Roach Clip (3:31)
[Eric B. & Rakim – Paid In Full, PM Dawn – Set Adrift On Memory Bliss, Nice & Smooth - Down the Line]

Dexter Wansel – Theme From The Planets (0:00)
[Eric B. & Rakim - I Ain't No Joke, Last Night by Kid 'N Play, Gutfest '89 by Digital Underground]

Donny Hathaway – Magnificent Sanctuary Band (0:00)
[Beastie Boys - Johnny Ryall]

Edwin Birdsong - Rapper Dapper Snapper (0:00)
[De La Soul - Me Myself and I, Gang Starr - Skills]

Fred Wesley and The J.B.'s - More Peas (7:22)
[Organized Konfusion - Fudge Pudge, Cypress Hill - The Phunky Feel One]

Funkadelic – Good Old Music (0:00)
[Young Black Male by 2Pac]

Kool & The Gang – Jungle Jazz (0:13)

Kool & The Gang - N.T.
[Jump by Kris Kross, Fakin' the Funk by Main Source]

Funk Inc. – Kool Is Back (1:48)
[Jeru the Damaja - Come Clean]

Isley Brothers – Footsteps In The Dark

James Brown - Blues and Pants (0:00)
[Ice-T - New Jack Hustler, Cypress Hill - The Phunky Feel One]

James Brown - Funky Drummer (5:35)

James Brown – Funky President (0:00)
Eric B. Is President by Eric B. & Rakim, Hey Ladies by Beastie Boys]

Jeff Beck – Come Dancing (0:00)
[Chino XL - It's All Bad, Gang Starr - New York Strait Talk]

Jimi Hendrix - Flashing (0:00)

Joe Tex – Papa Was Too (0:00)
[EPMD - Headbanger, Wu-Tang Clan - Wu-Tang Clan Ain't Nuthing Ta F*** Wit]

Lafayette Afro Rock Band - Hihache (0:00)

Led Zeppelin - When The Levee Breaks (0:00)
[Ice-T - Midnight, Eminem - Kim]

Little Feat - Fool Yourself (0:00)
[A Tribe Called Quest - Bonita Applebum, Organized Konfusion - Soundman]

Little Richard – The Rill Thing (0:00)

Lowell Fulsom - Tramp (0:00)
[House Of Pain - Jump Around, Cypress Hill - How I Could Just Kill A Man]

Lyn Collins - Think (About It) (1:23)

Manzel - Midnight Theme (0:00)
[Wreckx-N-Effect - Rump Shaker, Cypress Hill - How I Could Just Kill A Man]

Mountain - Long Red (Live) (0:00)

Melvin Bliss - Synthetic Substitution (0:00)
[Naughty By Nature - OPP, Ice-T - Original Gangster, The Pharcyde - Ya Mama]

Orange Crush – Action (0:25)
[Do You Want It... Do You Like It... by Puff Daddy, We Major by Kanye West]

Power of Zeus – Sorcerer of Isis (0:10)
[The Wiseguys - Face the Flames, Eminem - Amityville]

Q65 – Get Out Of My Life Woman
[Jeru the Damaja - Revenge of the Prophet (Part 5), The Wiseguys - We Be the Crew]

Roy Ayers – The Boogie Back (0:00)
[N.W.A - F*** Tha Police]

Silhouettes – Fonky First (0:00)

Skull Snaps – It’s A New Day (0:00)
[The Pharcyde - Passin' Me By, Digable Planets - For Corners]

Sly & the Family Stone - Sing a Simple Song (2:12)

The Headhunters – God Make Me Funky (0:09)
[Eric B. & Rakim - Beats for the Listeners]

The Honey Drippers - Impeach the President (0:00)

The Incredible Bongo Band - Apache (0:00)
[Sugarhill Gang - Apache, LL Cool J - You Can't Dance, Grandmaster Flash - Freelance]

The Meters – Here Comes The Meterman
[Big Daddy Kane - Long Live The Kane, Run-DMC - How'd Ya Do It Dee?]

The Meters - Look-Ka Py Py (0:50)
[Cypress Hill - The Phunky Feel One]

The Winstons - Amen, Brother (1:27)
[N.W.A. - Straight Out Of Compton, 3rd Bass - Portrait Of The Artist As A Hood, Brand Nubian - The Gods, Snow - Informer, Eric B. & Rakim - Casualties of War]

Young Holt Unlimited – Wah Wah Man

Zapp and Roger – More Bounce To The Ounce (0:00)
[EPMD – You Gots To Chill, Ice Cube – Jackin’ For Beats, Public Enemy – One Million Bottlebags, Ice Cube – Look Who’s Burnin’]

The Turtles – I’m Chief Kamanawanalea (0:00)
[Serious by Steady B feat. KRS-One, Say No Go by De La Soul, AmeriKKKa's Most Wanted by Ice Cube]

Grady Tate - Be Black Baby (0:00)
[Set It Off by Big Daddy Kane, Fakin' the Funk by Main Source, Stand Together by Beastie Boys]

Sounds
The J.B.'s - The Grunt (0:00)
Herbie Hancock - Wiggle Waggle (0:00)

Identifying keys

A key that is made up of one field is a simple key. All other keys are combined keys. There are two types of combined keys, compound and composite:

Simple key: key that consists of one field

Combined key: key that consists of more than one field

Compound key: combined key in which each field is itself a simple key (of another entity)

Composite key: combined key, that is not a compound key

A new term I have made up myself:

Concatenated key: a simple key, that is the concatenation of the fields in a combined key