Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.