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 null
或null
指示它是否为必需的。
有关CREATE TABLE命令SQL指南的更深入的指南 (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 通过导入CSV文件并更改该表来创建表
- Creating a table using the MySQL workbench tool 使用MySQL工作台工具创建表
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)
- Highest level; The Database; the database system installation. In this case, it’s MySQL. Called “Local instance MySQL Router” in the screen shots above. 最高水平; 数据库; 数据库系统安装。 在这种情况下,它是MySQL。 在上面的屏幕截图中称为“本地实例MySQL路由器”。
- Next is a Schema; a container for the objects needed to managed data in a relational database system. 接下来是一个模式; 一个关系数据库系统中管理数据所需的对象的容器。
- Objects we create (tables, indexes, stored procedures, functions) to manage the system and its data 我们创建的对象(表,索引,存储过程,函数)用于管理系统及其数据
创建一个MySQL模式 (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:
步骤将是:
- make sure we don’t have the table already 确保我们还没有桌子
- create the table 创建表
- insert the test data. 插入测试数据。
- Data Types: the student name is a character field limited to 90 characters 数据类型:学生姓名是一个字符字段,最多90个字符
- 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. 学生ID是一个数字(整数)(范围-2147483648至2147483647)。 这将是表的主键,并且在添加记录时将自动递增。
- 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。 让我们来看看。
使用MySql Workbench创建表 (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
右键单击要在其中放置新文件的模式下的“表”。选择“创建表”。 然后,根据需要填写表格,然后单击“应用”。
创建表为选择(CTAS) (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);
通过导入CSV文件创建并填充表格 (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语法