Informatics Practices · Ch 7 — Introduction to Structured Query Language (SQL)
Summary
Summary
- A database is a collection of related tables; MySQL is a relational DBMS. A table is a collection of rows and columns, where each row is a record and the columns describe the features (attributes) of the records.
- SQL is the standard language for most relational database management systems, and it is case insensitive —
SELECT,selectandSelecTall work the same way. CREATE DATABASEcreates a new database;USEmakes the specified database the active one.CREATE TABLEcreates a table; every attribute in a CREATE TABLE statement must have a name and a datatype. Constraints restrict the values a column may hold — a primary key uniquely identifies each record, while a foreign key declares that an index (column) in one table is related to that in another table.ALTER TABLEmakes changes in the structure of a table — adding, removing, or changing the datatype of column(s).DESCfollowed by the table name shows the structure of the table.INSERT INTOinserts record(s) in a table;UPDATEmodifies existing data in a table;DELETEremoves records from a table. In both UPDATE and DELETE, omitting the WHERE clause makes the statement act on every record of the table.SELECTretrieves data from one or more database tables, displayed in tabular form;SELECT * FROM table_namedisplays data from all the attributes of the table.- The
WHEREclause enforces condition(s) in a query, using relational operators (=,<,<=,>,>=,!=) combined with the logical operatorsAND,ORandNOT. String values in conditions are enclosed in quotes. ASgives a column an alias — a display-only heading in the output (quoted if it contains a space); it does not add a new column to the stored table.DISTINCTeliminates repetition and displays each value only once.BETWEENdefines a range of values inclusive of both boundary values;INselects values that match any value in a given list, andNOT INexcludes them. …