Home  >  Article  >  Database  >  How to create a table in sql

How to create a table in sql

藏色散人
藏色散人Original
2020-11-04 13:43:2450417browse

In sql, you can create a data table through the syntax "CREATE TABLE table name (column name 1 data type, column name 2 data type,...)".

How to create a table in sql

Recommended: "sql tutorial"

SQL creates databases and tables And index

Create database

You can create a database by doing this:

CREATE DATABASE 数据库名称

Create a table

You can create a table in the database by doing this:

CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
.......
)

Example

This example demonstrates how to create a table named "Person" with four columns. The column names are: "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 for some columns:

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

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

How to create a table in sql

Create Index

Indexes are created on existing tables , which makes positioning rows faster and more efficient. Indexes can be created on one or more columns of a table, and each index will be given a name. Indexes are not visible to the user; they can only be used to speed up queries.

Note: Updating a table that contains an index takes more time than updating a table without an index because the index itself also needs to be updated. Therefore, it is ideal to create indexes only on columns that are frequently used for searches.

Unique Index

Create a unique index on the table. A unique index means that two rows cannot have the same index value.

CREATE UNIQUE INDEX index name

ON table name (column name)

"Column name" specifies the column you need to index.

Simple Index

Create a simple index on the table. When we omit the keyword UNIQUE, duplicate values ​​can be used.

CREATE INDEX index name

ON table name (column name)

"Column name" specifies the column you need to index.

Example

This example will create a simple index named "PersonIndex" in the LastName field of the Person table:

CREATE INDEX PersonIndex
ON Person (LastName)

If you want to index a column in descending order For values ​​in , 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 column names in brackets, separated by commas:

CREATE INDEX PersonIndex
ON Person (LastName, FirstName)

The above is the detailed content of How to create a table in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn