Home Database Mysql Tutorial 跟我学SQL_MySQL

跟我学SQL_MySQL

Jun 01, 2016 pm 02:05 PM
sql where Can data database Inquire statement gather

且不说你是否正在从事编程方面的工作或者不打算学习SQL,可事实上几乎每一位开发者最终都会遭遇它。你多半还用不着负责创建和维持某个数据库,但你怎么着也该知道以下的一些有关的SQL知识。
我为那些感兴趣的开发者或者能从数据库操作中得益的读者撰写了这篇关于基本SQL语法的概述性文章。本文主要讨论基本的数据操作查询,后续的文章还会继续讨论如何修改数据库自身以及更高级的查询概念。

SQL数据库是怎么回事?

SQL(结构化查询语言)就是负责与ANSI维护的数据库交互的标准。最新的版本是SQL-99,还有一个新标准SQL-200n尚处于制定过程中。大多数的数据库都至少遵守ANSI-92标准的部分子集。不过,目前对最新标准的有效性还存在一些争论。专有数据库制造商根据这些标准开发自己的产品,同时制定出自己特有的数据库存储操作新概念。几乎各种不同的数据库都包含了自己特有的语法集合,只是通常很类似ANSI标准。在大多数情况下,尽管有一些数据库实例基于特定的扩展语法会因数据库的不同而产生不同的结果,但总的说来,这些新加的语法不过是对原有标准的扩充。如果数据库操作并没有得到你希望的结果,那么你不妨事先读一读数据库制造商提供的产品说明。

假如到目前为止你头回遭遇SQL语言,那么你怎么也得先理解一些基本的SQL概念。我尽量把这些基本知识阐述得简明扼要,如果你对那些数据库术语还能忍受,你尽可跳到下一节,此外你还可以把自己的问题提交给以下的讨论区。

笼统地说,“SQL数据库”其实就是关系型数据库管理系统(RDMS)通俗的叫法。对某些系统来说,“数据库”也指一组数据表、数据以及相互区分但结构类似的配置信息。在这种情况下,每一SQL数据库的安装都可能由若干数据库组成。在有些系统上,这种数据库则指的是表空间。

数据表是一种包含多行数据的数据库构造,这种数据库构造由命名的列组成。通常数据表构造为包含关系信息,同一数据库或表空间以内可以创建若干数据表。

表内的列保存某一种类型的数据而且应根据其保存数据的内容得以命名。例如,被称为“LastName”的列就应该在每一行包含姓氏条目。正是这一前提的存在才能让关系数据库查询返回一致的结果。

字段(field)指的是某一行某一列对应的数据(或保存数据的地方)。另外,数据集合(data set)则指的是多行多列的数据,而且数据集合通常说明你的数据库或数据表内的全部数据。结果集合(result set)就是从数据库查询返回的数据;它能够描述从单一字段到数据库内全部数据这一范围内的全部信息。

数据库查询就是发送给数据库的SQL指令,这些指令向数据库请求某种施加在数据集合或数据库上的功能。

现在我们就来看看基本的数据库查询,这些查询主要涉及到对数据库内数据的操作。在本文中,所有的例子都采用了标准SQL语言,而且这些基本功能可以转换为应用在几乎各种环境下。

数据查询类型

SQL语言中的数据查询分为4种基本类型:

SELECT:这条语句要求数据库返回指定结果的数据集合;你可以用这一语句检索数据库中保存的信息。
INSERT:这条语句用来给数据表增加新一行数据。
DELETE:该语句从你的数据库中删除若干行数据。
UPDATE:该语句修改数据库内的现有数据。
以上的这些语句都有各种各样的限定词和函数供你用来定义有关的数据集合,同时控制查询返回的结果集合。SELECT语句的选项最多。有许多种组合SELECT的查询选项,例如JOIN和UNION等。不过就我们目前来说,本文主要还是关注基本用途。

用SELECT语句检索保存的信息
为了获得数据库中保存的信息就必须采用SELECT语句。其基本功能限制在针对单一数据表操作,当然,其他范围的构造也是有的。为了返回特定列所对应的所有数据行,你可以使用以下语句:

SELECT column1, column2 FROM table_name;

另外,使用通配符“*”可以从表中选出所有的列:

SELECT * FROM table_name;

你要愿意自己编码分析以上返回的结果当然也没问题,不过你完全可以采用方便的WHERE子句限制返回的结果集合,该子句可以让你为选择数据定义某些条件。以下查询就会返回“column1”数值等于3的所以数据行:
SELECT * FROM table_name WHERE column1 = 3;

除了“=”(等于)条件之外你还可以用到下列条件运算符:

表A

= 等于

不等于

> 大于


>= 大于或等于



SQL 条件语句

另外,你还可以联合WHERE语句使用BETWEEN、LIKE等比较运算符以及AND和OR这类逻辑运算符。注意,OR语句是包含性的的。以下有一个例子组合了以上这些概念:

SELECT * FROM table_name WHERE ((Age
用自然语言来说,这条选择语句的含义是这样的:从数据表中选出年龄小于18岁而且姓氏在“Anderson”和“MIller”之间的或者其公司名称类中有“School”字样的数据行。

用INSERT语句加入新数据
使用INSERT语句可以创建新的数据行。如果你希望在某一行的某个字段中赋值则要用到UPDATE语句。

插入语句的语法如下:
INSERT INTO table_name (column1, column2, column3)
VALUES (‘data1’, ‘data2’, ‘data3’);

如果你想按照表内现有列的同一顺序插入所有的值,那么你不必指定列名,当然,从可读性考虑最好不要这样做。另外,如果你列出列名则不必要按照它们在数据库中出现的顺序包括它们,只要你列出的值与它们一一对应即可。有些列你并没有为其输入新的信息所以你自然没有必要列出它们来。

一旦数据库中有了数据要修改起来也与此很相似。

UPDATE语句和WHERE子句
UPDATE用来修改现有的值或行里的空字段,因此它必须在匹配现有的数据集合同时提供可接受的值。除非你真地想要修改所有数据行上的值,否则你必须使用WHERE子句。
UPDATE table_name SET column1 = ‘data1’, column2 = ‘data2’
WHERE column3 = ‘data3’;

你可以采用WHERE子句随意匹配任何一列,正在修改的一列都可以。这样会有助于你把某一特定的值修改为另一个值:

UPDATE table_name SET FirstName = ‘Shelley’
WHERE FirstName = ‘Shelly’ AND LastName = ‘Doll’;


小心DELETE语句
DELETE语句会从数据库的数据表中删除整行。如果你仅仅想删除单一的字段则应该使用UPDATE语句把它修改为代表应用程序中的NULL的其他空值。一定要小心使用带WHERE子句的DELETE语句,否则你可能会遭遇清空全部数据表的风险。
DELETE FROM table_name WHERE column1 = ‘data1’;

一旦你数据库中删除某一行数据就不可再后悔了,因此一般来说,最好在数据表中包括一名为“IsActive”的列或类似的指示信息,这样你就可以把该列数据设置为零表示数据禁用。只有在你确信不再需要受到影响的信息之后你才可以用DELETE语句。

小结
SQL就是数据库的语言,我们已经了解了数据查询中所采用的最基本命令语句。但还有很多基本概念尚未涉及,例如SUN和COUNT函数等,但以上列出的这些命令应该足够你开始着手数据库操作了。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Slow Cellular Data Internet Speeds on iPhone: Fixes Slow Cellular Data Internet Speeds on iPhone: Fixes May 03, 2024 pm 09:01 PM

Facing lag, slow mobile data connection on iPhone? Typically, the strength of cellular internet on your phone depends on several factors such as region, cellular network type, roaming type, etc. There are some things you can do to get a faster, more reliable cellular Internet connection. Fix 1 – Force Restart iPhone Sometimes, force restarting your device just resets a lot of things, including the cellular connection. Step 1 – Just press the volume up key once and release. Next, press the Volume Down key and release it again. Step 2 – The next part of the process is to hold the button on the right side. Let the iPhone finish restarting. Enable cellular data and check network speed. Check again Fix 2 – Change data mode While 5G offers better network speeds, it works better when the signal is weaker

The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. The U.S. Air Force showcases its first AI fighter jet with high profile! The minister personally conducted the test drive without interfering during the whole process, and 100,000 lines of code were tested for 21 times. May 07, 2024 pm 05:00 PM

Recently, the military circle has been overwhelmed by the news: US military fighter jets can now complete fully automatic air combat using AI. Yes, just recently, the US military’s AI fighter jet was made public for the first time and the mystery was unveiled. The full name of this fighter is the Variable Stability Simulator Test Aircraft (VISTA). It was personally flown by the Secretary of the US Air Force to simulate a one-on-one air battle. On May 2, U.S. Air Force Secretary Frank Kendall took off in an X-62AVISTA at Edwards Air Force Base. Note that during the one-hour flight, all flight actions were completed autonomously by AI! Kendall said - "For the past few decades, we have been thinking about the unlimited potential of autonomous air-to-air combat, but it has always seemed out of reach." However now,

Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! Tesla robots work in factories, Musk: The degree of freedom of hands will reach 22 this year! May 06, 2024 pm 04:13 PM

The latest video of Tesla's robot Optimus is released, and it can already work in the factory. At normal speed, it sorts batteries (Tesla's 4680 batteries) like this: The official also released what it looks like at 20x speed - on a small "workstation", picking and picking and picking: This time it is released One of the highlights of the video is that Optimus completes this work in the factory, completely autonomously, without human intervention throughout the process. And from the perspective of Optimus, it can also pick up and place the crooked battery, focusing on automatic error correction: Regarding Optimus's hand, NVIDIA scientist Jim Fan gave a high evaluation: Optimus's hand is the world's five-fingered robot. One of the most dexterous. Its hands are not only tactile

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

58 lines of code scale Llama 3 to 1 million contexts, any fine-tuned version is applicable 58 lines of code scale Llama 3 to 1 million contexts, any fine-tuned version is applicable May 06, 2024 pm 06:10 PM

Llama3, the majestic king of open source, the original context window is only... 8k, which makes me swallow back the words "it smells so good". Today, when 32k is the starting point and 100k is common, is this intentional to leave room for contributions to the open source community? The open source community certainly didn't miss this opportunity: now with just 58 lines of code, any fine-tuned version of Llama370b can automatically scale to 1048k (one million) contexts. Behind the scenes is a LoRA, extracted from a fine-tuned version of Llama370BInstruct that extends good context, and the file is only 800mb. Next, using Mergekit, you can run it with other models of the same architecture or merge it directly into the model. 1048k context used

See all articles