Web Hosting Talk







View Full Version : check unique key of a composite primary key?


Yred71
07-09-2005, 12:25 PM
Hi everyone,

How can I check unique key of a composite primary key? A user should not be allowed to enter book information if a book_title + Release_year + Edition_no, already available.

Is there a way to do this? am using DreamweaverMX (ASP VBScript)

thankx in advance

Yred71

error404
07-09-2005, 07:26 PM
I'm not sure what you're asking. If you have a primary key that consists of those three fields, the database will force that the combination of all three is unique in each row. If you want to make a single one unique, say force book_title to be unique, you can add another constraint on that field alone, or a subset of the PK as well.

If you want to know before trying to insert the record, just do a select and check if it exists yet or not.

Yred71
07-10-2005, 06:22 AM
Yes the Primary key is a composite (3 fields, title, year_id, edition_no). I want to know prior saving if it a duplicate record or not!.

I played a bit with the check username unique server beheviour in DreamweaverMX and got stuck with this error message. Anyone can help please?

Error message: Microsoft VBScript compilation (0x800A0401) Expected end of statement.

How does it work for those quotes. And what if I want to add three fields to be checked?

The Error message:

Microsoft VBScript compilation (0x800A0401)
Expected end of statement
/muscatbooks/test.asp, line 49, column 95
MM_dupKeySQL="SELECT Title, year_id FROM tblBooks WHERE Title='" & MM_dupKeyUsernameValue & "" "And Year_id='" & MM_dupKeyUsernameValue & "'"

The code is:

<%
' *** Redirect if username exists
MM_flag="MM_insert"
If (CStr(Request(MM_flag)) <> "") Then
MM_dupKeyRedirect="./Thankyou.asp"
MM_rsKeyConnection=MM_mctconn_STRING
MM_dupKeyUsernameValue = CStr(Request.Form("title"))
MM_dupKeyUsernameValue1 = CStr(Request.Form("year"))
'MM_dupKeyUsernameValue2 = CStr(Request.Form("edition"))

MM_dupKeySQL="SELECT Title, year_id FROM tblBooks WHERE Title='" & MM_dupKeyUsernameValue & "" "And Year_id='" & MM_dupKeyUsernameValue & "'"
MM_adodbRecordset="ADODB.Recordset"
set MM_rsKey=Server.CreateObject(MM_adodbRecordset)
MM_rsKey.ActiveConnection=MM_rsKeyConnection
MM_rsKey.Source=MM_dupKeySQL
MM_rsKey.CursorType=0
MM_rsKey.CursorLocation=2
MM_rsKey.LockType=3
MM_rsKey.Open
If Not MM_rsKey.EOF Or Not MM_rsKey.BOF Then
' the username was found - can not add the requested username
MM_qsChar = "?"
If (InStr(1,MM_dupKeyRedirect,"?") >= 1) Then MM_qsChar = "&"
MM_dupKeyRedirect = MM_dupKeyRedirect & MM_qsChar & "requsername=" & MM_dupKeyUsernameValue
Response.Redirect(MM_dupKeyRedirect)
End If
MM_rsKey.Close
End If
%>



Thanks

Yred71

keithslater
07-10-2005, 09:48 PM
MM_dupKeySQL="SELECT Title, year_id FROM tblBooks WHERE Title='" & MM_dupKeyUsernameValue & "' And Year_id='" & MM_dupKeyUsernameValue & "'"