Create a database, table, index, delete the index, see table on how to use indexes and tables index

Create a database

Doing so can create a database:

CREATE DATABASE database name

Create a table

Doing so can create a database table:

CREATE TABLE table name 
( 
Column Name Data Type 1, 
column name Data type 2, 
....... 
)

Examples

This example demonstrates how to create a table named "Person", there are four columns. Column name is: "LastName", "FirstName", "Address" and "Age":

CREATE TABLE Person 
(
LastName varchar,
FirstName varchar,
Address varchar,
Age int
) 

This example shows how to define the maximum length of some of the columns:

CREATE TABLE Person 
(
LastName varchar(30),
FirstName varchar,
Address varchar,
Age int(3)
)

Data type (data_type) column specifies what type of data can be received. The following table contains the most commonly used SQL data types:

type of data description
  • integer(size)
  • int(size)
  • smallint(size)
  • tinyint(size)
Only hold an integer. Specify the maximum number of digits in parentheses.
  • decimal(size,d)
  • numeric(size,d)

Receiving with decimals.

"Size" specifies the maximum number of digits. "D" specifies the maximum number of digits to the right of the decimal point.

char(size)

Receiving a fixed length string (accommodates letters, numbers and special characters).

A predetermined length of the string in the parenthesis.

varchar(size)

Receiving a variable length string (accommodates letters, numbers and special characters).

The maximum length of the string specified in parentheses.

date(yyyymmdd) Accommodate date.

Creating an index

The index was created in an existing table, it can locate rows more quickly and efficiently. You can create an index on one or more columns in a table, each index will be a name. The user can not see the indexes, they can only be used to speed up queries.

Note: The update contains a table index requires more time than updating a table without an index, this is because the index itself needs to be updated. Therefore, the ideal approach is to just above the columns are often used to create a search index.

The only index (Unique Index)

Create a unique index on a table top. Unique index means that two rows can not have the same index value.

CREATE UNIQUE INDEX index name 
ON table names (column names)

"Column Name" provides you need to index the columns.

Simple Index

Create a simple index on a table. When we omit keyword UNIQUE, you can use the duplicate values.

CREATE INDEX index name 
ON table names (column names)

"Column Name" provides you need to index the columns.

Examples

This regular meeting to create a simple index, named "PersonIndex", LastName field in the Person table:

CREATE INDEX PersonIndex
ON Person (LastName) 

If you want to descending index value of a column, you can add the reserved word DESC after the column name:

CREATE INDEX PersonIndex
ON Person (LastName DESC) 

If you want to index more than one column, you can list the names of columns in parentheses, separated by commas:

INDEX PersonIndex the CREATE 
ON the Person (LastName, FirstName) 

delete the index
Drop index index name on table name

index view of the table
show index index name from the table name

Guess you like

Origin www.cnblogs.com/mark5/p/11113868.html