Certainly! Here is another example of creating a table with a different context, where one column is constrained as the primary key:
Suppose we are creating a table for a library system to keep track of books. We will use the ISBN (International Standard Book Number) as the primary key, as it uniquely identifies each book.
The SQL syntax would be as follows:
CREATE TABLE Books (
ISBN VARCHAR(13) NOT NULL,
Title VARCHAR(255) NOT NULL,
Author VARCHAR(255) NOT NULL,
PublicationYear INT NOT NULL,
CONSTRAINT PK_ISBN PRIMARY KEY (ISBN)
);
Explanation:
CREATE TABLE Books: This command creates a new table namedBooks.ISBN VARCHAR(13) NOT NULL: This defines a column namedISBNwith a data type ofVARCHARand a maximum length of 13 characters. TheNOT NULLconstraint ensures that theISBNfield cannot contain NULL values.Title VARCHAR(255) NOT NULL: This defines a column namedTitlewith a data type ofVARCHARand a maximum length of 255 characters. TheNOT NULLconstraint ensures that theTitlefield cannot contain NULL values.Author VARCHAR(255) NOT NULL: This defines a column namedAuthorwith a data type ofVARCHARand a maximum length of 255 characters. TheNOT NULLconstraint ensures that theAuthorfield cannot contain NULL values.PublicationYear INT NOT NULL: This defines a column namedPublicationYearwith a data type ofINT(integer). TheNOT NULLconstraint ensures that thePublicationYearfield cannot contain NULL values.CONSTRAINT PK_ISBN PRIMARY KEY (ISBN): This adds a constraint to the table, naming itPK_ISBN, and sets theISBNcolumn as the primary key for the table. The primary key constraint ensures that each value in theISBNcolumn is unique and not null.
This syntax will successfully create the Books table with ISBN as the primary key.