Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools Database Foundations Joining Relational Data Between Tables in SQL Keys and Auto-Incrementing Values

Ryan Quinn
Ryan Quinn
2,027 Points

Creating Primary and Unique Keys

The Question is:

Create a genres table called "t_genres" with an auto incrementing primary key called "pk_id" and a unique column called "uk_name" that can't be null. The "uk_name" is a varchar of up to 45 characters.

I cannot seem to write a proper statement for this one.

3 Answers

Ryan Chatterton
Ryan Chatterton
5,914 Points

MS SQL:

create table t_genres (
pk_id int IDENTITY(1,1) Primary key,
uk_name varchar(45) not null
constraint uc_name unique (uk_name)
)

 insert into t_genres (uk_name)
 values ('bob')

 go

 insert into t_genres (uk_name)
 values ('bob')

for MySql:

create table t_genres (
pk_id int not null auto_increment primary key,
uk_name varchar(45) not null,
unique (uk_name)
)

now to pass the challenge which is a bug in my book.

create table t_genres (
pk_id int not null PRIMARY KEY auto_increment, uk_name varchar(45), unique(uk_name)
);

bug being the PRIMARY KEY they are saying must come before auto_increment which is not true.

Ryan Quinn
Ryan Quinn
2,027 Points

I appreciate your help, but neither of those passes the challenge. I can't figure it out.

Ryan Chatterton
Ryan Chatterton
5,914 Points

The very last one will. That's what I wrote to pass the challenge.

Ryan Quinn
Ryan Quinn
2,027 Points

Thank you for your help Ryan.