473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Advanced Constraint

Hi! ALL

I'm an novice programmer. Been trying to set up a database for an accounting program. What I want to do is have one table that contains the department number and name and another table that contains the account number and name. However I want the last 5 digits of the account number to refer to the department number that would be in the department table. I also want it to give me an error message if the last 5 digits do not match with a department number from the depatment table.

Thanks
Kooter12000
Feb 1 '08 #1
8 3615
rski
700 Expert 512MB
Hi! ALL

I'm an novice programmer. Been trying to set up a database for an accounting program. What I want to do is have one table that contains the department number and name and another table that contains the account number and name. However I want the last 5 digits of the account number to refer to the department number that would be in the department table. I also want it to give me an error message if the last 5 digits do not match with a department number from the depatment table.

Thanks
Kooter12000
in think you will need a trigger. give more details (table and column names etc.) and i willl try to help you
Feb 1 '08 #2
Thanks for the quick reply

What I've done thus far

CREATE TABLE acct_type(
acct_type char(1) check(acct_type IN ('b','i','r') primary key not null);

*b represents balance sheet accounts, i represents income statement accounts and r represents retained account*

CREATE TABLE acct(
acct_num char(4) not null primary key,
acct_name text not null,
acct_type char(1) references acct_type);

*acct table for balance sheet accounts*

CREATE TABLE dept(
dept_num char(5) not null primary key,
dept_name text not null);

For income statement accounts trying to do this

CREATE TABLE accdept(
acct_dept_num char(9) *this where I'm stuck, I want the last 5 characters to refer to the dept_num of the dept table*
acct_dept_name text not null,
acct_type char(1) references acct_type

*accdept table to be used for income statement accounts*

hope this info is helpful

Kooter12000
Feb 1 '08 #3
rski
700 Expert 512MB
Thanks for the quick reply

What I've done thus far

CREATE TABLE acct_type(
acct_type char(1) check(acct_type IN ('b','i','r') primary key not null);

*b represents balance sheet accounts, i represents income statement accounts and r represents retained account*

CREATE TABLE acct(
acct_num char(4) not null primary key,
acct_name text not null,
acct_type char(1) references acct_type);

*acct table for balance sheet accounts*

CREATE TABLE dept(
dept_num char(5) not null primary key,
dept_name text not null);

For income statement accounts trying to do this

CREATE TABLE accdept(
acct_dept_num char(9) *this where I'm stuck, I want the last 5 characters to refer to the dept_num of the dept table*
acct_dept_name text not null,
acct_type char(1) references acct_type

*accdept table to be used for income statement accounts*

hope this info is helpful

Kooter12000
try this
1. first create a function

create or replace function f() returns trigger as $$
declare
r record;
begin
select * into r from dept where dept_num =substring(NEW.i from 5 for 5);
if not found then
raise info 'constraint error';
return NULL;
else
raise info 'constraint fulfill';
return NEW;
end if;
end;
$$ language plpgsql

next create a trigger

create trigger t_acdept before insert on accdept for each row execute procedure f();

it's late but i think it should work.
let me know if it is helpfull.
Feb 1 '08 #4
hi!
When I try to enter values in acctdept get the following message:

ERROR: record "new" has no field "i"
CONTEXT: PL/pgSQL function "f" line 4 at sql statement

Kooter12000
Feb 1 '08 #5
rski
700 Expert 512MB
hi!
When I try to enter values in acctdept get the following message:

ERROR: record "new" has no field "i"
CONTEXT: PL/pgSQL function "f" line 4 at sql statement

Kooter12000
oh sorry instead of NEW.i you should write NEW.acct_dept_num, it's my mistake
Feb 2 '08 #6
Thanks

Working perfectly I would like to understand what I did specially:

=substring(new.acct_dept_num from 5 for 5)

Thanks again

Kooter12000
Feb 2 '08 #7
rski
700 Expert 512MB
Thanks

Working perfectly I would like to understand what I did specially:

=substring(new.acct_dept_num from 5 for 5)

Thanks again

Kooter12000
i'm not good at english so it would be better you read the postgres string functions manual here
see you
Feb 2 '08 #8
i'm not good at english so it would be better you read the postgres string functions manual here
see you
Thanks rski

The link you provided was very helpful. I understand it now.

Kooter12000
Feb 6 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

26
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.)...
4
by: wireless | last post by:
I've written code that dynamically builds an sql query based on various constraint possibilities. The problem is the code would have been very complex had I not come up with a dummy constraint...
2
by: adammitchell | last post by:
How can you indicate that a FOREIGN KEY constraint references two columns in two different tables? "SQL Server Books Online" show an example of how to reference two columns in the SAME table:...
3
by: ferg | last post by:
I have a Customer table. The table has two different CHECK constraints. Then there is the Customer details dialog, which provides the user with an UI for changing users. I have some UPDATE sql,...
3
by: Jeff Kish | last post by:
Hi. I'm getting errors like this when I try to run an upgrade script I'm trying to write/test: altering labels to length 60 Server: Msg 5074, Level 16, State 4, Line 5 The object...
10
by: Laurence | last post by:
Hi there, How to differentiate between unique constraint and unique index? These are very similar but I cannot differentiate them? Could someone give me a hand? Thanks in advance
4
by: antpal | last post by:
I am not sure exactly what i am doing run but when I run this code in sql plus i get constraint error on most of my tables except author and books table. Please help I am new to Programming in...
3
by: pr | last post by:
Is it possible to use an XML Schema to ensure that the data in one attribute is less than or equal to the value in another? Is it possible, through the use of the <xsd:sequenceor similar, to...
15
by: Frank Swarbrick | last post by:
I have the following three tables DROP TABLE CALLTRAK.SERVICE_CODES @ CREATE TABLE CALLTRAK.SERVICE_CODES ( CODE CHAR(1) NOT NULL , CONSTRAINT SERVICE_CODES_PK PRIMARY KEY (CODE) ,...
2
by: rorajoey | last post by:
Violation of UNIQUE KEY constraint 'IX_surveyQuestions'. Cannot insert duplicate key in object 'dbo.surveyQuestions'. This might seem like a simple matter of trying to insert a row with ID=20 when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.