Get the App
SLTechnology News&Howtos  ›  Database  › 

The method of creating tables in sql

Shulou Source: shulou.com Published: 2022-06-01 01:46:31 09月23日 Update

This article will explain in detail about sql table creation method, Xiaobian feel quite practical, so share to everyone as a reference, I hope you can have some harvest after reading this article.

In sql, you can specify TABLE names (column name 1 data type, column name 2 data type,...) "to create a data table.

SQL creates databases, tables, and indexes

create a database

This creates a database:

CREATE DATABASE database name

create a table

This creates a table in the database:

CREATE TABLE table name (column name 1 data type, column name 2 data type,...)

examples

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

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

This example demonstrates how to define maximum lengths for certain columns:

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

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

create an index

Indexes are created in existing tables to make locating rows faster and more efficient. You can create indexes on one or more columns of a table, and each index is named. Users cannot see indexes, they can only be used to speed up queries.

Note: Updating a table with an index takes longer than updating a table without an index because the index itself needs to be updated. Therefore, it is desirable to create indexes only on columns that are often used for searching.

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 want to index.

Simple index

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

CREATE INDEX index name

ON Table Name (Column Name)

Column Name specifies the column you want to index.

examples

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

CREATE INDEX PersonIndexON Person (LastName)

If you want to index the values in a column in descending order, you can add the reserved word DESC after the column name:

CREATE INDEX PersonIndexON Person (LastName DESC)

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

CREATE INDEX PersonIndexON Person (LastName, FirstName) About sql table creation method shared here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Tags: Index name data type database more table update method example article presentation good practical maximum valid same two regular meetings examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Microsoft MariaDB MySQL