Case insensitive check constraint in SQL database creation - sql-server

This is my create statement. I need to make a photo_filename constraint for it to be case insensitive to accept and form of .jpg JPG, JpG, jpG, etc.
CREATE TABLE P_PHOTO
(
PHOTO_ID CHAR(5) NOT NULL,
PHOTO_FILENAME VARCHAR2(60) DEFAULT 'NOIMAGE.JPG' NOT NULL,
PHOTO_TIMESTAMP TIMESTAMP(2) DEFAULT SYSDATE NOT NULL,
DOG_ID CHAR(5) NOT NULL,
CONSTRAINT P_PHOTO_PK PRIMARY KEY (PHOTO_ID),
CONSTRAINT P_PHOTO_P_DOG_FK
FOREIGN KEY (DOG_ID) REFERENCES P_DOG(DOG_ID),
This is what I have tried so far:
CONSTRAINT P_PHOTO_CHK CHECK (UPPER(PHOTO_FILENAME) LIKE '%.JPG')
CONSTRAINT P_PHOTO_CHK CHECK((PHOTO_FILENAME) LIKE %[.]jpg')

Related

PowerDesigner constraint name, how to default to lower case

I want all my tables, columns and references in the PDM to be generated as lower case values in the SQL scripts.
Tools > Model Options > Naming convention has been set to lower case for all the objects, but for some reason the primary key constraint name is defaulting to uppercase.
Is there a section of the menu that specifically sets the primary key that I am missing?
The Table Preview tab indicates all except the keys are being lower cased
I don't know what menu option I am missing I've tried all related to the model settings.
Example:
create table mtm_orders2customers (
objid SERIAL not null,
entry_date DATE null,
order_id INT4 null,
customer_id INT4 null,
payment_status VARCHAR(1000) null,
order_status VARCHAR(500) null,
order_total DECIMAL(12,2) null,
tax_due DECIMAL(12,2) null,
shipping_fee DECIMAL(12,2) null,
constraint PK_MTM_ORDERS2CUSTOMERS primary key (objid)
);
I tried with an Oracle Physical Model.
Looking in the DBMS, under ORACLE Version 18c::Script\Objects\PKey\ConstName, I see PK_%.U27:TABLE%.
If I change it to pk_%TABLE%, I'm closer to a lowercase primary key constraint name.
Same with ORACLE Version 18c::Script\Objects\Key\ConstName.

SQL Server : foreign key references invalid table

I'm not sure what I'm doing wrong, but I'm getting a foreign key references invalid table message telling me that dbo.ValueStream is an invalid table.
Edit: I originally posted my code in the incorrect order. Fixed in the edit to reflect the order it was done in SSMS
CREATE TABLE dbo.ValueStream
(
ValueStreamKey int IDENTITY(1,1) NOT NULL
CONSTRAINT PK_ValueStream_ValueStreamKey PRIMARY KEY,
ValueStream nvarchar(20),
)
CREATE TABLE dbo.NonConformance
(
NonConformanceKey int IDENTITY(1,1) NOT NULL
CONSTRAINT PK_NonConformance_NonConformanceKey PRIMARY KEY,
CustomerVendorKey int NOT NULL
CONSTRAINT FK_NonConformance_CustomerVendorKey_CustomerVendor_CustomerVendorKey
FOREIGN KEY REFERENCES dbo.CustomerVendor(CustomerVendorKey),
ValueStreamKey int NOT NULL
CONSTRAINT FK_NonConformance_ValueStream_Key_ValueStream_ValueStreamKey
FOREIGN KEY REFERENCES dbo.ValueStream(ValueStreamKey),
RepairOrder nvarchar(50) NOT NULL,
WorkOrder nvarchar(8) NOT NULL,
PartNumber nvarchar(50) NOT NULL,
SerialNumber nvarchar(50) NOT NULL,
PartDescription nvarchar(50) NOT NULL,
InductionDate datetime NOT NULL,
TriageStartDate datetime NOT NULL,
TriageCompletionDate datetime NOT NULL,
RequiresRRCA Bit NOT NULL,
NonConformanceSummary nvarchar(max) NOT NULL
);
taking out the CONSTRAINT FK_NonConformance_CustomerVendorKey_CustomerVendor_CustomerVendorKey
FOREIGN KEY REFERENCES dbo.CustomerVendor(CustomerVendorKey)
(we don't have that one)
and running the scripts seperately - first the first table and then the creation of the referencing table - this works for me.
No code errors.
You are creating dbo.NonConformance first in the script that you have posted. You need to move the CREATE TABLE dbo.ValueStream statement so that it is created first, ensuring there is a GO statement still between the two CREATE TABLE statements.
As it stands you are trying to create a foreign key to a table that does not exist.

Can't find where multiple cascade paths or cycles are in SQL Code?

I keep getting the following error
Msg 1785, Level 16, State 0, Line 99
Introducing FOREIGN KEY constraint 'FK_linkUserGroup_tblStudent' on table >'linkUserGroup' may cause cycles or multiple cascade paths. Specify ON DELETE ?>NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 0, Line 99
Could not create constraint or index. See previous errors.
I cannot understand how a cycle or multiple cascade paths are possible? I also cannot understand how a FK constraints to the TeacherID are not causing the same problem? My understanding is that should the parent StudentID be deleted then by specifying that On Delete No Action in the child table should ensure that the record is not deleted from the child table. I don't understand how their is any other path or possible cycle? Please help
CREATE TABLE tblUserAccount (
uaUserAccountID INT IDENTITY (1,1) CONSTRAINT PK_tblUserAccount PRIMARY KEY CLUSTERED,
uaUserAccountTitle NVARCHAR (50) NOT NULL,
uaUserAccountFirstName NVARCHAR (50) NOT NULL,
uaUserAccountSurname NVARCHAR (50) NOT NULL,
uaUserAccountUserName NVARCHAR (50) NOT NULL,
uaUserAccountPassword NVARCHAR (50) NOT NULL,
uaDateTimeModified DATETIME
CONSTRAINT DF_tblUserAccount_DateTimeModified DEFAULT SYSDATETIME()
);
CREATE TABLE tblRole (
rRoleID INT IDENTITY (1, 1) CONSTRAINT PK_tblRole PRIMARY KEY CLUSTERED,
rRole NVARCHAR (50),
rRoleDescription NVARCHAR (MAX) NULL,
rDateTimeModified DATETIME
CONSTRAINT DF_tblRole_DateTimeModified DEFAULT SYSDATETIME()
);
CREATE TABLE linkUserAccountRole (
uarUserAccountID INT,
uarRoleID INT,
uarDateTimeModified DATETIME
CONSTRAINT DF_linkUserAccountRole_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT PK_linkUserAcccountRole
PRIMARY KEY CLUSTERED (uarUserAccountID , uarRoleID)
,CONSTRAINT FK_linkUserAcccountRole_tblRole
FOREIGN KEY (uarRoleID)
REFERENCES tblRole (rRoleID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserAcccountRole_tblUserAccount
FOREIGN KEY (uarUserAccountID)
REFERENCES tblUserAccount (uaUserAccountID)
ON DELETE NO ACTION
ON UPDATE CASCADE
);
CREATE TABLE tblTeacher (
tTeacherID INT CONSTRAINT PK_tblTeacher PRIMARY KEY CLUSTERED,
tUserAccountID INT,
tDateTimeModified DATETIME
CONSTRAINT DF_tblTeacher_DateTimeModified DEFAULT SYSDATETIME(),
CONSTRAINT FK_tblTeacher_tblUserAccount
FOREIGN KEY (tUserAccountID)
REFERENCES tblUserAccount(uaUserAccountID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE tblStudent (
sStudentID INT CONSTRAINT PK_tblStudent PRIMARY KEY CLUSTERED,
sUserAccountID INT,
sDateTimeModified DATETIME
CONSTRAINT DF_tblStudent_DateTimeModified DEFAULT SYSDATETIME(),
CONSTRAINT FK_tblStudent_tblUserAccount
FOREIGN KEY (sUserAccountID)
REFERENCES tblUserAccount(uaUserAccountID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
CREATE TABLE tblKeyStage (
ksKeyStageGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblKeyStage PRIMARY KEY CLUSTERED,
ksKeyStageTitle NVARCHAR (50) NOT NULL,
ksKeyStageDescription NVARCHAR(50),
ksDateTimeModified DATETIME
CONSTRAINT DF_tblKeyStage_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblKeyStage_KeyStageTitle UNIQUE (ksKeyStageTitle)
);
CREATE TABLE tblYearGroup (
ygYearGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblYearGroup PRIMARY KEY CLUSTERED,
ygYearGroupTitle NVARCHAR (50) NOT NULL,
ygYearGroupDescription NVARCHAR(50),
ygDateTimeModified DATETIME
CONSTRAINT DF_tblYearGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblYearGroup_YearGroupTitle UNIQUE (ygYearGroupTitle)
);
CREATE TABLE tblClassGroup (
cgClassGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblClassGroup PRIMARY KEY CLUSTERED,
cgClassGroupTitle NVARCHAR (50) NOT NULL,
cgClassGroupDescription NVARCHAR (50),
cgDateTimeModified DATETIME
CONSTRAINT DF_tblClassGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblClassGroup_ClassGroupTitle UNIQUE (cgClassGroupTitle)
);
CREATE TABLE tblLearningGroup (
lgLearningGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblLearningGroup PRIMARY KEY CLUSTERED,
lgLearningGroupTitle NVARCHAR (50) NOT NULL,
lgLearningGroupDescription NVARCHAR (50),
lgDateTimeModified DATETIME
CONSTRAINT DF_tblLearningGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblLearningGroup_LearningGroupTitle UNIQUE (lgLearningGroupTitle)
);
CREATE TABLE tblLearningGroup (
lgLearningGroupID INT IDENTITY (1,1) CONSTRAINT PK_tblLearningGroup PRIMARY KEY CLUSTERED,
lgLearningGroupTitle NVARCHAR (50) NOT NULL,
lgLearningGroupDescription NVARCHAR (50),
lgDateTimeModified DATETIME
CONSTRAINT DF_tblLearningGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT UQ_tblLearningGroup_LearningGroupTitle UNIQUE (lgLearningGroupTitle)
);
CREATE TABLE linkUserGroup (
ugTeacherID INT,
ugStudentID INT,
ugKeyStageGroupID INT,
ugYearGroupID INT,
ugClassGroupID INT,
ugLearningGroupID INT,
ugDateTimeModified DATETIME
CONSTRAINT DF_linkUserGroup_DateTimeModified DEFAULT SYSDATETIME()
,CONSTRAINT PK_linkUserGroup
PRIMARY KEY CLUSTERED (ugTeacherID, ugStudentID, ugClassGroupID, ugLearningGroupID)
,CONSTRAINT FK_linkUserGroup_tblTeacher
FOREIGN KEY (ugTeacherID)
REFERENCES tblTeacher (tTeacherID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblStudent
FOREIGN KEY (ugStudentID)
REFERENCES tblStudent (sStudentID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblKeyStage
FOREIGN KEY (ugKeyStageGroupID)
REFERENCES tblKeyStage (ksKeyStageGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblYearGroup
FOREIGN KEY (ugYearGroupID)
REFERENCES tblYearGroup (ygYearGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblClassGroup
FOREIGN KEY (ugClassGroupID)
REFERENCES tblClassGroup (cgClassGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT FK_linkUserGroup_tblLearningGroup
FOREIGN KEY (ugLearningGroupID)
REFERENCES tblLearningGroup (lgLearningGroupID)
ON DELETE NO ACTION
ON UPDATE CASCADE
,CONSTRAINT UQ_linkUserGroup_PK_YearGroupID
UNIQUE (ugTeacherID, ugStudentID, ugClassGroupID, ugLearningGroupID, ugYearGroupID)
);
Multiple cascade paths exist between tblUserAccount and linkUserGroup:
tblUserAccount -> tblTeacher -> linkUserGroup
tblUserAccount -> tblStudent -> linkUserGroup
It is not the FK constraint itself which is not allowed, but the action: ON UPDATE CASCADE.
You can either specify ON UPDATE CASCADE for FK_linkUserGroup_tblTeacher, or ON UPDATE CASCADE for FK_linkUserGroup_tblStudent, but not for both.
Your ON DELETE is already marked as NO ACTION, so it is fine. It is ON UPDATE which you should decide what to do.
There are 2 options:
1) Forbid updating the key which participates in a FK constraint.
To do that specify ON UPDATE NO ACTION, or just remove the ON UPDATE clause altogether, because this is the default behaviour of a foreign key constraint.
Considering that updating a primary key is usually not a good idea (whether it is referenced by a foreign key or not), I would recommend this option.
2) If automatic propagation of changed sStudentID and tTeacherID to linkUserGroup is really what you want, then you can use triggers.

SQL Server error - Incorrect syntax for definition of the 'TABLE' constraint

T-SQL error on SQL 2008 when I run this code.
This type of question has been asked a few times in the past on Stack Overflow, and is normally to do with a comma being in the wrong place, but for the life of me I can't see what is wrong with the following SQL
CREATE TABLE IntroducerSupportLink
(
IntroducerSupportLinkId int identity,
IntroducerId nvarchar(12) not null,
SupportStaffId nvarchar(12) not null,
Status numeric(5),
SupportStaffType numeric(5),
CreatedDateTime datetime not null,
CONSTRAINT PK_IntroducerSupportLink_IntroducerSupportLinkId Primary key(IntroducerSupportLinkId),
CONSTRAINT FK_IntroducerSupportLink_IntroducerId foreign key(IntroducerId) REFERENCES Introducer(introducerId),
CONSTRAINT FK_IntroducerSupportLink_SupportStaffId foreign key(SupportStaffId) REFERENCES Introducer(introducerId),
CONSTRAINT DF_IntroducerSupportLink_CreatedDateTime DEFAULT (GETDATE())
)
It runs if the last (default) constraint is commented out
Try following,
CREATE TABLE IntroducerSupportLink
(
IntroducerSupportLinkId int identity,
IntroducerId nvarchar(12) not null,
SupportStaffId nvarchar(12) not null,
Status numeric(5),
SupportStaffType numeric(5),
CreatedDateTime datetime not null DEFAULT GETDATE(),
CONSTRAINT PK_IntroducerSupportLink_IntroducerSupportLinkId Primary key(IntroducerSupportLinkId),
CONSTRAINT FK_IntroducerSupportLink_IntroducerId foreign key(IntroducerId) REFERENCES Introducer(introducerId),
CONSTRAINT FK_IntroducerSupportLink_SupportStaffId foreign key(SupportStaffId) REFERENCES Introducer(introducerId)
)
You can also do this thing inline
CreatedDateTime datetime not null CONSTRAINT "constraints_CreatedDateTime" DEFAULT GETDATE(),
You can not define DEFAULT constraints at table level by design, see here
You didn't "attach" the last constraint to any column.
To do that with a DEFAULT constraint, you must use it on the column definition itself (see full syntax here)
Try below code..
create TABLE IntroducerSupportLink5
(
IntroducerSupportLinkId int identity,
IntroducerId nvarchar(12) not null,
SupportStaffId nvarchar(12) not null,
Status numeric(5),
SupportStaffType numeric(5),
CreatedDateTime datetime not null constraint DF_IntroducerSupportLink_CreatedDateTime DEFAULT (GETDATE()),
CONSTRAINT PK_IntroducerSupportLink_IntroducerSupportLinkId Primary key(IntroducerSupportLinkId),
CONSTRAINT FK_IntroducerSupportLink_IntroducerId foreign key(IntroducerId) REFERENCES Introducer(introducerId),
CONSTRAINT FK_IntroducerSupportLink_SupportStaffId foreign key(SupportStaffId) REFERENCES Introducer(introducerId)
)

Dropping PRIMARY KEY constraint

I have created a table which has two columns combined as a primary key.
CREATE TABLE [dbo].[Workflow_Name]
(
[Workflow_ID] [int] NOT NULL,
[Unique_Workflow_ID] [int] NOT NULL,
[Workflow_Name] [varchar](255) NULL,
[Row_ID] [int] NULL,
[ReleaseVersion] [varchar](255) NULL,
[Release] [varchar](255) NULL,
CONSTRAINT [PK_WorkFlowName] PRIMARY KEY CLUSTERED
([Workflow_ID] ASC, [Unique_Workflow_ID] ASC )
)
As seen , [Workflow_ID] ASC, [Unique_Workflow_ID] ASC combined together are forming the Primary key.
Now i want to remove [Unique_Workflow_ID] from the Primary key constraint and maintain only [Workflow_ID] as Primary Key.
How to do it?
You can do it by executing the following statements in SSMS Query Window after selecting the database that the table is in.
ALTER TABLE [dbo].[Workflow_Name]
DROP CONSTRAINT [PK_WorkFlowName]
ALTER TABLE [dbo].[Workflow_Name]
ADD CONSTRAINT [PK_WorkFlowName] PRIMARY KEY ([Workflow_ID] ASC)
Please note, in order for it to work, if you have existing rows in the [Workflow_Name], then the data in this new single column Primary Key i.e. in [Workflow_ID] must be unique per row. Otherwise the ALTER statement will (rightly) throw an error that data is not unique in that column.
You can remove your primary key constraint and then create a new one only with the Workflow_ID. But make sure it has only unique values.

Resources