[Solved] SQL to make entry required

Creating tables and queries
Post Reply
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

[Solved] SQL to make entry required

Post by Nocton »

Is there an SQL command I can use to make 'Entry Required' for a field?
I have a split database so cannot make the change via the table design.
Last edited by Nocton on Sun Sep 25, 2016 3:50 pm, edited 1 time in total.
OpenOffice 4.1.12 on Windows 10
FJCC
Moderator
Posts: 9278
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: SQL to make entry required

Post by FJCC »

A command of the form

Code: Select all

ALTER TABLE <tablename> ALTER COLUMN <columnname> SET [NOT] NULL
entered through the menu Tools -> SQL should work. The above example is from the documentation for the HSQLDB 1.8 database, which is what ships with OpenOffice. The square brackets around the NOT just mean it can be excluded if you want to set the column to allow NULL. In your command, you would include the NOT with no brackets.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Nocton
Volunteer
Posts: 533
Joined: Fri Nov 05, 2010 10:27 am
Location: UK

Re: SQL to make entry required

Post by Nocton »

Many thanks, FJCC.
OpenOffice 4.1.12 on Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: SQL to make entry required

Post by Villeroy »

Alter the whole column definition:
ALTER TABLE "Table Name" ALTER COLUMN "Column Name" INTEGER DEFAULT 0 NOT NULL; [fails if existing data are incompatible with integer type]

Alter only the nullability:
ALTER TABLE "Table Name" ALTER COLUMN "Column Name" SET NOT NULL; [fails if existing data contain Null values]

Remove default value:
ALTER TABLE "Table Name" ALTER COLUMN "Column Name" SET DEFAULT NULL;

Some other aspects:
ALTER TABLE "Table Name" ALTER COLUMN "Column Name" SET DEFAULT 43;
ALTER TABLE "Table Name" ALTER COLUMN "Column Name" SET PRIMARY KEY; [fails if existing data are not unique]
ALTER TABLE "Table Name" ALTER COLUMN "Column Name" SET GENERATED BY DEFAULT AS IDENTITY; [auto PK for int and bigint types]
ALTER TABLE "Table Name" ADD FOREIGN KEY ("Column1", "Column2") REFERENCES "other table" ("ColumnA", "ColumnB"); [relation between one, two or more columns in (braces).fails if types don't match, if there are data that do not exist in the referened table]
ALTER TABLE "Table Name" ADD CONSTRAINT "D1geqD2" CHECK "Date1">="Date2"; [not possible in the GUI anyway]
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply