When exporting a SQL script, how do I conditionally quote object names that begin with a digit? - powerdesigner

Using PowerDesigner 16.5, I am trying to export a SQL script from a model that I have reversed from an existing Oracle schema. Some object names in this schema begin with a digit. When the script is exported, these object names are emitted verbatim (which is normally what you want) but the leading digit in the object name causes a SQL statement to be parsed incorrectly.
For example,
create table MY_SCHEMA.FOO_1234_ACTION
(
MY_ID NUMBER not null,
MY_COLUMN VARCHAR2(32)
)
/
alter table MY_SCHEMA.FOO_1234_ACTION
add constraint 1234_ACTION_PK primary key (MY_ID)
/
In the example above, the constraint name begins with a digit causing this statement to be mis-parsed resulting in ORA-0902 invalid datatype. If I manually edit the script to quote the constraint name, the statement is correctly parsed and the table is altered, adding the primary key:
alter table MY_SCHEMA.FOO_1234_ACTION
add constraint "1234_ACTION_PK" primary key (MY_ID)
/
I understand that maybe the name could simply be changed but I'm trying to establish a baseline from the schema we have in order to iterate it to what it needs to be.
I have looked in both the options for the Oracle database definition as well as the options on the model itself that might influence script output (such as naming conventions). Is there any way to cause PowerDesigner to, in the case of object names with leading digits, emit the name with quotes but leaving all other names alone?

Related

What does the FOR keyword do in this SQL Server ALTER TABLE clause?

I'm not sure what FOR is doing in this SQL Server snippet.
ALTER TABLE [dbo].[TableName]
ADD DEFAULT (NEXT VALUE FOR [dbo].[TableName_Seq]) FOR [TableName_Key]
When I try to google for FOR clause the only meaningful result I got was this, but I don't think it's relevant
FOR is a keyword of the NEXT VALUE FOR function. It is used to return the next value from the specified sequence object.
FOR is also a keyword of the DEFAULT constraint clause, specifying the target column name of the constraint.
In the context of the DDL in your question, the function result is the default value automatically assign the value when a value is not explicitly specified in INSERT statements.
BTW, it's a best practice to name constraints rather than relying on auto-generated names. This makes subsequent schema modifications easier.
FOR by itself has no specific meaning, it depends on context.
In this case, there are 2 usages of FOR:
NEXT VALUE FOR function, which is used to generate the next value from a sequence.
ALTER TABLE [table_name] ADD [table_constraint], where table_constraint is of the form DEFAULT constant_expression FOR column. So it's adding a default constraint for the TableName_Key column, and the value for that default constraint is coming from the TableName_Seq sequence.

Access to SQL link table showing Allow Zero Length as 'Yes'

I have made multiple attempts at creating a SQL Server table to link to an Access front-end with 'disallow zero length value' constraints on the table but when I link the table up to my front-end the design of the table shows Allow Zero Length = Yes
Now I have tried various methods of trying to change this to No (I need it to be No for a migration project I am working on). I am not sure what needs to be done on the SQL Server to ensure that upon linking this table to my access front-end, this constraint is a No.
This is the Create script for my table:
Create Table Riku(
ID int NOT NULL PRIMARY KEY,
testtext varchar(255),
CONSTRAINT DissalowNulls
CHECK (testtext <> ''),
CONSTRAINT DissalowNull2
CHECK (LEN(testtext) = 0)
);
Neither of these two constraints work. I have tried using Nvarchar, Varchar, and Text as SQL Data Type all of which yielded this same result (Yes).
Any ideas?
You must indicate to the column that does not allow null
Create Table Riku(
ID int NOT NULL PRIMARY KEY,
testtext varchar(255) NOT NULL,
CONSTRAINT DissalowNulls
CHECK (testtext <> '')
);
I am interpreting your question as:
"Why is it when I create a constraint in SQL Server to 'DisalowNulls'
does it not appear that way when viewing the table properties in
Access?"
My answer to that questions is, "they are not syntax equivalent features". When Access interprets the design of the linked table it is not perceiving these as the same property. The constraint you created in SQL Server is more equivalent to an Access Validation Rule although that will also not appear in the Access table designer.
It would be nice if Access would disable properties that aren't relevant to the database type of the linked table. Other properties like Format, Input Mask, and Caption could also be in that category.

Can one alter a PostgresSql table to have an autogenerated keys after the table has values?

Is it possible to only alter a table to make an existing column a serial auto generated key, without adding a new column? Sorry if this question is a bit newbie-ish for PostgreSQL, I'm more a SQL Server person but moving to PostgreSQL..
In a nut shell the program will copying an existing SQL Server database into PostgreSQL. With the desire to have a mirrored DB in PostgreSQL as the source from SQL Server with the only caveat one may selectively include/exclude any table or column as desired, or do everything...
Given the process copies all values, thought one should be able create the keys after the copy has finished just as one may do in SQL Server. Thought PostgreSQL would have a comparable methods as SQL Server's SET INSERT_IDENTITY [ON|OFF] so one may override the auto generated key with a desired value. Not seeing an equivalent in PostgreSQL. So my fallback is to create the mirrored records in Postgres without keys any keys and then alter the tables. But it seems to fix up the table as desired one has create a new column, but doing this break or cause a headache fixing up the RI for PK/FK relationships.
Any suggestions? Thanks in advance.
In PostgreSQL, the auto-generated key is always overridden if you insert an explicit value for it. If you don't specify a value (omit the column), or specify the keyword DEFAULT, a generated key is used.
Given table
CREATE TABLE t1 (id serial primary key, dat text);
then both these will get a generated key from sequence t1_id_seq:
INSERT INTO t1 (dat) VALUES ('fred');
INSERT INTO t1 (id, dat) VALUES (DEFAULT, 'bob');
This will instead provide its own value:
INSERT INTO t1 (id, dat) VALUES (42, 'joe');
You are responsible for ensuring that the provided value doesn't conflict with existing data, or with future values the identity sequence will generate. PostgreSQL will not notice that you manually inserted a row with id 42 and skip when its own sequence counter gets to that point.
Usually what you do is load with provided values, then reset the sequence to the max of all keys already in the table, so it keeps counting from there for new local inserts.

Regex for MsSQL check constraint

I am trying to make a column level check constraint on a table I have. I want values being inserted into the columns to be checked and made sure they're only character.
For example values ('hello','there') would pass, but values ('h3llo','th3r3') would not.
I can get it to work for a specific amount of characters (see table below), but I want to make it dynamic.
I have tried ^[a-zA-Z]+$ as well, but that doesn't seem to work either.
The simple table layout is below.
CREATE TABLE owner
(
owner_id ID IDENTITY(1, 1) PRIMARY KEY,
owner_firstname FIRSTNAME,
owner_lastname LASTNAME,
CONSTRAINT firstname_cc CHECK (owner_firstname LIKE '[a-zA-Z][a-zA-Z][a-zA-Z]'),
CONSTRAINT lastname_cc CHECK (owner_lastname LIKE '[a-zA-Z][a-zA-Z][a-zA-Z]')
);
SQL Server LIKE syntax does not accept regular expressions.
You can use check (owner_firstname not like '%[^A-Z]%').
This constraint rejects any value containing a character not in the range A-Z.
You don't need to also specify a-z except if you are on a case sensitive collation.

An error occurred while the batch was being executed

I am trying to add a foreign key to my database table, but I am getting this error:
An error occurred while the batch was being executed.
Possible data issues
The column [dbo].[Transactions].[Expenses_Id] on table [dbo].[Transactions] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
Warnings
The column [dbo].[Transactions].[Expenses_Id] on table [dbo].[Transactions] must be added, but the column has no default value and does not allow NULL values. If the table contains data, the ALTER script will not work. To avoid this issue you must either: add a default value to the column, mark it as allowing NULL values, or enable the generation of smart-defaults as a deployment option.
But my other table (the one I am referring to using the FK) is not empty, so there should not be any null exceptions.
You are attempting to add new column which does not allow NULLs. Since there are some rows already, what value should be in every row right after column creation? That is NULL or default if defined. Create it with allowed NULLs, update to real values, alter to disable NULLs.

Resources