postgres语法_SQL Create Table解释了MySQL和Postgres的语法示例

news/2024/7/1 4:12:53

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指示它是否为必需的。

有关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)

  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.

    最高水平; 数据库; 数据库系统安装。 在这种情况下,它是MySQL。 在上面的屏幕截图中称为“本地实例MySQL路由器”。
  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

    我们创建的对象(表,索引,存储过程,函数)用于管理系统及其数据

创建一个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:

步骤将是:

  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

    数据类型:学生姓名是一个字符字段,最多90个字符
  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.

    学生ID是一个数字(整数)(范围-2147483648至2147483647)。 这将是表的主键,并且在添加记录时将自动递增。
  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。 让我们来看看。

使用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语法


http://lihuaxi.xjx100.cn/news/237688.html

相关文章

json的简单的数据格式

json的简单数据格式 var arr{"obj1":["张三","12","女"],"obj2":["李四","12","女"],"obj3":["王五","12","女"],} var str""; $.each(a…

智能合约语言 Solidity 以太单位及时间单位

链客,专为开发者而生,有问必答! 此文章来自https://www.liankexing.com,未经允许拒绝转载。 Solidity是以太坊智能合约编程语言,当然你在阅读这篇文章之前,你应该对以太坊、智能合约有初步或者深入的了解&…

Android逆向--如何调试smali代码?

最近在重整Android逆向分析环境,一切都在从零开始,做下记录,给大家分享。 本文介绍Android逆向中smali代码的调试及环境的准备。 事先准备如下工具: Android killer:反编译APK应用为smali源码的工具 Android studi…

svg: svg预定义的形状

SVG 有一些预定义的形状元素&#xff0c;可被开发者使用和操作&#xff1a;矩形 <rect>圆形 <circle>椭圆 <ellipse>线 <line>折线 <polyline>多边形 <polygon>路径 <path> 矩形 <rect x"20" y"20" width&qu…

grafana美人鱼_编码美人鱼–我如何从海洋生物学家转到前端开发人员

grafana美人鱼I have wanted to share my story for a while, but I didn’t know exactly how to start, or even what name to give it. 我想分享我的故事一段时间&#xff0c;但我不知道确切的开头&#xff0c;甚至不知道用什么名字。 But recently I was talking with som…

Android拍照得到全尺寸图片并进行压缩/拍照或者图库选择 压缩后 图片 上传

http://www.jb51.net/article/77223.htm https://www.cnblogs.com/breeze1988/p/4019510.html

记一次知乎维权过程——严肃批评某非法引流商

“ 我的文章被人盗版了。” 首先请关注本号的云技术君的马甲赶快取关&#xff0c;这里不欢迎你&#xff0c;你如果再抄我的文章去干坏事&#xff0c;你全家得新冠肺炎。 文章被盗版&#xff0c;被洗稿&#xff0c;且用来给菠菜党引流&#xff0c;我很生气。 好了&#xff0c…

AI让你看片更丝滑

欢迎大家前往腾讯云社区&#xff0c;获取更多腾讯海量技术实践干货哦~ 本文由腾讯音视频实验室发表于云社区专栏 在线“看片”时&#xff0c;我们经常会遇到这些事情&#xff1a;视频画面突然卡住进入缓冲状态或者视频画面突然变得模糊而不忍直视。这些事情的背后很可能是网络环…