End of Summer - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Default values
How to auto-generate values in a database
16. Summary

Instruction

Great! We've reached the final section for this part. Let's review what we've learned.

A database architect can define a default value for a column. You can use DEFAULTs ...

  • ... in an INSERT statement:
    INSERT INTO author (id, first_name, last_name, photo, create_timestamp, is_active) VALUES
    (120, 'Gary', 'Brown', 'imgs/gary.jpg', '2018-08-25', DEFAULT);
  • ... by omitting columns in INSERT:
    INSERT INTO author (id, first_name, last_name) VALUES
    (121, 'Mary', 'Taylor');
  • ... during an UPDATE operation:
    UPDATE author
      photo = DEFAULT
    WHERE id = 120;

Plus, you learned how to auto-generate unique values by:

  • Using SERIAL and omitting the name of the ID column:
    INSERT INTO author(first_name, last_name) VALUES
    ('Stanley', 'Lewis');
  • Using DEFAULT to generate a SERIAL value:
    INSERT INTO author VALUES
    (DEFAULT, 'Mark', 'Barker', 'imgs/barker1.jpg', DEFAULT, DEFAULT);
  • Using a sequence:
    INSERT INTO author (id, first_name, last_name, is_active) VALUES
    (NEXTVAL('id_seq',  'Miriam', 'Clerk', 1);

Remember, auto-generated values from the same sequence can be used in multiple tables and each value will be unique.

Exercise

Well done! You've completed Part 3 and learned the various ways PostgreSQL can automatically generate values.

Click Next exercise to move on to Part 4.