postgres语法

A table is a group of data stored in a database.

表是存储在数据库中的一组数据。

To create a table in a database you use the  CREATE TABLE  statement. You give a name to the table and a list of columns with its datatypes.

要在数据库中创建表,请使用CREATE TABLE语句。 您为表命名,并为其数据类型指定列列表。

CREATE TABLE TABLENAME(Attribute1 Datatype, Attribute2 Datatype,........);

Here’s an example creating a table named Person:

这是一个创建名为Person的表的示例:

CREATE TABLE Person(
  Id int not null,
  Name varchar not null,
  DateOfBirth date not null,
  Gender bit not null,
  PRIMARY KEY( Id )
);

In the example above, each Person has a Name, a Date of Birth and a Gender. The Id column is the key that identifies one person in the table. You use the keyword  PRIMARY KEY  to configure one or more columns as a primary key.

在上面的示例中,每个人都有一个姓名,出生日期和性别。 Id列是标识表中一个人的键。 您可以使用关键字PRIMARY KEY将一个或多个列配置为主键。

A column can be  not null  or  null  indicating whether it is mandatory or not.

列不能为not nullnull指示它是否为必需的。

(A More In-Depth Guide to the SQL Guide to the CREATE TABLE command)

This guide is an overview to the basics of the SQL  CREATE TABLE  functions.

本指南概述了SQL CREATE TABLE函数的基础知识。

We will be using MySQL for all examples throughout these freeCodeCamp SQL guides. MySQL is a used frequently on websites for the backend database, 2) it’s free, and is fun and easy to use.

这些freeCodeCamp SQL指南中的所有示例都将使用MySQL。 MySQL在后端数据库的网站上经常使用,2)它是免费的,并且有趣且易于使用。

(Covered in this Guide)

  • Creating a schema, the container for all our database objects.
  • Create a table so we have something to alter.
  • Creating a table by importing a CSV file and altering that table
  • Creating a table using the MySQL workbench tool

We do most of this work with SQL statements in the MySQL workbench scripting tool. We will also see how to Create a table using the workbench interface instead of with SQL statements.

我们使用MySQL工作台脚本工具中SQL语句来完成大部分工作。 我们还将看到如何使用工作台界面而不是SQL语句创建表。

(High level structure of a Relational Database)

  1. Highest level; The Database; the database system installation. In this case, it’s MySQL. Called “Local instance MySQL Router” in the screen shots above.
  2. Next is a Schema; a container for the objects needed to managed data in a relational database system.
  3. Objects we create (tables, indexes, stored procedures, functions) to manage the system and its data

(Creating a MySQL schema)

The schema is a container for a the objects required to manage the data for a given subject or process. We show examples as we progress in this guide.

模式是用于管理给定主题或过程的数据所需的对象的容器。 我们将在本指南中逐步展示示例。

We’ll create the schema for our learning and testing using the SQL command;

我们将使用SQL命令创建用于学习和测试的架构;

create database fCC_alterTableGuide;

This instances schema structure prior to running this command

运行此命令之前,此实例架构结构

(Creating a table, add test data with “insert”, rename the table (alter))

We’ll create a Student Table.

我们将创建一个学生表。

The steps will be:

步骤将是:

  1. make sure we don’t have the table already
  2. create the table
  3. insert the test data.
  4. Data Types: the student name is a character field limited to 90 characters
  5. The student ID is a number (integer) (range of -2147483648 to 2147483647). This will be the primary key for the table and will auto increment when a record is added.
  6. There will also be two “time-stamp” fields to play with as well

Create statement and display of results from execution.

创建语句并显示执行结果。

Using a Select statement we’ll see that the table is there but now records have been added.

使用Select语句,我们将看到该表已存在,但现在已添加了记录。

Now to insert some data and see what our new table looks like with records in it (and understand create and update timestamps);

现在插入一些数据,并查看带有记录的新表的外观(并了解创建和更新时间戳);

The first time stamp is the creation data and time and the 2nd is the update date and time. Changing a record should update ts2 but not ts1. Let’s take a look.

第一个时间戳是创建数据和时间,第二个时间戳是更新日期和时间。 更改记录应更新ts2,但不能更新ts1。 让我们来看看。

(Create a table with the MySql Workbench)

Right click on the “Tables” under the schema you want the new file placed in. Select Create Table. Then, complete the form as desired and click Apply

右键单击要在其中放置新文件的模式下的“表”。选择“创建表”。 然后,根据需要填写表格,然后单击“应用”。

(Create Table as Select (CTAS))

A quick way to create a copy of a table, including data is to create table as select.

创建包含数据的表副本的快速方法是将表创建为select。

CREATE TABLE my table as (SELECT * FROM orig tbl);

将表创建为(SELECT * FROM orig tbl);

(Create and populate a table by importing a CSV file)

Right click on the “Tables” under the schema you want the new file placed in. Select Table Data Import.

右键单击要放置新文件的模式下的“表”。选择表数据导入。

Select the CSV file to import and click NEXT Usually you create a new table from the data, select the options desired and click NEXT

选择要导入的CSV文件,然后单击“下一步”。通常,您将根据数据创建一个新表,选择所需的选项,然后单击“下一步”。

Adjust the data types as needed and click NEXT.

根据需要调整数据类型,然后单击“下一步”。

Click NEXT (on this screen and the next one that is displayed) to import the data into the table You’ll see completion status, review and click FINISH

单击“下一步”(在此屏幕上,然后显示下一个),将数据导入表中。您将看到完成状态,查看并单击“完成”。

(Other Material)

There lots more detail to cover this topic so install MySQL and have fun!

有更多细节可以涵盖此主题,因此请安装MySQL并玩得开心!

翻译自: https://www.freecodecamp.org/news/sql-create-table-explained-with-mysql-and-postgres-examples/

postgres语法