SQL must know - create and manipulate tables

"SQL must know must know" reading notes

1. Create a table

There are generally two ways to create a table:

  • Most DBMSs have tools for interactively creating and managing database tables;
  • Tables can also be manipulated directly with SQL statements.

2. Table Creation Basics

To create a table with CREATE TABLE, the following information must be given:

  • the name of the new table, given after the keyword CREATE TABLE;
  • table column names and definitions, separated by commas;
  • Some DBMSs also require the specified table to be unknown.
CREATE TABLE Products
(
    prod_id        CHAR(10)      NOT NULL,
    vend_id        CHAR(10)      NOT NULL,
    prod_name      CHAR(254)     NOT NULL,
    prod_price     DECIMAL(8,2)  NOT NULL,
    prod_desc      VARCHAR(1000) NULL
);

Tip: Replacing an existing table
When creating a new table, the specified table name must not exist, otherwise an error will occur. To prevent accidental overwriting of an existing table, SQL requires that the table be manually dropped first and then recreated, rather than simply overwriting it with a rebuild table statement.

3. Use NULL values

A NULL value is no value or a missing value. Unlike the empty string, the empty string ' ' is a value.

CREATE TABLE Vendors
(
    vend_id       CHAR(10)     NOT NULL,
    vend_name     CHAR(50)     NOT NULL,
    vend_address  CHAR(50)     ,
);

NULL is the default setting, if NOT NULL is specified, it is assumed that the specified value is NULL.

Note:
When specifying NULL and not specifying NOT NULL, most DBMSs think that NULL is specified, but not all DBMSs are restricted. DB2 requires that the keyword NULL be specified, and an error will occur if not specified.

Tip: Primary Keys and NULL Values ​​A
primary key is a column whose value uniquely identifies each row in a table. Only columns that do not allow NULL values ​​can be used as primary keys, and columns that allow NULL values ​​cannot be used as unique identifiers.

4. Specify default values

The default value is specified with the keyword DEFAULT in the column definition of the CREATE TABLE statement.

CREATE TABLE OrderItems
(
    order_num     INTEGER      NOT NULL,
    order_item    INTEGER      NOT NULL,
    quantity      INTEGER      NOT NULL        DEFAULT 1,
);

Default values ​​are often used for date or timestamp columns. For example, use the system date as the default date by specifying a function or variable that references the system date.

Hint: use DEFAULTR instead of NULL values.

5. Update table

Here are some things to consider when using ALERT TABLE.

  • Ideally, do not update the table when it contains data. The possible future needs should be fully considered in the design process of the table to avoid major changes to the structure of the table in the future.
  • All DBMSs allow adding columns to existing tables, but there are restrictions on the data type of the added columns (and the use of NULL and DEFAULT).
  • Many DBMSs do not allow dropping or changing columns in a table.
  • Most DBMSs allow renaming of columns in a table.
  • Many DBMS restrictions make changes to columns that are already populated with data, and there are few restrictions on columns that are not populated with data.

To change the table structure using ALTER TABLE, the following information must be given:

  • give the name of the table to be changed after ALTER TABLE (the table must exist, otherwise an error will occur);
  • List what changes are to be made.
ALTER TABLE Vendors
ADD vend_phone CHAR(20);
ALTER TABLE Vendors
DROP COLUMN vend_phone;

Complex table structure changes generally require a manual delete process, which involves the following steps:

  • Create a new table with the new column layout;
  • Copy data from the old table to the new table using the INSERT SELECT statement. If necessary, use transformation functions and calculated fields;
  • Check the new table that contains the required data;
  • Rename the wine label (you can delete it if you are sure);
  • Rename the new table with the original name of the old table;
  • Recreate triggers, stored procedures, indexes, and foreign keys as needed.

NOTE: CAUTION USING ALTER TABLE
Use extreme caution with ALTER TABLE, you should do a full backup (schema and data backup) before making changes. Changes to database tables cannot be undone, and if unneeded columns are added, they may not be dropped. Similarly, if you delete a column that shouldn't be deleted, you may lose all data in that column.

6. Delete table

Dropping a table (deleting the entire table instead of its contents) is as easy as using the DROP TABLE statement:

DROP TABLE CustCopy;

7. Rename the table

DB2, MariaDB, MySQL, Oracle, and PostgreSQL users use the RENAME statement, SQLServer users use the sp_rename stored procedure, and SQLite users use the ALTER TABLE statement.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324696314&siteId=291194637