Course

Multiple Tables - Solution

In the previous lesson we discussed needing these columns for our table:

  • id
  • title
  • content
  • createdAt
  • updatedAt

If you gave it an attempt- hopefully you ended up with something like this:

CREATE TABLE Posts (
  id INT PRIMARY KEY AUTO_INCREMENT,
  title VARCHAR(100) NOT NULL,
  content TEXT NOT NULL,
  createdAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  updatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);

All of the types and clauses and constraints should be familiar- except maybe the use of .

is just like . They're both used to store strings of data. Except is better for long amounts of text. We use for data we can assume the max length of- and TEXT for large blobs of text like a blogpost or a message board post.

Good uses for VARCHAR

  • Names
  • Emails
  • Titles
  • Addresses
  • Usernames

Good uses for TEXT

  • Articles or Blog Posts
  • Product Descriptions
  • User Comments or Reviews
  • Message board Posts

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Connecting Tables - Exercise