A composite unique key is a unique key made up of a combination of columns. Oracle creates an index on the columns of a unique key, so a composite unique key can contain a maximum of 16 columns.
A composite unique key designates a combination of columns as the unique key. To satisfy a unique constraint, no two rows in the table can have the same value for the unique key. However, the unique key made up of a single column can contain Nulls.
A database designer must define one or more composite KEYs whenever each row of the table under consideration has to be uniquely differentiated by the values of one or more combinations of columns.
Key Differences Between Primary key and Unique key: A table can have only one primary key whereas there can be multiple unique key on a table.
Answer the question:
use Doctrine\ORM\Mapping\UniqueConstraint;
/**
* Common\Model\Entity\VideoSettings
*
* @Table(name="video_settings",
* uniqueConstraints={
* @UniqueConstraint(name="video_unique",
* columns={"video_dimension", "video_bitrate"})
* }
* )
* @Entity
*/
See @UniqueConstraint
I find it more verbose to use
only ORM and then prefix ORM
in annotations. Also note that you can break annotation to several lines to make it more readable especially if you have several items to mention (index in the example below).
use Doctrine\ORM\Mapping as ORM;
/**
* VideoSettings
*
* @ORM\Cache(usage="NONSTRICT_READ_WRITE")
* @ORM\Entity(repositoryClass="AppBundle\Repository\VideoSettingsRepository")
* @ORM\Table(name="emails", uniqueConstraints={
* @ORM\UniqueConstraint(name="dimension_bitrate", columns={"video_dimension", "video_bitrate"})
* }, indexes={
* @ORM\Index(name="name", columns={"name"})
* })
*/
class VideoSettings
I know this is an old question, but I came across it while looking for a way to create composite PK and thought it could use some update.
Things are actually much simpler if what you need is a Composite Primary Key. (Which, of course, guarantees uniqueness) Doctrine documentation contains some nice examples by this url: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/tutorials/composite-primary-keys.html
So the original example could look something like this:
/**
* @var string $videoDimension
*
* @ORM\Id @ORM\Column(type="string")
*/
private $videoDimension;
/**
* @var string $videoBitrate
*
* @ORM\Id @ORM\Column(type="string")
*/
private $videoBitrate;
A few notes here:
videoDimension
and videoBitrate
are both parts of the PK - there is no need to specify nullable = false
In case someone want use PHP 8 Attributes instead of Doctrine annotations:
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
#[ORM\UniqueConstraint(
name: 'video_unique_idx',
columns: ['video_dimension', 'video_bitrate']
)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With