Unique on Compound Columns - mysql

I am trying to ensure that the values in RESTAURANTID and TABLENUMBER, together, are Unique using PowerDesigner (12.5). I've tried creating an alternate key which resulted in the following in my .sql file:
create table TABLES
(
TABLEID int not null,
RESTAURANTID int not null,
TABLENUMBER int not null,
primary key (TABLESID),
key AK_mykey (RESTAURANTID, TABLENUMBER)
);
However with this, I can still enter identical values for RESTAURANTID and TABLENUMBER more than once.
I used this http://www.tek-tips.com/viewthread.cfm?qid=403554 to create the alternate key in PowerDesigner.
Would anyone know the proper way to achieve this in PowerDesigner?
Note: This isn't a duplicate of the question posted above as I'm looking for a way to achieve this in PowerDesigner without having to edit the generated sql file afterwards.

The unique property for keys (other than primary) in MySQL is stored as an extended attribute on the key.
You can modify it by displaying, and going to the MySQL tab in the Key properties dialog.
Or, in Model>Keys, you can use the Customize Columns and Filter button to show the Ext Unique (extended) property in the list of keys, so that you can set this unique property on several keys at once.
Or, you can create your own copy of the MySQL DBMS, and edit it. Under Profile>Key (using the right-click), add an event handler Initialize with the following Event Handler Script, so that each new key has ExtUnique set:
Function %Initialize%(obj)
obj.setextendedattribute "ExtUnique",true
%Initialize% = True
End Function

You are just adding a normal index. What you need is a unique index. Just replace key AK_mykey (RESTAURANTID, TABLENUMBER) with unique key AK_mykey (RESTAURANTID, TABLENUMBER) in your query and you are done.

In Power Designer:
Open the "TABLES" table properties (right click -> properties)
Keys tab
Insert row (name it to "AK_mykey")
Apply (previously inserted row saved)
On the "AK_mykey" row: right click -> properties
Columns tab
Add columns button
Select the required columns (RESTAURANTID, TABLENUMBER)
OK, OK, OK buttons
+1 You can check the result in table properties preview tab.
alter table TABLES
add unique AK_mykey (RESTAURANTID, TABLENUMBER);

Related

Is it possible to dynamically add SET values to a table?

I have a number of tables in which I need to reference scene IDs, which could be a SET. The problem is that I need to be able to update a set in a table that contains my login information for the app. This set needs to expand(or potentially shrink) based on the number of scenes that exist on the DB. Is it possible to do in phpmyadmin?
From what I've seen in the web interface, I must pre-define the SET values. But I cannot find any info on how to edit the SET's possible values after the column has been created.
What you have is a many-to-many relationship between logins and scenes.
The correct way to implement this is with three tables, for example:
CREATE TABLE logins (login_id INT PRIMARY KEY ...);
CREATE TABLE scenes (scene_id INT PRIMARY KEY ...);
CREATE TABLE login_has_scene (
login_id INT NOT NULL,
scene_id INT NOT NULL,
PRIMARY KEY (login_id, scene_id),
FOREIGN KEY (login_id) REFERENCES logins (login_id),
FOREIGN KEY (scene_id) REFERENCES logins (scene_id)
);
This way you can add new scenes anytime, and you can reference any scene from any login by adding one row per login-scene pair.
This is better than using a SET because SET requires you to redefine the list of values using ALTER TABLE every time you add a scene, and this will become quite a chore.
Also a SET column only allows up to 64 distinct values. If you ever want these tables to support further scenes, you'd have to add more SET columns, or start recycling scene ids or something.
The many-to-many table is a much better solution.
Frankly, I have been using MySQL for nearly 20 years, and I've never found a good use for the SET data type.

Validation rule to compare duplicates across multiple fields in access [duplicate]

We'd like to prevent record duplication in our MS access database using a multicolumn unique index. Because of how the data is sent (via network), duplicate data is sometimes received. The data source does not send a unique ID, so the simplest option is to prevent duplicate records being inserted.
According to Unique Index Design Guidelines:
With multicolumn unique indexes, the
index guarantees that each combination
of values in the index key is unique.
For example, if a unique index is
created on a combination of LastName,
FirstName, and MiddleName columns, no
two rows in the table could have the
same combination of values for these
columns.
This is for SQL 2005 however, so I'm not sure it's possible using MS access.
I guess an alternative is to perhaps use the query (pseudo code):
insert into foobar (a, b, c) values ('x', 'y', 'z')
where (a <> 'x') and (b <> 'y') and (c <> 'z')
... but I feel like an index would be better.
Turns out you can create a multi-column unique index on an MS access database, but it's a little crazy if you want to do this via the GUI. There's also a limitation; you can only use 10 columns per index.
Anyway, here's how you create a multi-column unique index on an MS access database.
Open the table in design mode, and Design, select Indexes.
Create a new row and enter a value in the Index Name cell,
Choose the first column from the drop down menu.
Add a new row and leave the Index Name cell blank.
Choose the second column, and so on.
Here's what it should look like:
Open the table in design view in MS Access, select the three columns that you want to make into the unique index, and then click the little key on the toolbar. You cannot have null values in a primary key (set).
We can make multi data to be unique data without set them as primary key.
(Note: only 1 data in the table can be primary key)
Step to set the data value as unique data (for MS ACCESS 2007 - 2010)
Open selected table in Design View
Click (Highlight) the specific column/attribute that you wish to set as unique
At the bottom of the table you will see "Index Properties" for that specific column
Find "Indexed" column, currently the data in Indexed text box is "No", change the data by click at the end of text box, choose "Yes(No Duplicates)"
Really hopes this methods can helps all of you! :)
I had the problem Nick Bolton reported above. Setting 2 fields (Foo, Bar) as PK set Foo to unique, when I wanted only the combination of Foo + Bar to be unique.
The problem turned out to be that I had created a 1:1 relationship to another table, linking on Foo. I deleted the relationship, set up the 2-field PK the way I wanted, and then reinstated the relationship, and it works as desired.
A trick I found is that in order to get a 2 column primary key (in the parent table) to be a child tableĀ“s 2(FK)+n primary key is to FIRST CHOOSE the indexed attribute as FISRT key attribute and THEN the not indexed attribute as a SECOND key attribute in the "Modify Relations Dialog Box" #Relations Window.
It will serve as Unique key in Ms Access 2007/2010

Not create a Primary Key when typing in a form

I was wondering if there is a way for a form to not generate a Primary Key as soon as I start typing on a field (this is just in case I don't really want to insert)? Can it show what Primary Key will be generated when I do/only generate and insert in the table?
There is a difference between a Primary Key and a automatically generated number. It is true that Access proposes an autonumber field as a PK, but you can definitely switch it to a number or character field. Then, you will have to find a way to enter manually the PK value, or generate it through code(*), before inserting the record in the table.
(*) In case you are using numbers as PKs, the logic could be here to check the higher PK value already in the table (max()) and add 1 to it.

How to create an primary key in phpmyadmin?

I have two tables, the first one is called BOOKSNAME and the second is called AUTHORNAME.
I have made the ID column for the BOOKSNAME table as the primary key (BIGINT auto increment) then my AUTHORNAME table has NAME and ADDRESS as it's columns but I have no ID/Primary key on this.
I want to make a relation between them by primary key, if anyone has an example of how this is achieved, could they share it?
What you need to do is add a new column to your AUTHORNAME table called AuthorID or something similar, you can select 'Primary' on a drop down list under the INDEX when adding a new column to your table, and you also want to tick the A_I box (auto increment).
If you are unsure how to add a new column, follow these steps:
Firstly go to your phpmyadmin, and select the table in which you need to add this new column/ primary key to, and select the structure tab.
Here you will see the current column headings that your table already has, so from here you want to look towards the bottom of that list of columns where it will display options on how to add a new column to your table.
Ideally, you want to add the ID at the beginning of the table, as it will make it look far more structured and easier to read.
From here you want to enter the name of the column, in your case AuthorID, the type will be an INT, and you will want to make your index as PRIMARY, then lastly you will need to tick the A_I / Auto increment box (This may appear differently depending on what version of phpmyadmin you are running).
If you then want to make relations between the two tables, you can use something called JOINS, if you do a simple Google search, you can find many guides on how these are carried out.
Also add integer primary key to the author table, next you can have author_id field to another table, which is same datatype as authors table primary key, and store author id to that field.
Also if you use Innodb engine you can add foreign key constraint, it is very useful, for more see documentation: http://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

Auto-increment primary key in SQL tables

Using Sql Express Management Studio 2008 GUI (not with coding), how can I make a primary key auto-incremented?
Let me explain: there is a table which has a column named "id" and the items of this column are set to be primary keys. I want to make this column auto-incremented, but how?
Cheers
Presumably you are in the design of the table. If not: right click the table name - "Design".
Click the required column.
In "Column properties" (at the bottom), scroll to the "Identity Specification" section, expand it, then toggle "(Is Identity)" to "Yes".
Although the following is not way to do it in GUI but you can get autoincrementing simply using the IDENTITY datatype(start, increment):
CREATE TABLE "dbo"."TableName"
(
id int IDENTITY(1,1) PRIMARY KEY NOT NULL,
name varchar(20),
);
the insert statement should list all columns except the id column (it will be filled with autoincremented value):
INSERT INTO "dbo"."TableName" (name) VALUES ('alpha');
INSERT INTO "dbo"."TableName" (name) VALUES ('beta');
and the result of
SELECT id, name FROM "dbo"."TableName";
will be
id name
--------------------------
1 alpha
2 beta
Right-click on the table in SSMS, 'Design' it, and click on the id column. In the properties, set the identity to be seeded # e.g. 1 and to have increment of 1 - save and you're done.
for those who are having the issue of it still not letting you save once it is changed according to answer below, do the following:
tools -> options -> designers -> Table and Database Designers -> uncheck "prevent saving changes that require table re-creation" box -> OK
and try to save as it should work now
I don't have Express Management Studio on this machine, so I'm going based on memory. I think you need to set the column as "IDENTITY", and there should be a [+] under properties where you can expand, and set auto-increment to true.
I think there is a way to do it at definition stage like this
create table employee(
id int identity,
name varchar(50),
primary key(id)
)..
I am trying to see if there is a way to alter an existing table and make the column as Identity which does not look possible theoretically (as the existing values might need modification)