- What is the difference between DDL and DML in SQL?
- DDL is the Data Definition Language, which carries out the creation and modification of the database structure. DML is the Data Manipulation Language, which carries out queries and maintenance of data. SQL is the industry standard for both. DDL statements build and change the tables, while DML statements search and maintain the data in them.
- How do I define a new database and a table in SQL?
- Use CREATE DATABASE with the name of the database to define a new database, for example CREATE DATABASE SHOPORDERS;. CREATE TABLE names the table, then lists each attribute with its data type inside brackets. End each attribute line with a comma and finish with a closing bracket and a semicolon.
- Which data type should I use for an identifier such as 0123 in SQL?
- Use CHAR or VARCHAR, not INTEGER. An identifier such as 0123 is stored as CHAR or VARCHAR, not as a number. The data types are CHARACTER, VARCHAR(n), BOOLEAN, INTEGER, REAL, DATE and TIME, so choose the one that fits each attribute.
- How do I set a primary key and a foreign key in an SQL table?
- Add a primary key with PRIMARY KEY (field) and a foreign key with FOREIGN KEY (field) REFERENCES Table (Field). The foreign key must match the primary key of the table it references. To link a foreign key to a table that already exists, use ALTER TABLE with ADD FOREIGN KEY.
- How do I join two tables in an SQL query?
- Name both tables after FROM and give the match after ON, for example FROM CONTAINER INNER JOIN SHIP ON CONTAINER.ShipID = SHIP.ShipID. The other accepted method lists both tables after FROM and puts the match in WHERE. Write the table name before a field name that appears in both tables.
- What mistake do students make when they correct an SQL script?
- Students often identify an error but do not rewrite it. When asked for errors, give the line number and the corrected line. A frequent error was the use of COUNT instead of SUM in the SELECT clause, because SUM totals a field while COUNT counts the entries in a field.