0
0
MySQLquery~5 mins

Primary key declaration in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a primary key in a database table?
A primary key is a column or a set of columns that uniquely identifies each row in a table. It ensures no two rows have the same key value.
Click to reveal answer
beginner
How do you declare a primary key when creating a table in MySQL?
You declare a primary key using the PRIMARY KEY keyword after the column definition or at the end of the column list in the CREATE TABLE statement.
Click to reveal answer
beginner
Can a primary key column contain NULL values?
No, a primary key column cannot contain NULL values because it must uniquely identify each row.
Click to reveal answer
beginner
What happens if you try to insert duplicate values into a primary key column?
The database will reject the insert and return an error because primary keys must be unique.
Click to reveal answer
beginner
Show an example of declaring a primary key on a single column in MySQL.
Example:<br>
CREATE TABLE users (<br>  id INT NOT NULL,<br>  name VARCHAR(100),<br>  PRIMARY KEY (id)<br>);
Click to reveal answer
Which of the following is true about a primary key?
AIt allows NULL values.
BIt can have duplicate values.
CIt is optional and does not affect uniqueness.
DIt uniquely identifies each row and cannot be NULL.
How do you declare a primary key on the 'id' column when creating a table?
APRIMARY KEY id;
BUNIQUE KEY id;
CPRIMARY KEY (id);
DKEY PRIMARY id;
What will happen if you insert two rows with the same primary key value?
ABoth rows will be inserted.
BThe second insert will fail with an error.
CThe database will automatically change the second value.
DThe first row will be deleted.
Can a table have more than one primary key?
ANo, a table can have only one primary key.
BYes, multiple primary keys are allowed.
CYes, but only if they are on different columns.
DOnly if the keys are composite keys.
Which keyword is used to declare a primary key in MySQL?
APRIMARY KEY
BUNIQUE
CFOREIGN KEY
DINDEX
Explain what a primary key is and why it is important in a database table.
Think about how you find a unique person in a list.
You got /3 concepts.
    Describe how to declare a primary key in a MySQL CREATE TABLE statement with an example.
    Look at the structure of CREATE TABLE and where PRIMARY KEY fits.
    You got /3 concepts.