首页 > 数据库 >SQL

SQL

时间:2023-11-10 12:24:23浏览次数:33  
标签:Customers name column SQL table WHERE SELECT

SQL

Select

SELECT column1, column2, ...
FROM table_name;

Select Distinct

select unique uint

SELECT DISTINCT column1, column2, ...
FROM table_name;
SELECT DISTINCT Country FROM Customers;

Where

SELECT column1, column2, ...
FROM table_name
WHERE condition;
SELECT * FROM Customers
WHERE Country='Mexico';

SELECT * FROM Customers
WHERE CustomerID=1;

Operators

Operator Description
= Equal
> Greeter than
< Less than
>= Gretter than or equal
<= Less than or equal
<>(!+) Not equal;Note: In some versions of SQL this operator may be written as !=
Between Between a certain range
LIKE Search for a pattern
In To specify multiple possible values for a column

AND,OR,NOT

SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;

SELECT * FROM Customers
WHERE Country='Germany' AND City='Berlin';
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;

SELECT * FROM Customers
WHERE City='Berlin' OR City='München';
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;

SELECT * FROM Customers
WHERE NOT Country='Germany';
SELECT * FROM Customers
WHERE Country='Germany' AND (City='Berlin' OR City='München');

Order By

SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC(顺序)|DESC(反序);
SELECT * FROM Customers
ORDER BY Country;

SELECT * FROM Customers
ORDER BY Country DESC;
orders by Country, but if some rows have the same Country, it orders them by CustomerName:

SELECT * FROM Customers
ORDER BY Country, CustomerName;

Insert Into

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

NULL

SELECT column_names
FROM table_name
WHERE column_name IS NULL;

SELECT column_names
FROM table_name
WHERE column_name IS NOT NULL;

SELECT CustomerName, ContactName, Address
FROM Customers
WHERE Address IS NOT NULL;

Update

The UPDATE statement is used to modify the existing records in a table.

UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;

UPDATE Customers
SET ContactName = 'Alfred Schmidt', City= 'Frankfurt'
WHERE CustomerID = 1;

Delete

The DELETE statement is used to delete existing records in a table.

DELETE FROM table_name WHERE condition;
DELETE FROM Customers WHERE CustomerName='Alfreds Futterkiste';

Delete All Record

Delete from table_name;

SELECT TOP

The SELECT TOP clause is used to specify the number of records to return.

mysql

SELECT column_name(s)
FROM table_name
WHERE condition
LIMIT number;

SELECT TOP 3 * FROM Customers;

Min,Max

The MIN() function returns the smallest value of the selected column.

The MAX() function returns the largest value of the selected column.

select MIN(column_name)
FROM table_name
WHERE condition;

SELECT MAX(column_name)
FROM table_name
WHERE condition;

COUNT,AVG,SUM

The COUNT() function returns the number of rows that matches a specified criterion.

SELECT COUNT(column_name)
FROM table_name
WHERE condition;

The AVG() function returns the average value of a numeric column

SELECT AVG(column_name)
FROM table_name
WHERE condition;

The SUM() function returns the total sum of a numeric column.

SELECT SUM(column_name)
FROM table_name
WHERE condition;

LIKE

SELECT column1, column2, ...
FROM table_name
WHERE columnN LIKE pattern;
Operator Description
a% start with a
%a end with a
%or% have or
_r% str(second)==r
a_% start with "a" and are at least 2 characters in length
a__% start with "a" and are at least 3 characters in length
a%o start with a,end with o
SELECT * FROM Customers
WHERE CustomerName LIKE 'a%';

IN

The IN operator allows you to specify multiple values in a WHERE clause.

The IN operator is a shorthand for multiple OR conditions.

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

SELECT column_name(s)
FROM table_name
WHERE column_name IN (SELECT STATEMENT);

BETWEEN

The BETWEEN operator selects values within a given range. The values can be numbers, text, or dates.

The BETWEEN operator is inclusive: begin and end values are included.

SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Aliases

SQL aliases are used to give a table, or a column in a table, a temporary name.

Aliases are often used to make column names more readable.

An alias only exists for the duration of that query.

An alias is created with the AS keyword.

SELECT column_name AS alias_name
FROM table_name;

SELECT column_name(s)
FROM table_name AS alias_name;

JOIN

A JOIN clause is used to combine rows from two or more tables, based on a related column between them.

INNER JOIN

The INNER JOIN keyword selects records that have matching values in both tables.

SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

Left JOIN

The LEFT JOIN keyword returns all records from the left table (table1), and the matching records from the right table (table2). The result is 0 records from the right side, if there is no match.

Note: The LEFT JOIN keyword returns all records from the left table (Customers), even if there are no matches in the right table (Orders).

SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
LEFT JOIN Orders ON Customers.CustomerID = Orders.CustomerID
ORDER BY Customers.CustomerName;

Right Join

The RIGHT JOIN keyword returns all records from the right table (table2), and the matching records from the left table (table1). The result is 0 records from the left side, if there is no match.

SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name = table2.column_name;

Full join

The FULL OUTER JOIN keyword returns all records when there is a match in left (table1) or right (table2) table records.

Tip: FULL OUTER JOIN and FULL JOIN are the same.

Union

UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
UNION 内部的 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每条 SELECT 语句中的列的顺序必须相同。

SELECT column_name(s) FROM table_name1
UNION (忽略重复)
SELECT column_name(s) FROM table_name2

SELECT column_name(s) FROM table_name1
UNION ALL
SELECT column_name(s) FROM table_name2

SELECT INTO

SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中。

SELECT INTO 语句常用于创建表的备份复件或者用于对记录进行存档。

SELECT column_name(s)
INTO new_table_name [IN externaldatabase]
FROM old_tablename

CREATE DB

CREATE DATABASE database_name

CREATE TABLE

CREATE TABLE 表名称
(
列名称1 数据类型,
列名称2 数据类型,
列名称3 数据类型,
....
)

Constraints

NOT NULL

约束强制列不接受NULL值

UNIQUE

约束唯一标识数据库表的数据

PRIMARY KEY

约束唯一标识数据库表中的每条记录。

FOREIGN KEY

外键约束


CREATE TABLE Orders
(
Id_O int NOT NULL,
OrderNo int NOT NULL,
Id_P int,
PRIMARY KEY (Id_O),
FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)
)

新增约束
ALTER TABLE Orders
ADD FOREIGN KEY (Id_P)
REFERENCES Persons(Id_P)

删除约束
ALTER TABLE Orders
DROP FOREIGN KEY fk_PerOrders

CHECK

约束用于限制列中的值的范围。

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CHECK (Id_P>0)
)

新增
ALTER TABLE Persons
ADD CHECK (Id_P>0)


删除
ALTER TABLE Persons
DROP CHECK chk_Person

DEFAULT

约束默认值

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255) DEFAULT 'Sandnes'
)

更新

ALTER TABLE Persons
ALTER City SET DEFAULT 'SANDNES'

删除

ALTER TABLE Persons
ALTER City DROP DEFAULT

索引

用户无法看到索引,它们只能被用来加速搜索/查询。

注释:更新一个包含索引的表需要比更新一个没有索引的表更多的时间,这是由于索引本身也需要更新。因此,理想的做法是仅仅在常常被搜索的列(以及表)上面创建索引。

CREATE INDEX index_name
ON table_name (column_name (DESC),column_name2,)

删除索引
ALTER TABLE table_name DROP INDEX index_name

DROP

drop 可以用了删除索引,表,库

ALTER TABLE table_name DROP INDEX index_name

DROP TABLE table_name;

DROP DATABASE db_name;

TRUNCATE TABLE table_name; #只清空值

ALTER

拥有向已有的表中添加,修改,删除列

添加列
ALTER TABLE table_name 
ADD column_name datatype;

删除列
ALTER TABLE table_name
drop column_name

修改列数据类型
ALTER TABLE table_name
ALTER COLUMN column_name datatype

AUTO INCREMENT

自增字段,开始值为1

设置起始值
ALTER TABLE table_name AUTO_INCREMENT=100

View

在 SQL 中,视图是基于 SQL 语句的结果集的可视化的表。

视图包含行和列,就像一个真实的表。视图中的字段就是来自一个或多个数据库中的真实的表中的字段。我们可以向视图添加 SQL 函数、WHERE 以及 JOIN 语句,我们也可以提交数据,就像这些来自于某个单一的表。

注释:数据库的设计和结构不会受到视图中的函数、where 或 join 语句的影响。

视图总是显示最近的数据。每当用户查询视图时,数据库引擎通过使用 SQL 语句来重建数据。

新建
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

CREATE VIEW [Device List] AS
SELECT DeviceID,DeviceName
FROM Device


修改视图
CREATE OR REPLACE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

删除视图

DROP VIEW view_name

标签:Customers,name,column,SQL,table,WHERE,SELECT
From: https://www.cnblogs.com/erfeng/p/16894567.html

相关文章

  • docker mysql
    dockerrun-d--namemysql--restart=always--privileged=true\-v/opt/mysql/log:/var/log/mysql\-v/opt/mysql/data:/var/lib/mysql\-v/opt/mysql/conf.d:/etc/mysql/conf.d\-v/etc/localtime:/etc/localtime:ro\-eMYSQL_ROOT_PASSWORD=123456-p33......
  • MySQL 死锁后事务无法回滚是真的吗?
    MySQL作为目前互联网企业使用最多的,或者说在基于成本下,最流行的数据库之一,MySQL在国内使用者众多,那么在MySQL偶然安装后,在使用中出现死锁后,死锁中的事务到底能不能回滚 ?我们来进行相关的实验我们先验证一遍1 我们打开一个MySQL版本为8.027 官方版本2通过下面的操作我们可以......
  • MySQL 内部Server 层机制
    主要包括连接器、查询缓存、分析器、优化器、执行器等,涵盖MySQL的大多数核心服务功能,以及所有的内置函数(如日期、时间、数学和加密函数等),所有跨存储引擎的功能都在这一层实现,比如存储过程、触发器、视图等。1.连接器像navicat、JDBC、MySQL等客户端软件需要先和mysql建立通信之后......
  • 16、Flink 的table api与sql之连接外部系统: 读写外部系统的连接器和格式以及FileSyst
    文章目录Flink系列文章一、Table&SQLConnectors1、概述2、支持的外部连接3、使用示例:kafka4、Transformtableconnector/formatresources5、SchemaMapping6、Metadata7、PrimaryKey8、TimeAttributes9、ProctimeAttributes10、RowtimeAttributes11、完整示例1)、建表2)、......
  • MySQL8
    MySQL8.0:窗口函数一、MySQL8.0窗口函数概述1、什么是窗口函数窗口函数是类似于可以返回聚合函数值的函数,例如SUM(),COUNT(),MAX()。但是窗口函数又与普通的聚合函数不一样,它不会对结果进行分组,使输出中的行数和输入中的行数相同。窗口函数示例:selectsum()over(partitionby_......
  • SQL知识点总结
    1、直接能看到的放最外一层,若感觉一层查询搞不定就再套一层,把复杂的逻辑放内部。  1、更新:updatetable_namesetparam1=A,param2=Bwhere....  set后面的两个参数用逗号连接。2、插入:insertintotable_namevalues...../insertintotable_name1selectparam1,pa......
  • 手把手教你如何扩展(破解)mybatisplus的sql生成 | 京东云技术团队
    mybatisplus的常用CRUD方法众所周知,mybatisplus提供了强大的代码生成能力,他默认生成的常用的CRUD方法(例如插入、更新、删除、查询等)的定义,能够帮助我们节省很多体力劳动。他的BaseMapper中定义了这些常用的CRUD方法,我们在使用时,继承这个BaseMapper类就默认拥有了这些能力。如果我......
  • windows系统上如何给mysql导入数据库和表
    1.连接数据库2.输入密码3.进入数据库4.创建数据库 createdatabase数据库名;5.进入数据库use  数据库名;6.查看当前所在数据库selectdatabase();7.把需要导入的数据库放到没有中文名的路径下面(蜜蜂这里放D盘了),之后使用SOURCE导入SOURCE数据库的位置/需要导入的数据库名称(中间......
  • 【Qt初入江湖】Qt QSqlRelationalTableModel 底层架构、原理详细描述
    鱼弦:内容合伙人、新星导师、全栈领域创作新星创作者、51CTO(Top红人+专家博主)、github开源爱好者(go-zero源码二次开发、游戏后端架构https://github.com/Peakchen) QtQSqlRelationalTableModel是Qt中用于实现具有关联表格的模型类,它继承自QSqlTableModel。QSqlRelationalTable......
  • mysql基本使用
    MySQL常用图形化工具:NavicatSqlyogMysqlworkbend(msi自动安装) //////////////////////////////////////////////////////////Mysql数据库基本操作1、ddl数据定义语言对数据库的常用操作 l 查看所有的数据库:showdatabases;l 创建数据库:createdatabase[i......