Ken Wong Software Development Blog

July 1, 2007

No Duplicate Keys in Foreign Key Tables

Filed under: Question And Answer, SQL — canonw @ 8:44 pm

Background

We have these tables with constrained relationship.

-- DROP TABLE FKeyTable3
-- DROP TABLE FKeyTable3
-- DROP TABLE FKeyTable1
-- DROP TABLE KeyTable
CREATE TABLE KeyTable (
  KeyColumn nvarchar(2) NOT NULL
)
ALTER TABLE KeyTable
  ADD CONSTRAINT PK_KeyTable
  PRIMARY KEY (KeyColumn)

CREATE TABLE FKeyTable1 (
  KeyColumn nvarchar(2) NOT NULL,
  ValueColumn nvarchar(13)
)
ALTER TABLE FKeyTable1
  ADD CONSTRAINT FK_FKeyTable1
  FOREIGN KEY (KeyColumn) REFERENCES KeyTable(KeyColumn)

CREATE TABLE FKeyTable2 (
  KeyColumn nvarchar(2) NOT NULL,
  ValueColumn nvarchar(13)
)
ALTER TABLE FKeyTable2
  ADD CONSTRAINT FK_FKeyTable2
  FOREIGN KEY (KeyColumn) REFERENCES KeyTable(KeyColumn)

CREATE TABLE FKeyTable3 (
  KeyColumn nvarchar(2) NOT NULL,
  ValueColumn nvarchar(13)
)
ALTER TABLE FKeyTable3
  ADD CONSTRAINT FK_FKeyTable3
  FOREIGN KEY (KeyColumn) REFERENCES KeyTable(KeyColumn)
KeyColumn
BL
BM
CA
CI
CL
CS
CV

Issue

I would like to add a requirement to these tables. The KeyColumn in KeyTable should list either zero or one time in its foreign key table. e.g. if FKeyTable1 has ‘CI’ KeyColumn value, the ‘CI’ cannot be found in table FKeyTable2 and in table FKeyTable3.

Question

Write a SQL that list all the KeyColumn value violating the one of zero time entry rule.

Answer

SELECT  COUNT(KeyColumn) as KeyCount, KeyColumn
FROM (SELECT KeyColumn FROM FKeyTable1
         UNION ALL SELECT KeyColumn FROM FKeyTable2
         UNION ALL SELECT KeyColumn FROM FKeyTable3) TestKey
GROUP BY KeyColumn
HAVING COUNT(KeyColumn) > 1

In essence, I need to create a temporary table to get KeyColumn count in all foreign key tables, using GROUP BY and HAVING to help counting and filtering.

Note

This original issue was even more complicated. I have to create a KeyColumn based on another subquery. To clarify the issue and to simplify the questioning, I rewrite this issue just to bare essential.

April 24, 2007

ASPState Installation Gotcha

Filed under: ASP.NET — canonw @ 11:08 am

My development machine is getting unstable. So I decided to reload my computer from scratch.

One of the tasks is to install ASPState to ASP.Net applications. It’s been a long time I install ASP.Net. And I’m repeating the same old mistake I made years ago.

I run this command:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727>aspnet_regsql.exe

That program guides me through a wizard. And it installs aspnetdb database on my machine.

But when I execute my ASP.net from Visual Studio, I get this error message:

Cannot open database requested in login ‘ASPState’. Login fails. Login failed for user ‘???’.

Hmm, that’s puzzling. I spent an hour finding out the error, and it turns out I need to run the command this way.

aspnet_regsql.exe -ssadd -sstype p -E

The wizard in aspnet_regsql.exe does not support for ASPState. I must add these parameters to create another database called ASPState.

Things are looking good. Well, almost. I got another error message when I run the ASP.Net application.

EXECUTE permission denied on object ‘GetMajorVersion’, database ‘ASPState’, owner ‘dbo’.

This is obvious to me. And the solution is simple. I must grant the database access to ASPState to appropriate users, and grant stored procedure in database ASPState EXEC permission to these users.

Here’s my lesson.

  • Write down note just to remind myself. I went through this problem before, and I totally forget all these details. If I spent time to write it down, I don’t have to go through the pain twice.

Here’s interesting link related to ASPState

February 9, 2007

Looking for Code Generation Tools

Filed under: Code Generation, Dot Net, Java — canonw @ 1:03 pm

I am researching ways to reduce code development time. Things have progress a lot over the years. When I first look at code generation back in 1998, there are few choices, and all of them are commercial. Thanks to open source movement. I see many varieties and options in the public domain.

I found Code Generation Network. This portal has the most comprehensive information on code generation. And I learn to use several tools from their website.
I try these tools so far. And none of them win my vote of confident.

Dot Net Based

  • MyGeneration
    I like this a lot. It has many user-contributed templates on object-relationship mapping framework. It’s very flexible, and have non ORM related sample to follow. One of my favorite is Insert SQL generation.
    Unfortunately, the program stability is questionable. I have a Core 2 Duo Thinkpad T60p, and I try to fetch insert SQL on a sizable table in my SQL Server. MyGeneration keeps popping up a message box, and I was forced to kill the process.
  • CodeSmith
    This is a popular commercial tool. Our local user group will demonstrate its usage this month. I thought about trying it out. But pass it.
  • Codus
    it has nice and clean user interface, but few templates. All template are ORM oriented.

Java Based

Frankly, I didn’t spend as much time in the Java. So far, none of the Java based tools share the same intent as the Dot Net based tools.

To be investigated

I have yet to find my ideal tool. So, I will research these tools in later day. MDA becomes an important consideration. But object modeling is not the only objectives I want to accomplish.

My Reflection

After going through my research, I realize my objective must enlarge. Initially, I tried to find an ad hoc code generation tool. But the state is not sophisticated enough.

Let’s put it another way. Why should I use code generation? And what purpose do I try to accomplish?

So far, most code generation tools focus on these area of technologies.

  1. Template engine
  2. Object relationship mapping

To make these code generation more robust. It needs improvement in these area.

  1. A mechanism of describing class definition
  2. Data oriented SQL statement manipulation
  3. Finer grain of control on object relationship mapping
  4. …More

More reflection to come…

August 5, 2006

Barrier of Entry

Filed under: Java — canonw @ 2:13 pm

I try to find a good personal blogger lately. And I learn a lesson why Java will never be as popular as other language like PHP.

Initially, I choose a Java-based bloggers. I used these bloggers in the past. Both Pebble and blojsom are good to use as personal blog server. Both are easy to setup. This time I select Roller Weblogger. The reason I choose this because I want to try out embedded database, Derby.

Among these Java-based bloggers, blojsom is the best. It’s easy to setup, and have good amount of plug-ins. Pebble is like a pet project by Simon Brown. Roller Weblogger tries to recruit more volunteers by being an Apache project.

Frankly, all of them are well polished and with care. However, none of them shares the same appeal as equivalent PHP projects.

For some reason, I tryout WordPress. It takes about 20 minutes to setup and running.

What shock me is this. Comparing the amount of time and knowledge to setup a PHP application, a Java application is more demanding. Among these application, Roller Weblogger takes the longest time to setup. The installation guide is clearly designed for hardcore Java developers and not for average Joe (the guide misses information about Hibernate configuration with Derby). Pebble and blojsom are better but they don’t provide help if you know nothing about web container like Tomcat.

My point is this.  It takes a fair amount of server-side Java knowledge to make these bloggers work. And this is not the first time I’ve experienced this. End-users are expected to know something on Java, especially Tomcat setup (knowledge on web container), JDBC connection setup (kowledge on database setup), etc. This takes time to research, and an average Joe doesn’t have patience to learn it.

On the other hand, setting up WordPress is a snap. Just follow the installation guide, and you’ll have what you want in no time. It is great if I know the required tools — Apache, MySQL and PHP. But it’s not required.  The guide will point you to the right location.
Java is designed to be specialized tools. And average Joe never needs the high end features.

August 1, 2006

Apatana — a JavaScript Editor

Filed under: Eclipse, IDE, JavaScript — canonw @ 9:36 am

Apatana is an Eclipse based open source Javascript editor.  Look like a great replacement of commercial products.

July 31, 2006

Visual Studio Alternative

Filed under: IDE, Visual Studio — canonw @ 9:59 am

#Develop is an open source IDE targeted for C#, VB.NET and Boo projects. I have a lot of respect to these developers. They put so much effort just to repeat the same work done by Microsoft.

Visual Studio Express has accomplished a lot as an free IDE.

July 25, 2006

Monitors Tools

Filed under: Administration, Java — canonw @ 12:03 pm

Just found these interesting tools for monitoring Java activity.

  1. JDbMonitor – Monitor JDBC Performance. It is very usful if a Java application works with multiple databases. All I need is just one piece of software to monitor all. (Updated: 2006-07-25)
  2. MessAdmin – Notification system and Session administration for J2EE Web Applications. A easy way to track session variables. (Updated: 2006-07-25)
  3. SuperACElet offers this product to cover many aspects of J2EE — EJB, JMX, JMS. The coverage scope is very ambitioius. (Updated: 2006-07-27)

July 20, 2006

ABBYY and professionalism

Filed under: OCR — canonw @ 5:52 pm

Their sales representative just calls me. I’m impressed by their promptness and aggressiveness.

On the other hand, its competitors response are not up-to-par. I request information from other companies via voice and email. Their sales rep have yet to call me. Frankly, it feels like they don’t care.

OCR Software

Filed under: OCR — canonw @ 3:07 am

I’m researching batch document processing, something with OCR capability to export data to database.  In my two hour search, I found some open source stuff.  But nothing of valueable.

So far, I found three candidates, and they are all Windows products.

  1. Abbyy - this seems to be the most comprehensive.  It has many products, and a version for Linux too.
  2. OmniPage
  3. FormStorm

Each package has shrink wrap version and SDK for development. Need to investigate further for difference.

March 27, 2005

Class DataSet and Class TableAdapter

Filed under: ADO.NET — canonw @ 1:40 pm

DataSet and TableAdapter really catch my eye. These classes map data relationship as an independent unit (e.g. disconnected from data source).

In a sense, it takes away work from ORM. I don’t have to create classes to map exact relationship. Sure, I also lose control over class design. The time saved is worthwhile.

Older Posts »

Blog at WordPress.com.