The basic operation of the database (2)

First, create a data table

Note that, before operating a data table, you should use "USE database name" to specify which operations are carried out in the database, otherwise it will throw "No database selected" error.

Create a data table of basic syntax is as follows:

CREATE TABLE 表名

(

  A field name, data type [integrity constraints],

2 field name, data type [integrity constraints],

......

Field name n, the data type [integrity constraints]

)     

For example, FIG:

 

 

 

In order to verify the data table is successfully created, you need to use the SHOW TABLES statement view, the specific implementation of the results is as follows:

 

 

 

Second, view the data table

Use SHOW CREATE TABLE to view the data in MySQL table, SHOW CREATE TABLE statement can be viewed not only create a definition statement when the table, you can also view the character encoding table. SHOW CREATE TABLE statement basic syntax is as follows:

SHOW CREATE TABLE 表名;

In the above format, the "table" refers to the name of the query to the data table.

Use the CREATE TABLE statement to see appled SHOW table, SQL statement is as follows:

SHOW CREATE TABLE appled;

Execution results are as follows:

 

 

 

In MySQL, use DESCRIBE statement can view field information table, including field names, field type and other information. The basic syntax DESCRIBE statement is as follows:

DESCRIBE 表名;

Or simply:

DESC table name;

Use DESCRIBE statement to see appled table, SQL statement is as follows

DESC appled;

Execution results are as follows:

 

 

 

Use DESCRIBE statement to view the data sheet:

NULL: indicate whether the column can store NULL values.

Key: Indicates whether the column has been indexed.

Default: indicates that the column has a default value.

Extra: indicates additional information relevant to a given column are acquired.

Second, modify the data table

(1) modify the table name

In the database, different data tables are distinguished by the table name. In MySQL, substantially modify the table name syntax is as follows:

ALTER TABLE old table name RENAME [TO] new table name;

In the above format, the "Old table name" refers to the name of the table before the amendment, "New table name" refers to a modified table name, keyword TO is optional, if it does not appear in the SQL statement affect the execution of the statement.

After modifying the database table name to see all the tables in the database with SHOW TABLES statement, execution results are as follows:

 

 

 

(2) modify the field name

Data field in a table is distinguished by a field name, the basic syntax is as follows:

ALTER TABLE table name CHANGE old name for the new field name field new data types;

The field data table name was changed to aple username, data type remains unchanged, SQL statements, as follows:

ALTER TABLE aple CHANGE name username INT(20);

In order to verify whether to amend the field name, you can view the structure of aple table by DECS statement, execution results are as follows:

 

 

 

Type data (3) modify the field of

Modify the field data type, the data type of the field is converted to another data type, the basic syntax is as follows:

ALTER TABLE table MODIFY Field Name Data Type;

We modify the data type of a field ega ALTER statement, SQL statement is as follows:

ALTER TABLE aple MODIFY ega CHAR(20);

In order to verify whether the data type of the id field of the amendment is successful, again using DECS View aple data tables, the results are as follows:

 

 

 

 

(4) Add Field

In MySQL, add fields basic syntax is as follows:

ALTER TABLE table name ADD name of the new field data types

      [Constraints] [FIRST | AFTER field name already exists]

INT type field age added without a constraint in the data table aple, SQL statements as follows:

ALTER TABLE aple ADD age INT(10);

In order to verify whether the age field added successfully, then, using the DESC statement to view the data sheet aple, execution results are as follows:

 

 

 

(5) removing fields

The so-called Delete field refers to a field removed from the table. In MySQL, delete the field of basic syntax is as follows:

ALTER TABLE table DROP field name;

Delete aple age field in the table, SQL statement is as follows:

ALTER TABLE aple DROP age;

In order to verify age field is deleted, Next, use DESC statement aple table view, the results are as follows:

 

 

 

 

(6) arranged to modify the position of the field

In MySQL, the arrangement position of the field to modify the basic syntax is as follows:

ALTER TABLE table MODIFY column name Data type FIRST | AFTER field name 2

The data of Table aple username field to modify the first field of the table, executing the SQL statement is as follows:

ALTER TABLE aple MODIFY username INT(20) FIRST;

In order to verify whether the username field to modify the first field in the table, then, the DESC statement see the data sheet is performed as follows:

 

 

 

(7) Delete Data Sheet

Delete to delete the data table refers to the table already exists in the database, delete data in tables, data stored in the data table will be deleted.

In MySQL, directly DROP TABLE statement can not delete the data table associated with the other tables, the basic syntax is as follows:

DROP TABLE 表名;

Delete data table aple, SQL statement is as follows:

DROP TABLE aple;

In order to verify whether the data sheet grade is deleted successfully, view the data table using the DESC statement, execution results are as follows:

 

 

 

The results can be seen, aple table does not exist, a data table aple was successfully deleted.

Third, table constraints

(1) primary key constraint

It is defined by the primary key constraint of PRIMARY KEY

In MySQL, a primary key constraint is divided into two, as follows:

1, a single primary key field

2, the multi-primary key field

Refers to a single-field primary key is the primary key consisting of a field, the basic syntax is as follows

Field Name Data Type PRIMARY KEY

Multi-field primary key is the primary key refers to a combination of a plurality of fields, the basic syntax is as follows:

PRIMARY KEY (field names 1, 2 field names, field names ...... n)

(3) non-null constraint

Refers to a non-empty constraint value field is not NULL, in MySQL, non-null constraint is defined by NOT NULL, the basic syntax is as follows:

Field Name Data Type NOT NULL;

(4) The only constraint

The only constraint is used to ensure the uniqueness of the value, i.e., a data field in a table field in the table is not repeated. The only constraint is defined by UNIQUE, the basic syntax is as follows:

Field Name Data Type UNIQUE;

(5) Default constraints

Default constraint to specify a default value for the field data in the table, i.e., when a new record is inserted in the table, if there is no assignment to the field, then the database system will automatically insert the default value for this field. The default value is defined by the DEFAULT keyword.

Default constraint basic syntax is as follows:

Field Name Data Type DEFAULT default;

Fourth, the automatic setting table field values ​​increased

In the data table, if you want to automatically generate a unique ID for the new record is inserted in the table, can be used to achieve AUTO_INCREMENT constraints.

AUTO_INCREMENT constraints may be any integer type field.

By default, the value of this field is from 1 starts to increment.

Use AUTO_INCREMENT field value setting table is automatically added to the basic syntax is as follows:

Field Name Data Type the AUTO_INCREMENT;

 

Guess you like

Origin www.cnblogs.com/XXxhl/p/11759306.html