Ask Your Question
1

How do I create a field in a table with that must be Unique?

asked 2012-07-25 15:16:18 +0200

craig gravatar image craig
11 2

updated 2012-08-12 13:35:44 +0200

tohuwawohu gravatar image tohuwawohu flag of Germany
1812 13 40

Hi, In BASE. I have created a table for my database for customers which works fine with all the fields etc and a Primary Key that auto increments for CustomerID which must be unique. How can I create another field that must be unique that will bring an error if someone tries to imput a repeated data entry.

For instance I am trying to create a field that displays the first four letters of the surname plus a number i.e. 01 for the first person with this surname. Then If there is another surname its should be 02...and so on. If someone tries putting in two of the same it should bring up an error. Can this be done? The field will presumably be a VARCHAR as I want it to be letters and numbers?

I can't figure it out, please help...

delete close flag offensive retag edit

2 Answers

Sort by » oldest newest most voted
1

answered 2012-07-25 16:39:19 +0200

w_whalley gravatar image w_whalley flag of United States
1581 10 24

You want to give your column a unique constraint. Here is a link to the general SQL method.

To apply this to the default embedded LibreOffice database, use these specific steps.

For example if your column is called MYCODE, then in the SQL tool (from the main menu choose Tools/SQL, and execute this command 'ALTER TABLE MYTABLE ADD CONSTRAINT MYCONSTRAINT UNIQUE ("MYCODE");

link delete flag offensive edit
0

answered 2012-07-25 16:20:37 +0200

tohuwawohu gravatar image tohuwawohu flag of Germany
1812 13 40

There's no GUI to do this; instead, use Menu "Tools" -> "SQL..." with the following SQL command:

ALTER TABLE "Customers" ADD UNIQUE ("CustomerID");

or

ALTER TABLE "Customers" ADD CONSTRAINT customer_id_unique UNIQUE ("CustomerID");

You can find more info in the HSQLDB User Guide on SQL Syntax (ALTER TABLE).

link delete flag offensive edit

Login/Signup to Answer

Donate

LibreOffice is made available by volunteers around the globe, backed by a charitable Foundation. Please support our efforts: Your donation helps us to deliver a better product!

Question tools

Follow

subscribe to rss feed

Stats

Asked: 2012-07-25 15:16:18 +0200

Seen: 258 times

Last updated: Jul 25 '12