SQL is required. Note chapter 17th to create and manipulate tables. SQL is required.

Source: Internet
Author: User

SQL is required. Note chapter 17th to create and manipulate tables. SQL is required.
17.1 create a table

There are two ways to create a table.
(1) Most DBMS have interactive table creation and management tools
(2) tables can also be directly manipulated using SQL statements.
Syntax difference: in different SQL implementations, the syntax of the CREATE TABLE statement may be different.

17.1.1 table creation Basics

To CREATE a TABLE using create table, the following information must be provided:
(1) name of the new TABLE, which is given after the keyword create table.
(2) names and definitions of table columns, separated by commas.
(3) Some DBMS also need to specify the table location.
Create a Products table

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};

Replace existing tables
When creating a new table, the specified table name must not exist. Otherwise, an error occurs. To avoid overwriting an existing table, SQL requires that you manually delete the table and recreate it, instead of simply overwriting it with the table creation statement.

17.1.2 use NULL

Columns that allow NULL values can also be inserted without the value of this column. A column with a NULL value is not allowed to accept rows without a value in the column. In other words, when inserting or updating a row, the column must have no value.
Specify NULL: when not null is NOT specified for most DBMS systems, NULL is specified, but NOT all DBMS systems do.
Primary Key and NULL value: the primary key uniquely identifies each row in the table. Only columns that do not allow NULL values can be used for the primary key. Columns that allow NULL values cannot be used as unique identifiers.
Understand NULL:Do NOT confuse NULL values with empty strings. The NULL value does NOT have a value. It is NOT an empty string. If ''is specified, this is allowed in the not null column. A null string is a valid value. It is not a null value. The NULL value is specified with the keyword NULL instead of an empty string.

17.1.3 specify the default value

SQL allows you to specify the default value. If no value is provided during the insert operation, DBMS automatically uses the default value. 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,     prod_id          CHAR(10)     NOT NULL,     quantity         INTEGER     NOT NULL     DEFAULT 1,     item_price      DECIMAL(8,2)   NOT NULL};

Obtain system date

17.2 update a table

To update the TABLE definition, you can use the alter table statement.
Use alter table.
(1) Generally, do not update the table when it contains data.
(2) All DBMS allow adding columns to the alumni table, but there are restrictions on the Data Types of the added columns (and the use of NULL and DEFAULT.
(3) Many DBSM statements do not allow you to delete or modify columns in a table.
(4) Most DBMS allow renaming columns in a table.
(5) Many DBMS have restrictions on the change of Columns with data already filled in, and there are almost no restrictions on columns with data not filled in.
To use alter table to change the TABLE structure, the following information must be provided:
(1) Name of the TABLE to be modified after alter table (the TABLE must exist; otherwise, an error will occur)
(2) list of changes
Add Column

ALTER TABLE VendorsADD vend_phone CHAR(20);

Delete column

ALTER TABLE VendorsDROP COLUMN vend_phone;

Be careful when using alter table: be extremely careful when using alter table. Make a complete backup before the change. Changes to database tables cannot be undone. If you add unnecessary columns, you may not be able to delete them. Similarly, if you delete a column that should not be deleted, all data in the column may be lost.

17.3 delete a table

Delete a TABLE (Delete the entire TABLE instead of its content) and use the drop table statement.

DROP TABLE CustCopy;
17.4 rename a table

Table rename supported by different DBMS is different.
Oracle RENAME

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.