MySQL Basics

  1. For row values in an existing table without changing table definition.

    • Add new values (we've seen this!)
      INSERT INTO abductee(fname, lname, zipcode, numTrips)  VALUES ('Mickey','Mouse','99999',3);
                           
    • Change but don't delete values that are already there. (Fix up table!)
      UPDATE abductee     SET addressline='Disney World',zipcode='37383'     WHERE lname='Mouse';
                           
    • Remove ROWS from a table. (Fix up table!)
      DELETE FROM abductee     WHERE lname='Mouse';

  2. But, what if you *do* want to change a table definition. Is it nececcary to "start all over"? NO!

    In the below, columns added to an existing table with existing data will have NULL stored as their value for the existing rows.

    • ALTER TABLE tablename
            ADD COLUMN columnname columntype
            [FIRST | AFTER columnname ];
      

    • ALTER TABLE tablename
            DROP COLUMN columnname;
      

    • ALTER TABLE tablename
            CHANGE COLUMN columnname newcolumnname newcolumntype;
      

    • ALTER TABLE oldtablename
            RENAME AS newtablename;