06-update-delete.sql 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. CREATE TABLE students (
  2. ID INTEGER PRIMARY KEY AUTOINCREMENT,
  3. name VARCHAR(25),
  4. major VARCHAR(25)
  5. );
  6. .mode table
  7. .nullvalue NULL
  8. INSERT INTO students(name, major) VALUES('Jack', 'Biology');
  9. INSERT INTO students(name, major) VALUES('Kate', 'Sociology');
  10. INSERT INTO students(name, major) VALUES('Claire', 'Chemistry');
  11. INSERT INTO students(name, major) VALUES('Jack', 'Biology');
  12. INSERT INTO students(name, major) VALUES('Mike', 'Computer Science');
  13. SELECT * FROM students;
  14. /*
  15. * Actualiza la información de todos los
  16. * registros que cumplen con la condición.
  17. */
  18. UPDATE students SET major = 'Bio'
  19. WHERE major = 'Biology';
  20. UPDATE students SET major = 'Comp Sci'
  21. WHERE major = 'Computer Science';
  22. SELECT * FROM students;
  23. UPDATE students SET major = 'Comp Sci'
  24. WHERE id = 4;
  25. -- Utilizando operadores lógicos.
  26. UPDATE students SET major = 'Biochemistry'
  27. WHERE major = 'Bio' OR major = 'Chemistry';
  28. SELECT * FROM students;
  29. -- Actualiza varias columnas.
  30. UPDATE students SET name = 'Tom', major = 'Undecided'
  31. WHERE id = 1;
  32. SELECT * FROM students;
  33. -- Actualiza todas las columnas.
  34. UPDATE students SET major = 'Undecided';
  35. SELECT * FROM students;
  36. /*
  37. * Elimina todos los registros
  38. * de la tabla de estudiantes.
  39. */
  40. --DELETE FROM students;
  41. DELETE FROM students WHERE id = 5;
  42. SELECT * FROM students;
  43. /*
  44. * Elimina todos los registros
  45. * que cumplen con la condición.
  46. */
  47. DELETE FROM students
  48. WHERE name = 'Tom' AND major = 'Undecided';
  49. SELECT * FROM students;