I have been trying recently to change the default value of a column as described here. The solution I found there requires deleting the default constraint, which requires knowing the name of that constraint.
The problem I am having is that I do not know how to find out the constraint name in MariaDB. In the same post, these solutions were suggested:
SELECT NAME FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID('dbo.MyTable');
sp_help MyTable
sp_helpconstraint MyTable
All of these produce syntax errors in MariaDB, since they all seem to be for MSSQL or MySQL.
So, what do I need to do to find out the constraint name?
Constraints are shown in the output of SHOW CREATE TABLE <table-name> along with everything else that relates to a table.
Related
I have an existing table in Sqlite. How do I add a unique constraint to it?
You can't add a constraint to existing table in SQLite,(In SQL there is a option for do that). You need to re-create the table with necessary contraints.
There is only a few option available for alter table command in sqlite. Please check the image:
Also check Sqlite org reference.
EDIT
However you can add unique index for your table to achieve the same effect.
So you can use the following query to achieve that:
CREATE UNIQUE INDEX your_unique_index ON your_table(column_name);
In most cases, UNIQUE and PRIMARY KEY constraints are implemented by
creating a unique index in the database.
Reference : SQLite Constraints
You can add constraint by creating unique index:
CREATE UNIQUE INDEX ux_friend_name ON friend(name);
where ux_friend_name is unique index name, friend(name) - table and its columns that will be affected by this constraint.
You can read more here.
I am assuming you are using SQLiteManager from FireFox, please create the table again, normally it doesn't allow to change the constraints when you have already created a table.
NOTE - It can be done using code too, this is another way of doing it..
Edited
See the image below
I'm trying to write a constraint for a table that only allows a column named "filename" to accept entries that contain '.docx', '.doc', '.pdf', and '.txt.' I'd also like the file extensions to be case insensitive so, '.PDF' and '.pDF' etc, would also be accepted. I've tried this code:
ALTER TABLE DOCS ADD CONSTRAINT DOCS_EXTENSION_CHK CHECK(FILENAME IN ('.docx', '.doc', '.pdf', '.txt'));
I've tried inserting multiple names for files with different extensions such as, 'RANDOMFILE.doc' and 'FILENAME.PdF' but it's I'm receiving an error for everything.
I need the constraint to also allow the name of the file before the extension to be anything, like 'xxxx.docx'.
If anyone could help me out I would really appreciate it.
Use regexp_like():
ALTER TABLE DOCS ADD CONSTRAINT DOCS_EXTENSION_CHK
CHECK (REGEXP_LIKE(LOWER(FILENAME), '[.]docx$|[.]doc$|[.]pdf$|[.]txt$'));
Or more simply as:
ALTER TABLE DOCS ADD CONSTRAINT DOCS_EXTENSION_CHK
CHECK (REGEXP_LIKE(LOWER(FILENAME), '[.](docx|doc|pdf|txt)$'));
Regular expression is good. But you could simply use lower() and like() like below
ALTER TABLE DOCS ADD CONSTRAINT DOCS_EXTENSION_CHK
CHECK (
lower(FILENAME ) LIKE ('%.docx')
or lower(FILENAME ) LIKE ('%.doc')
or lower(FILENAME ) LIKE ('%.pdf')
or lower(FILENAME ) LIKE ('%.txt')
);
I looked around for an answer to this. How to add a column using SQL in an Oracle Database.
I keep finding the same answer, but my Oracle SQL Developer tool keeps telling me that the syntax is wrong even though I write it exactly as they do.
What am I missing exactly? (Before you ask, yes I do use ALTER TABLE before this)
The syntax is supposed to be:
ADD Column_Name constraint Data_Type;
Issue is, I have no constraints for this column so I've seen examples not use it. I tried that as well and I get the same error. The value can be null and have no constraints, yet I am not allowed to do this:
ADD SERIES_YEAR NUMBER(2,10);
Any suggestions? It's probably something incredibly simple.
EDIT: Here is the error it gives me:
You are missing parentheses. Try the following:
ALTER TABLE
Foo
ADD
(
SERIES_YEAR NUMBER(2,10) NOT NULL,
);
You have to write the data type before the constraint, not the opposite, like:
ADD
(
column1_name column1_datatype column1_constraint,
column2_name column2_datatype column2_constraint
);
in orther to get a column similar to the mysql ENUM type, I wrote a sql query as follows
ALTER TABLE [DbName].[dbo].[MediaContent]
ADD MediaType nvarchar(50)
check(MediaType in ('audio','video','song','other'))
this worked as wished(for test): But now I want to delete this column without success. It seems like there no way to directly delete a column which has a constraint up on it.
How can I solve this issue? I want to delete this column and create another one.
here is the error message I get while the deletion
The object 'CK__MediaCont__Media__14270015' is dependent on column 'MediaType'.
ALTER TABLE DROP COLUMN MediaType failed
because one or more objects access this
column. (Microsoft SQL Server, Error: 5074)
The object referenced in the error message is the name of the constraint. You should be able to use the follow:
ALTER TABLE [DbName].[dbo].[MediaContent]
DROP CONSTRAINT CK__MediaCont__Media__14270015
You need to first drop the check constraint mentioned in the error message since that's stopping you from dropping the column. Following that you may drop the column.
Drop the constrain first then drop the column ,it will work
I have the following query which is adding contraint.
but in order to add, i want to check if this key has already been used or not?
ALTER TABLE HL7_MessageHierarchy
ADD CONSTRAINT fk_vMessageType FOREIGN KEY (vMessageType)
REFERENCES HL7_MessageType(vMessageType);
for example. if i have to add a column, i can easily check if the table exists in sysobjects and its respective column exists in syscolumns.
Is it possible to use the query multiple times without GO and without making any error indeed? if yes then how ???
[EDIT]
I don't know why my browser not allowing me to add comments so i am adding to Edit.
I want to check if there exists any foreign key with same name. so if there is no data even then the query can make problem because the key may already be existing. I want to run the above script clean (ofcourse resident data does matter but that is perhaps a straight forward check?)
[EDIT]
my bad, i must have known that version is important... I believe its 2005... (will love to know if someone can tell for other versions too)
I assume you mean
check the HL7_MessageHierarchy for values not inHL7_MessageType"
So, a query like this will tell you
SELECT *
FROM HL7_MessageHierarchy H
WHERE NOT EXISTS (SELECT *
FROM HL7_MessageType T
WHERE H.vMessageType = T.vMessageType)
Also, I'd recommend using WITH CHECK too
ALTER TABLE HL7_MessageHierarchy WITH CHECK ADD
CONSTRAINT fk_vMessageType FOREIGN KEY (vMessageType)
REFERENCES HL7_MessageType(vMessageType);
In SQL 2005, the recommended way of checking for the existence of objects is Catalog Views. The one you want is sys.foreign_keys:
IF NOT EXISTS ( SELECT * FROM sys.foreign_keys
WHERE name = 'fk_vMessageType' )
BEGIN
EXEC ('
ALTER TABLE HL7_MessageHierarchy
ADD CONSTRAINT fk_vMessageType FOREIGN KEY (vMessageType)
REFERENCES HL7_MessageType(vMessageType)
')
END
I have wrapped the creation in EXEC to avoid confusing the parser.