Making more than one primary key in hsqldb - hsqldb

I have a mssql-server query which contains something similar to the query like below;
CONSTRAINT PK_Application PRIMARY KEY CLUSTERED(ApplicationName, ApplicationID),
CONSTRAINT IX_Application UNIQUE NONCLUSTERED (ApplicationParentID)
I tried writing like below in hsqldb,
CONSTRAINT PK_Application PRIMARY KEY(ApplicationName, ApplicationID),
CONSTRAINT IX_Application PRIMARY KEY(ApplicationParentID)
but, i get error:
primary key already exist
Is there anyway to achieve the same functionality in hsqldb?

Tried with the UNIQUE keyword and looks like it works!
CONSTRAINT PK_Application PRIMARY KEY(ApplicationName, ApplicationID),
CONSTRAINT IX_Application UNIQUE(ApplicationParentID)
I get no more errors if i write like above.

Related

ORA-00905: missing keyword (copied from textbook)

Have tried looking through the code up-and-down and cannot get it to work. Was mostly trying out the Create table function. Worked fine until trying to create a table with composite primary key. Absolutely stumped. Any help is appreciated.
Tried removing the names for the keys, but then was redirected to a different sort of error: "invalid datatype"
Create Table Cust_Artist_EOI (ArtistID Number(38) Not Null,
CustomerID Number(38) Not Null,
Constraint Cust_Artist_EOI_PK Primary Key(ArtistID, CustomerID),
Constraint Cust_Artist_EOI_ArtistFK Foreign Key(ArtistID)
References MyArtist(ArtistID)
On Update No Action
On Delete Cascade,
Constraint Cust_Artist_EOI_CustFK Foreign Key(CustomerID)
References MyCustomer(CustomerID)
On Update No Action
On Delete Cascade);
The problem is the on update. Remove that:
Create Table Cust_Artist_EOI (
ArtistID Number(38) Not Null,
CustomerID Number(38) Not Null,
Constraint Cust_Artist_EOI_PK Primary Key(ArtistID, CustomerID),
Constraint Cust_Artist_EOI_ArtistFK Foreign Key(ArtistID)
References MyArtist(ArtistID) On Delete Cascade,
Constraint Cust_Artist_EOI_CustFK Foreign Key(CustomerID)
References MyCustomer(CustomerID) On Delete Cascade
);
Here is a db<>fiddle.
If you look in the syntax diagram for foreign key constraint in Oracle, you will see that neither on update nor no action is supported.
I'm not sure why no action is not supported, because that is the default behavior (and I think it is a good idea to be able to express default behavior).
Not supporting on update is a better idea. It discourages changes to primary keys. And changing primary keys is generally a bad idea.

Cannot find primary key column in table

I added a constraint primary key to my table and the query ran successfully. The dilemma I am facing is I cannot locate the primary key column in my table.
ALTER TABLE salesdata
ADD CONSTRAINT pk_salesdata PRIMARY KEY( "Address_of_New_Home","Sq_Ft","Build_Spec", "Realtor_Sale","Can","Actual_Sale_Date")
When I do:
select * from salesdata
It shows all the columns from before and no primary key column (pk_salesdata).
And even more baffling is when:
select pk_salesdata from salesdata
Database shows:
ERROR: column "pk_salesdata" does not exist
I want to add primary key column to the table. I humbly request assistance of databasers.
You create PRIMARY KEY, but...
you create composed primary key from columns "Address_of_New_Home", "Sq_Ft", "Build_Spec", "Realtor_Sale", "Can", "Actual_Sale_Date" - it is not good idea
your primary key have an alias name pk_salesdata, but it's only constraint name
you didn't create new column
If you would like new synthetic primary key column you have to use command:
ALTER TABLE salesdata ADD COLUMN mynewautoincrementid SERIAL PRIMARY KEY;
Primary Kay it's constraint in table when you add a primary key to any column you can select column name to show data
It looks like you've wrapped the columns you want to include in your primary keys in double quotes. That's not how this command works. Drop the quotes re-run the command and see what happens.
ALTER TABLE salesdata
ADD CONSTRAINT pk_salesdata PRIMARY KEY( Address_of_New_Home,Sq_Ft,Build_Spec, Realtor_Sale,Can,Actual_Sale_Date)
It might be easier to add a primary index through the SQL GUI.
Here's the MS documentation page for creating a primary key.
https://msdn.microsoft.com/en-us/library/ms189039.aspx
Note: Since your adding a primary key don't expect it to be available as a column.

Difference between using or not using CONSTRAINT keyword on SQL Server

What is the difference between using or not using the CONSTRAINT keyword when working with Foreign Keys on SQL Server?
I noticed that apparently both worked the same in this specific case, without CONSTRAINT:
CREATE TABLE ClientsPhones
(
ClientPhone varchar(10) NOT NULL,
ClientID smallint NOT NULL,
PRIMARY KEY (ClientPhone),
FOREIGN KEY (ClientID) REFERENCES Clients(ClientID)
);
And with CONSTRAINT:
CREATE TABLE ClientsPhones
(
ClientPhone varchar(10) NOT NULL,
ClientID smallint NOT NULL,
PRIMARY KEY (ClientPhone),
CONSTRAINT fk_ClientID
FOREIGN KEY (ClientID) REFERENCES Clients(ClientID)
);
Both didn't let me add records to the table unless the ClientID already existed on the Clients table, and the same ClientID and ClientPhone weren't already on the ClientsPhones table.
Is there any real difference between the two besides the fact that I'm able to name the constraint?
If you don't create constraint.it will automatically create own constraint name
the foreign key index name is generated using the name of the referencing foreign key column Automatically.
So there is no way to see difference of using and not using Constraint keyword. by default constraint name will be defined.
I did some research and don't believe Hell Boy's answer was as clear as it could be and had some misinformation.
Every constraint you add to a database has a name set by default. This includes PRIMARY KEY, FOREIGN KEY, DEFAULT, NOT NULL. It isn't necessarily the name of the column(s) used.
You can imagine that when you don't use the CONSTRAINT keyword SQL Server puts it there as well as generates a name for you.
If you want to remove or change a constrain you would either have to delete the entire table and recreate it with the correct constraints or you can reference the constraint by name and then alter it somewhat like a column using the ALTER keyword. This can be useful for when you need to delete a table with a foreign key. If you name the foreign key constraint you can delete it and then the table instead of having to delete the table the foreign key points to.

Why use named key constraint (Eg: Foreign Key) [duplicate]

This question already has answers here:
Differences between "foreign key" and "constraint foreign key"
(3 answers)
Closed 9 years ago.
I am not much familiar with the SQL and please explain me the difference of following two and what is the best way to use. Is there any advantage using one over a another.
CREATE TABLE Employee
(
Emp_Id int NOT NULL,
Dep_Id int NOT NULL,
...
FOREIGN KEY (Dep_Id) REFERENCES Department(Dep_Id)
);
And
CREATE TABLE Employee
(
Emp_Id int NOT NULL,
Dep_Id int NOT NULL,
...
CONSTRAINT fk_EmpDept FOREIGN KEY (Dep_Id) REFERENCES Department(Dep_Id)
);
I always name my keys and constraints, generally with enough information that anybody looking at the key name will be able to understand what the key does.
Foreign keys I name FK_FieldName#TableName.
Primary keys I name PKC_TableName or PKN_TableName ("c" for "clustered" and "n" for "nonclustered", although this is not critical).
For indexes, I name them based upon their uniqueness, their clusteredness, and an "I" for index, e.g., UNI_FieldName#TableName.
The reason for naming is for the convenience both if you ever need to drop the object and, more importantly, if something in your code attempts to violate the relationship / constraint. It's immediately apparent where the problem is, if you get a message back saying that you've had a key violation of key FK_FieldName#TableName but not so clear if the name doesn't make sense.
The only difference is in naming. If you don't explicitly set a name for constraint, it will has auto generated name. For example: FK_Employee_Dep_Id__164452B1 . And you'll see this name in description of table keys, in different exceptions and so on.
If you do not name a foreign key, the db implicitly assign a foreign key name for the field.
And if you name a foreign key, the db uses that as foreign key constant name.
You can use drop FOREIGN KEY foreign_key_name for dropping the constraint (for MySql).
You can use drop constraint foreign_key_name for dropping the constraint (for Oracle, MS Sql etc).
You can use
ALTER TABLE TableName
ADD CONSTRAINT foreign_key_name
FOREIGN KEY (field)
REFERENCES foreign_key_table(Foreign_key_field)
First example with FOREIGN KEY (Dep_Id) REFERENCES Department(Dep_Id) is used in MySQL and it does not allow you to give this constraint specific name.
In the second example with CONSTRAINT fk_EmpDept FOREIGN KEY (Dep_Id) REFERENCES Department(Dep_Id) you can change fk_EmpDept to basically any name you want.
It can be used in MySQL, SQL Server, Oracle, MS Access.
Using second version you can also created Foreign Key constraint on several columns.

Trouble understanding SQL (Oracle) create table code

I am aware of Oracle's create table syntax
CREATE TABLE MyTable(
id int primary key,
...
);
This will create a table called MyTable with an int primary key. So, nothing new here.
but I am having difficulties understanding the following query:
CREATE TABLE departament (
cod_dept INTEGER CONSTRAINT dept_key PRIMARY KEY,
dept_name CHAR(15) NOT NULL,
admission DATE NOT NULL,
localization CHAR(20))
When I look up on Oracle's SQL Developer software on departement's table, I can see 4 columns: cod_dept, dept_name, admission and localization. On the constraints tab, I can also see dept_key, but I am confused as to what this might mean. What is dept_key purpose here?
Edit
Ok, seems it is a way to define the name of the constraint you're adding to the table. My next question is why don't you just call it the same name as the primary key column? From what I've seen it seems Oracle by default just creates a random name for the constraint!
Thanks
When you write id int primary key, Oracle will create a primary key constraint to ensure uniqueness of primary key values. All constraints have names, so in this case Oracle assigns an autogenerated name to this constraint. But you can set a name of this constraint explicitly using the CONSTRAINT syntax:
cod_dept INTEGER CONSTRAINT dept_key PRIMARY KEY
This name may be used later to refer to the constraint, for example, to delete or modify it:
ALTER TABLE department DROP CONSTRAINT dept_key;
EDIT:
Constraint names are unique across the schema, so Oracle can't just use the name of primary key column as a constraint name.
Primary keys can be explicitly be named. dept_key is just a name.
dept_key is the name of the primary key constraint. That means cod_dept is the unique identifier for your table, the mechanism for identifying a row, and so it can only have one occurrence of any given value.
That is the constraint you created representing the primary key.
A table is made up of:
Columns (where the data lives)
Indexes (indexed copies of the data used for faster searching)
Constraints (rules about what data can be in the table, including PK, FK, and check constraints).
dept_key is the name of the constraint. You specified the name here : "INTEGER CONSTRAINT dept_key PRIMARY KEY," so it will create a constraint with the name dept_key.
Another syntax for the same would be to write the following after your CREATE TABLE instruction.
ALTER TABLE department
ADD CONSTRAINT dept_key PRIMARY KEY (cod_dept)
dept_key is then the name of the constraint you created to be the primary key for this table. In order for a database engine to know the primary key, and to index it for fastest results and so forth, it needs to create a known constraint that is indexed. Here, it is you who has given the name which is dept_key.
For you kind information, it is often seen to write PK_[table name] for primary keys constraints and FK_[current_table_name]_[foreign_table_name] for foreign keys constraints.
Hope this helps! =)
I think whenever we create a Primary Key value then by default Oracle will crate constraint for it with the same name but it looks like that u are creating constraint with some other name.
Thank You