语句分类

SQL 命令一般分为三类:DQL、DML、DDL。

一、DDL语句。

1.1建表语句

CREATE TABLE table_name(

col01_name data_type,

col02_name data_type,

);

实例:

  1. student_name varchar(40),
  2. chinese_score int,
  3. math_score int,
  4. test_date date
  5. );
  6. CREATE TABLE

\d  显示表

  1. postgres=# \d score
  2. Table "public.score"
  3. Column | Type | Modifiers
  4. ---------------+-----------------------+-----------
  5. student_name | character varying(40) |
  6. chinese_score | integer |
  7. math_score | integer |
  8. test_date | date | 

主键

创建表的时候可以指定主键primary key,标识这一列唯一,不能重复。

  1. postgres=# create table student(
  2. postgres(# no int primary key,
  3. postgres(# student_name varchar(40),
  4. postgres(# age int);
  5. CREATE TABLE

 1.2 删除表

drop table table_name;

二、DML语句。

DML用于插入、更新和删除数据。insert、update、delete。

2.1  插入语句:

  1. postgres=# insert into student(no,age,student_name) values(2,13,'steven');
  2. INSERT 0 1

2.2 更新语句:

  1. postgres=# update student set age = 15;
  2. UPDATE 1

  使用where过滤:

  1. postgres=# update student set age=14 where no = 3;
  2. UPDATE 1

  2.3 删除语句:

  1. postgres=# delete from student where no = 3;
  2. DELETE 1

  2.4.1 查询语句:

  1. postgres=# select no,student_name,age from student;
  2. no | student_name | age
  3. ----+--------------+-----
  4. 2 | haha | 15
  5. 1 | steven | 13
  6. (2 rows)

  2.4.2 过滤查询:

  1. postgres=# select * from student where age > 14;
  2. no | student_name | age
  3. ----+--------------+-----
  4. 2 | haha | 15
  5. (1 row)

  2.4.3排序

  1. postgres=# select * from student order by age;
  2. no | student_name | age
  3. ----+--------------+-----
  4. 1 | steven | 13
  5. 2 | haha | 15
  6. (2 rows)

  2.4.4倒序

  1. postgres=# select * from student order by no desc;
  2. no | student_name | age
  3. ----+--------------+-----
  4. 2 | haha | 15
  5. 1 | steven | 13
  6. (2 rows)

  2.4.5分组

  1. postgres=# select age,count(*) from student group by age;
  2. age | count
  3. -----+-------
  4. 15 | 1
  5. 13 | 1
  6. (2 rows)

  2.4.6多表联查

  1. postgres=# create table class(no int primary key,
  2. class_name varchar(40));
  3. CREATE TABLE
  4. postgres=# insert into class values(1,'初二(1)班');
  5. INSERT 0 1
  6. postgres=# insert into class values(2,'初二(2)班');
  7. INSERT 0 1
  8. postgres=# insert into class values(3,'初二(3)班');
  9. INSERT 0 1
  10. postgres=# insert into class values(4,'初二(4)班');
  11. INSERT 0 1
  12. postgres=# select * from class;
  13. no | class_name
  14. ----+-------------
  15. 1 | 初二(1)班
  16. 2 | 初二(2)班
  17. 3 | 初二(3)班
  18. 4 | 初二(4)班
  19. (4 rows)

  

  1. postgres=# create table student2(no int primary key,
  2. student_name varchar(40),
  3. age int,
  4. class_no int);
  5. CREATE TABLE
  6.  
  7. postgres=# insert into student2 values(1,'a',12,1);
  8. INSERT 0 1
  9. postgres=# insert into student2 values(2,'b',13,1);
  10. INSERT 0 1
  11. postgres=# insert into student2 values(3,'c',15,2);
  12. INSERT 0 1
  13. postgres=# insert into student2 values(4,'d',15,2);
  14. INSERT 0 1
  15. postgres=# insert into student2 values(5,'e',15,3);
  16. INSERT 0 1
  17. postgres=# insert into student2 values(6,'f',15,2);
  18. INSERT 0 1
  19. postgres=#

  

  1. postgres=# select student_name,class_name from student2,class
  2. where student2.class_no = class.no;
  3. student_name | class_name
  4. --------------+-------------
  5. a | 初二(1)班
  6. b | 初二(1)班
  7. c | 初二(2)班
  8. d | 初二(2)班
  9. e | 初二(3)班
  10. f | 初二(2)班
  11. (6 rows)

  

三、其它SQL语句

insert into ....select语句,可以把一个表插入到另外一张表中。

  1. postgres=# create table student_bak(no int primary key,
  2. postgres(# sutdent_name varchar(40),
  3. postgres(# age int,
  4. postgres(# class_no int);
  5. CREATE TABLE
  6. postgres=# insert into student_bak select * from student;
  7. INSERT 0 2
  8. postgres=# select * from student_bak;
  9. no | sutdent_name | age | class_no
  10. ----+--------------+-----+----------
  11. 2 | haha | 15 |
  12. 1 | steven | 13 |
  13. (2 rows)

  

union语句将两个表查询的数据整合在一起

  1. postgres=# select * from student2 where no = 1 union select * from student_bak
  2. where no =2;
  3. no | student_name | age | class_no
  4. ----+--------------+-----+----------
  5. 1 | a | 12 | 1
  6. 2 | haha | 15 |
  7. (2 rows)

  truncate table 清楚表数据

  1. postgres=# truncate table student_bak;

  

SQL 语句语法简介(一)的更多相关文章

  1. SQL语句语法简介

    SQL命令一般分为DQL.DML.DDL几类: DQL:数据查询语句,基本就是SELECT查询命令,用于数据查询 DML:Data Manipulation Language的简称,即数据操纵语言,主 ...

  2. 【知识库】-数据库_MySQL常用SQL语句语法大全示例

    简书作者:seay 文章出处: 关系数据库常用SQL语句语法大全 Learn [已经过测试校验] 一.创建数据库 二.创建表 三.删除表 四.清空表 五.修改表 六.SQL查询语句 七.SQL插入语句 ...

  3. 关系数据库常用SQL语句语法大全

    创建表 语法 CREATE TABLE <表名>(<列名> <数据类型>[列级完整性约束条件] [,<列名> <数据类型>[列级完整性约束条 ...

  4. 基础SQL语句/语法

    SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库  Create DATABAS ...

  5. mysql数据库之基础SQL语句/语法

    SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库  Create DATABAS ...

  6. sql语句语法大全

    一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...

  7. sql语句语法

    13.1. 数据定义语句 . ALTER DATABASE语法 . ALTER TABLE语法 . CREATE DATABASE语法 . CREATE INDEX语法 . CREATE TABLE语 ...

  8. 转载-增删改查sql语句语法

    一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...

  9. 简单实用 “易忘” 的SQL 语句语法,新老皆宜

    --创建数据库 create database 数据库名 on primary ( name='数据库名_data',  filename='数据库储存路径', size=数据库初始大小(MB),   ...

随机推荐

  1. C++插件架构浅谈与初步实现

    一.插件架构初步介绍 想到写本博客,也没想到更好的名字,目前就先命这个名吧.说到插件架构,或许大部分IT从业者都听过或者某些牛人也自己实现过稳定高效的插件框架.目前有很多软件以及库都是基于插件架构,例 ...

  2. 初始Hbase

    Hbase 定义 HBase是一个开源的非关系型分布式数据库(NoSQL),它参考了谷歌的BigTable建模,实现 的编程语言为 Java. 是Apache软件基金会的Hadoop项目的一部分,运行 ...

  3. 对java高级程序员有益的十本书

    英文原文:http://www.programcreek.com/2013/08/top-books-for-advanced-level-java-developers/ java语言是当今最受欢迎 ...

  4. CRC校验3种算法_转载

    //CRC16校验在通讯中应用广泛,这里不对其理论进行讨论,只对常见的3种//实现方法进行测试.方法1选用了一种常见的查表方法,类似的还有512字//节.256字等查找表的,至于查找表的生成,这里也略 ...

  5. UniGUI的TUniLoginForm窗口自定义背景色和背景图片

    雨田家园 UniGUI的TUniLoginForm窗口自定义背景色 uniGUI的TUniLoginForm类创建的登录窗口默认是不带颜色,可以自定义css风格来改变背景颜色. 一般是通过在UniSe ...

  6. Android-Kotlin-枚举enum

    案例一 星期: 星期的枚举:enum class 类名 {} package cn.kotlin.kotlin_oop09 /** * 定义星期的枚举类 */ enum class MyEnumera ...

  7. ASP .Net C# ---CSV导入导出

    CSV和Excel大致相同  复制下来 数据传到方法里面就可以了 /// <summary> /// DataTable数据写入CSV文件 /// </summary> /// ...

  8. .net图表之ECharts随笔01-最简单的使用步骤

    找了几种绘制图表的办法,后面选定了ECharts.下载链接如下,好像不同的ECharts有不同的用法,要下对. https://gitee.com/Tuky/SomeWebFrame/tree/mas ...

  9. 使用Samba实现文件共享

    1987年,微软公司和英特尔公司,共同制定了SMB(Server Messages Block 服务消息块)协议,指在解决局域网内的文件或打印机等资源的共享问题,这也使得在多个主机之间共享文件变得越来 ...

  10. iOS-项目开发1-图片浏览器

    FFBrowserImageViewController 自定义的图片浏览器:支持图片双击放大,单击取消,拖动取消. 重点: 1:在iOS11之后再布局是要将UIScrollViewContentIn ...