sql語句語法多表關聯_SQL創建表語句-帶有示例語法

sql語句語法多表關聯

SQL is one of the most reliable and straightforward querying languages around. It provides clear cut syntax that reads easily without abstracting away too much of the functionality's meaning.

SQL是最可靠,最直接的查詢語言之一。 它提供了清晰易懂的語法,易于閱讀,而無需抽象過多的功能含義。

If you'd like some history on the language as well as some interesting facts, check out the introduction portion of my SQL Update Statement article. ?

如果您想了解某種語言的歷史以及一些有趣的事實,請查看我的《 SQL更新語句》文章的介紹部分。

In this article, we're going to go through the important parts of creating a table in SQL. ?My preferred "flavor" of SQL is SQL Server but the information about creating a table is fairly ubiquitous across all SQL variations. ?

在本文中,我們將介紹在SQL中創建表的重要部分。 我最喜歡SQL“風味”是SQL Server,但是有關創建表的信息在所有SQL變體中都非常普遍。

If you've never used SQL or don't know what a table is, fear not! Briefly (and broadly), a table is a database object that holds, or contains, all of the data within that portion of the database. It stores this data in named columns and numbered rows which is not unfamiliar if you've ever used any spreadsheet program. Each row represents a whole database record.

如果您從未使用過SQL或不知道表是什么,請不要擔心! 簡要地(廣義上),表是一個數據庫對象,用于保存或包含數據庫該部分內的所有數據。 它將這些數據存儲在命名列和編號行中,如果您曾經使用過任何電子表格程序,這些都不是不熟悉的。 每行代表一個完整的數據庫記錄。

If data were in box form then a table would be a section of the warehouse shelving we store those boxes in.

如果數據是盒式的,那么表格將是我們將這些盒存儲在其中的倉庫貨架的一部分。

I'm simplifying the explanation greatly and there is much more to SQL tables but that's outside the scope of this article. ?If you're itching for a more in-depth explanation on tables, I encourage you to dive into the Microsoft Database Design documentation.

我正在極大地簡化說明,SQL表還有很多其他內容,但這不在本文的討論范圍之內。 如果您想對表進行更深入的說明,建議您深入閱讀Microsoft數據庫設計文檔 。

Before we learn how to create the table, it's important that we learn what types of data these columns and rows can store.

在學習如何創建表之前,重要的是要了解這些列和行可以存儲什么類型的數據。

資料類型 (Data Types)

SQL tables can hold text, numbers, a combination of text and numbers, as well as images and links.

SQL表可以保存文本,數字,文本和數字的組合以及圖像和鏈接。

When creating our table, we designate the type of data its rows and columns will hold. Here are the overarching classifications of data:

在創建表時,我們指定其行和列將保存的數據類型。 以下是數據的總體分類:

  • Approximate Numerics

    近似數值
  • Strings

    弦樂
  • Date & Time

    約會時間
  • Unicode Character Strings

    Unicode字符串
  • Exact Numerics

    精確數值
  • Other

    其他

I'll list some of the more commonly used data types below, but if you'd like a more on all data types, I invite you to check out this exhaustive article on each type from Microsoft.

我將在下面列出一些更常用的數據類型,但是如果您想在所有數據類型上都有更多的信息,我邀請您查看Microsoft每種類型的詳盡文章 。

Here are the more commonly used types of data from my experience, in no particular order:

根據我的經驗,以下是一些最常用的數據類型,不分先后順序:

  • char(size) - fixed length string that can contain letters, numbers, special characters

    char(size)- 固定長度的字符串,可以包含字母,數字和特殊字符

  • varchar(size) - variable length string that can contain letters, numbers, & special characters

    varchar(size)- 可變長度的字符串,可以包含字母,數字和特殊字符

  • boolean - Zero (or values that equate to 0) is false, non-zero is true

    布爾值-零(或等于0的值)為false,非零為true
  • int(size optional) - a number up to 10 characters in length, accepts negative & positive numbers

    int( size可選 )-長度最大為10個字符的數字,可接受負數和正數

  • bigint(size optional) - a number up to 19 characters in length, accepts negative & positive numberrs

    bigint( size可選 )-長度最大為19個字符的數字,可接受負號和正號

  • float(size, d) - a number with total number size represented by size and the number of characters after the decimal represented by the d

    float(size,d)-一個數字,其總大小由size表示,而小數點后的字符數由d表示

  • date - date in the format of YYYY-MM-DD

    date-日期,格式為YYYY-MM-DD

  • datetime - date time in the format of YYY-MM-DD hh:mm:ss

    datetime-日期時間,格式為YYY-MM-DD hh:mm:ss

  • time - time in the format of hh:mm:ss

    時間-時間,格式為hh:mm:ss

Alright, now that we know what types of data the rows and columns can contain let's get into the fun parts!

好了,現在我們知道行和列可以包含哪些數據類型,讓我們進入有趣的部分!

建立表格 (Creating a Table)

Before we start it's important to note that I'll be providing all of my examples independent of any program.

在開始之前,請務必注意,我將提供與所有程序無關的所有示例。

However, if you'd like to start writing queries and you aren't sure where to start, take a look at SQL Server Management Studio. It's a free, robust program that's widely used and supported in the community.

但是,如果您想開始編寫查詢并且不確定從哪里開始,請查看SQL Server Management Studio。 這是一個免費,強大的程序,在社區中得到廣泛使用和支持。

Alternatively, there are several options including DB Fiddle that allow you to build schemas and write queries right in your browser. ?

另外,還有一些選項,包括DB Fiddle ,這些選項使您可以在瀏覽器中構建模式并編寫查詢。

Let's start with a simple statement to create a basic table:

讓我們從創建基本表的簡單語句開始:

CREATE TABLE table_name ( column1_name datatype, column2_name datatype, column3_name datatype, column4_name datatype, column5_name datatype,)CREATE TABLE table_name ( column1_name datatype, column2_name datatype, column3_name datatype, column4_name datatype, column5_name datatype,)

There are other parameters we can add after the datatype to augment the columns:

我們可以在datatype之后添加其他參數以增加列:

  • NOT NULL - passing this parameter will ensure the column cannot hold a NULL value

    NOT NULL傳遞此參數將確保列不能包含NULL

  • UNIQUE - passing this parameter will prevent the column from holding the same value more than once

    UNIQUE傳遞此參數將防止該列多次保存相同的值

  • UNIQUE KEY - passing this parameter will designate that column as a unique identifier. It is essentially a combination of the previous two parameters.

    UNIQUE KEY傳遞此參數將將該列指定為唯一標識符。 它本質上是前兩個參數的組合。

Now, we're going to create a table (named doggo_info which must adhere to the identifier standards for databases) to hold information on the residents of Woof Woof Retreat, a fictional doggy daycare I just thought of :)

現在,我們將創建一個表(名為doggo_info,該表必須遵守數據庫的標識符標準 ),以保存有關Woof Woof Retreat居民的信息,Woof Woof Retreat是我剛剛想到的虛構的小狗日托:)

CREATE TABLE doggo_info ( ID int UNIQUE KEY, Name varchar(50) NOT NULL, Color varchar(50), Breed varchar(50), Age int, Weight int, Height int, Fav_Food varchar(100), Fav_Toy varchar(100), Dislikes varchar(500), Allergies varchar(500) NOT NULL ) CREATE TABLE doggo_info ( ID int UNIQUE KEY, Name varchar(50) NOT NULL, Color varchar(50), Breed varchar(50), Age int, Weight int, Height int, Fav_Food varchar(100), Fav_Toy varchar(100), Dislikes varchar(500), Allergies varchar(500) NOT NULL )

And here is the brand new table we just created:

這是我們剛剛創建的全新表:

NameColorBreedAgeWeightHeightFav_FoodFav_ToyDislikesAllergies
名稱 顏色 品種 年齡 重量 高度 最喜歡的食物 收藏玩具 不喜歡 過敏癥

You'll notice that our table is completely empty and this is because we haven't added any data to it yet. Doing so is beyond the scope of this article but I wanted you to be aware of that tidbit.

您會注意到我們的表完全為空,這是因為我們尚未向其添加任何數據。 這樣做不在本文的討論范圍之內,但是我希望您意識到這一點。

從現有表創建表 (Create A Table From An Existing Table)

It is also possible to create a new table based off of an existing table.

也可以基于現有表創建新表。

It's pretty easy and doesn't require that much more syntax. We need to select the table and columns to "copy" from:

這非常簡單,不需要太多語法。 我們需要從中選擇要“復制”的表和列:

CREATE TABLE new_table_name AS SELECT column1, column2, column3, column4 (use * to select all columns to be added to the new_table) FROM current_table_name WHERE conditions_existCREATE TABLE new_table_name AS SELECT column1, column2, column3, column4 (use * to select all columns to be added to the new_table) FROM current_table_name WHERE conditions_exist

So, expediency's sake, I've added some data to our doggo_info table and it now looks like the example below:

因此,為了方便起見,我已經在我們的doggo_info表中添加了一些數據,現在看起來像下面的示例:

NameColorBreedAgeWeightHeightFav_FoodFav_ToyDislikesAllergies
daisyredstandard dachshund1146salmon flavored kibblesqueeky ballbirds flying over the yardcats, baths, cleanliness
chiefblack/tanrottweiler34117literally anythingrope tugstaying off the couchlistening, behaving, not slobbering on everything
sammielight honeygolden retriever94619beef flavored kibbleher bedrambutcious puppiesnone known
名稱 顏色 品種 年齡 重量 高度 最喜歡的食物 收藏玩具 不喜歡 過敏癥
雛菊 標準臘腸犬 1個 14 6 鮭魚粗磨 粘球 鳥兒飛過院子 貓,浴室,清潔
首席 黑色/棕褐色 羅威納犬 3 41 17 幾乎任何東西 繩拖船 不在沙發上 聆聽,表現,不流連忘返
薩米 淡蜂蜜 金毛尋回犬 9 46 19 牛肉粗磨 她的床 貪婪的小狗 沒有人知道

Now we can create another table based off of the data we have in our doggo_info table by running the query below:

現在,我們可以通過運行以下查詢,基于doggo_info表中的數據創建另一個表:

CREATE TABLE puppies_only AS SELECT * FROM doggo_info WHERE Age < 4 CREATE TABLE puppies_only AS SELECT * FROM doggo_info WHERE Age < 4

We want to create a new table with all of the columns from the doggo_info table but only where the Age is less than 4. After running this query, our new table will look like this:

我們要創建一個新表,其中包含doggo_info表中的所有列,但僅Age小于4。運行此查詢后,新表將如下所示:

NameColorBreedAgeWeightHeightFav_FoodFav_ToyDislikesAllergies
daisyredstandard dachshund1146salmon flavored kibblesqueeky ballbirds flying over the yardcats, baths, cleanliness
chiefblack/tanrottweiler34117literally anythingrope tugstaying off the couchlistening, behaving, not slobbering on everything
名稱 顏色 品種 年齡 重量 高度 最喜歡的食物 收藏玩具 不喜歡 過敏癥
雛菊 標準臘腸犬 1個 14 6 鮭魚粗磨 粘球 鳥兒飛過院子 貓,浴室,清潔
首席 黑色/棕褐色 羅威納犬 3 41 17 幾乎任何東西 繩拖船 不在沙發上 傾聽,表現,不流連忘返

I hope you can see just how powerful this statement can be. ?With a few lines in our query we have essentially copied data from one table into another but only the rows that we wanted. ?

希望您能看到此語句的功能。 在查詢中只有幾行,我們基本上已將數據從一個表復制到另一個表中,但僅復制了所需的行。

This is not only a handy tool to have in your developer tool belt – it'll save you untold amounts of time when you need to move data around tables.

這不僅是開發人員工具帶中的便捷工具,而且還可以在需要在表中移動數據時為您節省大量時間。

結語 (Wrapping Up)

Now that you know how to create (or copy) a table in SQL no matter what situation you're presented with, you can start filling the columns and rows with data to store!

既然您知道了如何在SQL中創建(或復制)表,無論遇到什么情況,都可以開始用要存儲的數據填充列和行!

The CREATE TABLE statement is extremely useful and powerful. You're ready to start putting it to good use.

CREATE TABLE語句非常有用且功能強大。 您已經準備好開始充分利用它。

If you found this article helpful check out my blog where I frequently post articles about web development, life, and learning.

如果您覺得這篇文章對您有幫助,請訪問我的博客 ,我經常在其中發布有關Web開發,生活和學習的文章。

While you're there why not sign up for my newsletter? You can do that at the top right of the main blog page. I like to send out interesting articles (mine and others), resources, and tools for ?developers every now and then.

當您在那里時,為什么不注冊我的時事通訊? 您可以在博客主頁面的右上角進行操作。 我喜歡不時為開發人員發送有趣的文章(我的和其他文章),資源和工具。

If you have questions about this article or just in general let me know – come say hi on Twitter or any of my other social media accounts which you can find below the ?newsletter sign up on the main page of my blog or on my profile here at fCC :)

如果您對本文有疑問,或者只是一般而言,請告訴我–在Twitter或我的任何其他社交媒體帳戶上打個招呼,您可以在新聞快訊下方找到該郵件,并在我的博客主頁或此處的個人資料上進行注冊: fCC :)

Have an awesome day! Happy learning and happy coding, friend!

祝你有美好的一天! 祝您學習愉快,編碼愉快,朋友!

翻譯自: https://www.freecodecamp.org/news/sql-create-table-statement-with-example-syntax/

sql語句語法多表關聯

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390185.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390185.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390185.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

分布式改造劇集三:Ehcache分布式改造

第三集&#xff1a;分布式Ehcache緩存改造 前言 ? 好久沒有寫博客了&#xff0c;大有半途而廢的趨勢。忙不是借口&#xff0c;這個好習慣還是要繼續堅持。前面我承諾的第一期的DIY分布式&#xff0c;是時候上終篇了---DIY分布式緩存。 探索之路 ? 在前面的文章中&#xff0c;…

85. 最大矩形

85. 最大矩形 給定一個僅包含 0 和 1 、大小為 rows x cols 的二維二進制矩陣&#xff0c;找出只包含 1 的最大矩形&#xff0c;并返回其面積。 示例 1&#xff1a; 輸入&#xff1a;matrix [[“1”,“0”,“1”,“0”,“0”],[“1”,“0”,“1”,“1”,“1”],[“1”,“1”…

TP單字母函數

A方法 A方法用于在內部實例化控制器 調用格式&#xff1a;A(‘[項目://][分組/]模塊’,’控制器層名稱’) 最簡單的用法&#xff1a; $User A(User); 表示實例化當前項目的UserAction控制器&#xff08;這個控制器對應的文件位于Lib/Action/UserAction.class.php&#xff09;…

Angular問題03 @angular/material版本問題

1 問題描述 應用使用 angular4在使用angular/material時&#xff0c;若果在導入模塊時使用mat開頭&#xff0c;就會報錯。 2 問題原因 angular/material版本出現問題&#xff0c;angular/material 從版本5開始就必須要angular5的核心依賴&#xff1b;想要在angular5之前版本中的…

onclick判斷組件調用_從子組件Onclick更新狀態

onclick判斷組件調用How to update the state of a parent component from a child component is one of the most commonly asked React questions.如何從子組件更新父組件的狀態是最常見的React問題之一。 Imagine youre trying to write a simple recipe box application, …

Python 列表List的定義及操作

# 列表概念&#xff1a;有序的可變的元素集合# 定義 # 直接定義 nums [1,2,3,4,5]# 通過range函數構造&#xff0c;python2 和python3 版本之間的差異&#xff1b; # python3 用的時候才會去構造 nums range(1,101)# 列表嵌套 # 注意和C語言中數組的區別,是否可…

遞歸分解因數

題目總時間限制: 1000ms 內存限制: 65536kB描述給出一個正整數a&#xff0c;要求分解成若干個正整數的乘積&#xff0c;即a a1 * a2 * a3 * ... * an&#xff0c;并且1 < a1 < a2 < a3 < ... < an&#xff0c;問這樣的分解的種數有多少。注意到a a也是一種分解…

劍指 Offer 51. 數組中的逆序對

劍指 Offer 51. 數組中的逆序對 在數組中的兩個數字&#xff0c;如果前面一個數字大于后面的數字&#xff0c;則這兩個數字組成一個逆序對。輸入一個數組&#xff0c;求出這個數組中的逆序對的總數。 示例 1: 輸入: [7,5,6,4] 輸出: 5 限制&#xff1a; 0 < 數組長度 &…

react 圖像識別_無法在React中基于URL查找圖像

react 圖像識別If youre new to React and are having trouble accessing images stored locally, youre not alone.如果您不熟悉React&#xff0c;并且無法訪問本地存儲的圖像&#xff0c;那么您并不孤單。 Imagine you have your images stored in a directory next to a co…

html單行元素居中顯示,多行元素居左顯示

有很多的業務需要元素或者文字如果單行&#xff0c;居中顯示&#xff0c;如果數據增多&#xff0c;居中顯示代碼&#xff08;直接復制到編輯器可用&#xff09;&#xff1a;<!DOCTYPE html> <html lang"en"> <head> <meta charset"UTF-8&q…

ML.NET 0.2版增加了集群和新示例

在今年的Build大會上&#xff0c;微軟首次發布了ML.NET。ML.NET是開源的、跨平臺的以及運行在.NET上的機器學習框架。微軟的Ankit Asthana宣布該項目已經完成了第二版的開發。第二版增加了幾個新功能&#xff0c;包括名為集群的新機器學習任務&#xff0c;交叉驗證和訓練-測試&…

如何變得井井有條-來之不易的秘訣來組織您的生活

Because of the changes brought about by COVID-19, many people have had to find healthy and productive ways of working remotely. 由于COVID-19帶來的變化&#xff0c;許多人不得不尋找健康有效的遠程工作方式。 Some have been sent home and can continue doing thei…

被未知進程占用端口的解決辦法

echo off echo 這是用來結束一個未知進程占用端口的批處理可執行文件ipconfig /allnetstat -anoecho 請查看以上信息&#xff0c;輸入被占用的端口號:set /p port請輸入port:tasklist|findstr %port%echo 請結合上述程序進行輸入&#xff0c;請**謹慎輸入**set /p program請輸入…

怎樣在減少數據中心成本的同時不犧牲性能?

2019獨角獸企業重金招聘Python工程師標準>>> 導讀雖然組織對數據中心提出了更高的要求&#xff0c;但IT管理人員確實有辦法在嚴格的預算內展開工作。如今&#xff0c;組織認為即使性能預期不斷提高&#xff0c;其數據中心預算也在縮減。盡管2018年IT支出總體預計增長…

賽普拉斯 12864_如何使用賽普拉斯自動化輔助功能測試

賽普拉斯 12864In my previous post, I covered how to add screenshot testing in Cypress to ensure components dont unintentionally change over time. 在上一篇文章中 &#xff0c;我介紹了如何在賽普拉斯中添加屏幕截圖測試&#xff0c;以確保組件不會隨時間變化。 Now…

anaconda在win下和在mac下的安裝區別

1. 在win下安裝anaconda后會提示你選擇環境變量&#xff0c;但是建議使用默認。 于是CMD進入終端和使用navigator進入終端不一樣&#xff0c;前者會提示無此命令&#xff0c;只能通過navigator進入終端 即使在系統變量變量Path里添加了路徑&#xff0c;使用CMD還是不能使用pyth…

fcn從頭開始_如何使用Go從頭開始構建區塊鏈

fcn從頭開始介紹 (Introduction) With Web 3.0 and blockchain becoming more mainstream every day, do you know what blockchain is? Do you know its technical advantages and use-cases?隨著Web 3.0和區塊鏈每天變得越來越主流&#xff0c;您知道什么是區塊鏈嗎&#x…

java實現無序數組結構

一、數組的2種定義方式 數據類型 [] 數組名稱 new 數據類型[數組長度]; 這里 [] 可以放在數組名稱的前面&#xff0c;也可以放在數組名稱的后面&#xff0c;一般放在名稱的前面 數據類型 [] 數組名稱 {數組元素1&#xff0c;數組元素2&#xff0c;......} 這種方式聲明數組的…

Android App 的主角:Activity

Android App 程序主要由4種類型組成&#xff1a; 1.Activity&#xff08;活動&#xff09;&#xff1a;主要負責屏幕顯示畫面&#xff0c;并處理與用戶的互動。每個Android App至少都會有一個Activity&#xff0c;在程序一啟動時顯示主畫面供用戶操作。 2.Service&#xff08;后…

通過構建Paint App學習React Hooks

According to people in the know, React Hooks are hot, hot, hot. In this article, we follow Christian Jensens 14-part tutorial to find out about the basics of this new feature of React. Follow along to find out more! 據知情人士稱&#xff0c;React Hooks很熱&…