How to eliminate duplicate ids depending on another id - database

For my project in jsp ,
I want to insert values into the database
I have shop_Id and clothe_id (there are many shop each one having I'd).
I want to add clothes(there are many clothes and they too have I'd) ,
but for each shop the cloth I'd should not be repeated.
what query should I write to handel it in jsp .
shop_id and clothes_id are coming dynamically
Eg:- a) correct
shop I'd clothe I'd
0 0
0 1
0 2
1 0
1 1
1 2
Eg:-b) wrong
shop I'd clothe I'd
0 0
0 0 (wrong duplicate value)
1 0
The eg:- b) cause's data duplication.
What query should I write to Handel it

This is managed by your database
CREATE TABLE shop_cloth(
shop_id integer REFERENCES shop,
cloth_id integer REFERENCES cloth,
PRIMARY KEY(shop_id, cloth_id) --ensure uniqueness of combinaison shop_id, cloth_id or UNIQUE(shop_id, cloth_id) if you use another field for primary key
);
You should learn how works database and SQL (SQL is the language used by most relational database, like PostgreSQL)

Related

Auto Generate Unique ID separate from Primary Key

I have a Relational database (that I'm new to creating) that has many tables relating to baseball statistical tracking and analysis. The table I'm working on currently is the tbl_PitchLog table, that tracks pitches in an at-bat.
What I'd like to do is eliminate the need for an at-bat table and just use At-Bat Unique ID for a group of pitches. I have a Pitch_ID field, but I'd like SS to generate a new AtBat_ID that I can then reference to get all the pitches of a particular at-bat.
For Example
Pitch_ID | Pitch_Number | Result
1 1 Foul
2 2 Foul
3 3 Strike
4 1 Ball
5 2 Flyout
6 1 Groundout
To be:
Pitch_ID | AtBat_ID | Pitch_Number | Result
1 1 1 Foul
2 1 2 Foul
3 1 3 Strike
4 2 1 Ball
5 2 2 Flyout
6 3 1 Groundout
You don't specify what version of SS you're using, but SQL Server 2012 introduced sequences; you can create an at bat sequence, get the next value when the at bat changes, and use that value for your inserts.
CREATE SEQUENCE at_bat_seq
AS INTEGER
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE <what you want the max to be>
NO CYCLE;
DECLARE #at_bat int;
SET #at_bat = NEXT VALUE FOR at_bat_seq;
Most of the qualifiers are self-explanatory; the [NO] CYCLE specifies whether the value will start over at the min when it hits the max. As defined above, you'll get an error when you get to the max. If you just want it to start over when it gets to the max, then specify CYCLE instead of NO CYCLE.
Create the tbl_PitchLog table with a Pitch_ID as its primary key, while it's at the same time a foreign key taken from the main table.
What you're looking for is a one to one relationship.

Putting clustered index on a join used column vs heavily scanned column?

I have this simple table :
Table Users
userId | name
---------------------
1 'a1'
2 'a2'
3 'a3'
4 'a4'
5 'a5'
Table Cities
cityId | name
---------------------
1 'c1'
2 'c2'
3 'c3'
4 'c4'
5 'c5'
Each user is can be in more than one city. :
So the mapping table is :
userId | CityId
------------------------------------
1 4
1 4
1 4
2 5
5 6
Table users is heavily scanned by name .
Question :
For the mapping table I have no issues. both columns together are primary/clustered index.
But i'm struggling with myself about the first 2 tables :
I think that Users should have userId column as primary key. why ? because it is used throug the join to the mapping table.
but I also need clustered index on the name column cause this table is heavily scanned by name.
(leave aside the unique problem. lets say all columns are unique)
What is the best practice decision for this case ?
The best decision depends on how exactly you use the data returned by a query.
A clustered index means that the data in the page files are ordered based on this index.
A regular index will have it's own page files to order the index and a pointer to the physical row.
Thus a clustered index will serve better for theses queries that return a range of value instead of unique rows.
So, unless you do a lot of queries with like operations on the Name column, you would be better to keep your clustered index on the ID column, for this index will be constantly scanned and used to return recordsets to support your join operations.

How to (or Can I) select a random value from the Postgresql database excluding some particular records?

Is it possible to randomly select a record from the database excluding some records with particular status?
For eg,
For example, I have a table for storing employee details.
id employeename employeestatus
1 ab 1
2 cd 1
3 ef 2
4 gh 1
5 ij 1
What I want from the query is to fetch a single random record whose status is not 2. Is it possible to do so? The database I'm using is PostgreSQL 8.4.15.
TRY This
SELECT *
FROM employee
WHERE employeestatus != 2
ORDER BY RANDOM()
LIMIT 1
Try this other question on the same topic
Best way to select random rows PostgreSQL
It's tricker than you think (to do efficiently)

EF or SQL Server - Computed / Continous Column

I'm not sure how to do this and where it should be implemented.
I have a table with columns: ID, TypeID, AIndex. Both the ID and TypeID are supplied when creating a new record. The AIndex is a continuous column based on both IDs.
To illustrate here an example:
ID TypeID Aindex
---------------------
1 1 1
1 1 2
1 2 1
1 3 1
2 1 1
2 1 2
The big question is:
They are lot of people writing to this table at a time?
When you compute Aindex column from previous database data, some thing like max( Aindex ) + 1 the risk is to induce locks.
If only few inserts are made in table you can write increment code in your favorite layer, EF or DB. But if your table has high write ratio you should search for a alternate technique like counters table or something else. You can keep this counters table with EF if you want.

Sql Server 2008 unique columns by condition

Let's assume I have following table in the database:
Id ProductId ColorId IsDeleted
1 1 1 1
2 1 1 0
3 2 3 0
I want to make ProductId and ColorId columns unique but only for those rows where IsDeleted = 0. How do I can achieve this requirement?
I know, I can create a constraint which will call a stored function. And stored function will try to find an entry with the same values. But I think it is to complex way. May be there is a better decision...
In SQL Server 2008 and newer, you can take advantage of filtered indices to achieve this:
CREATE NONCLUSTERED UNIQUE INDEX ProductColor
ON dbo.YourTable(ProductID, ColorID)
WHERE IsDeleted = 0;
See the Filtered Index Design Guidelines for some more background and best practices.

Resources