Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

First Name and Last Name must be Unique Together in SQL

I know that using the following codes will make each column have unique values, but what if I want a full name to be unique?

CREATE TABLE people (first_name varchar2(32) unique,
                     last_name varchar2(32) unique);

this will make each attribute unique on its own, but I need to make them both together unique, like If I have a name "James Smith", I don't want this name to be repeated again, but its ok if there was a "James Sunderland" guy.

like image 919
Negoti Leboti Avatar asked Dec 30 '25 00:12

Negoti Leboti


1 Answers

Define the UNIQUE constraint on the combination of the two columns:

CREATE TABLE people 
  ( first_name varchar2(32) , 
    last_name varchar2(32) ,
    UNIQUE ( first_name, last_name )
  ) ;
like image 124
ypercubeᵀᴹ Avatar answered Dec 31 '25 16:12

ypercubeᵀᴹ