语句分类

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

一、DDL语句。

1.1建表语句

CREATE TABLE table_name(

col01_name data_type,

col02_name data_type,

);

实例:

student_name varchar(40),
chinese_score int,
math_score int,
test_date date
);
CREATE TABLE

\d  显示表

postgres=# \d score
Table "public.score"
Column | Type | Modifiers
---------------+-----------------------+-----------
student_name | character varying(40) |
chinese_score | integer |
math_score | integer |
test_date | date | 

主键

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

postgres=# create table student(
postgres(# no int primary key,
postgres(# student_name varchar(40),
postgres(# age int);
CREATE TABLE

 1.2 删除表

drop table table_name;

二、DML语句。

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

2.1  插入语句:

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

2.2 更新语句:

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

  使用where过滤:

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

  2.3 删除语句:

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

  2.4.1 查询语句:

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

  2.4.2 过滤查询:

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

  2.4.3排序

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

  2.4.4倒序

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

  2.4.5分组

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

  2.4.6多表联查

postgres=# create table class(no int primary key,
class_name varchar(40));
CREATE TABLE
postgres=# insert into class values(1,'初二(1)班');
INSERT 0 1
postgres=# insert into class values(2,'初二(2)班');
INSERT 0 1
postgres=# insert into class values(3,'初二(3)班');
INSERT 0 1
postgres=# insert into class values(4,'初二(4)班');
INSERT 0 1
postgres=# select * from class;
no | class_name
----+-------------
1 | 初二(1)班
2 | 初二(2)班
3 | 初二(3)班
4 | 初二(4)班
(4 rows)

  

postgres=# create table student2(no int primary key,
student_name varchar(40),
age int,
class_no int);
CREATE TABLE postgres=# insert into student2 values(1,'a',12,1);
INSERT 0 1
postgres=# insert into student2 values(2,'b',13,1);
INSERT 0 1
postgres=# insert into student2 values(3,'c',15,2);
INSERT 0 1
postgres=# insert into student2 values(4,'d',15,2);
INSERT 0 1
postgres=# insert into student2 values(5,'e',15,3);
INSERT 0 1
postgres=# insert into student2 values(6,'f',15,2);
INSERT 0 1
postgres=#

  

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

  

三、其它SQL语句

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

postgres=# create table student_bak(no int primary key,
postgres(# sutdent_name varchar(40),
postgres(# age int,
postgres(# class_no int);
CREATE TABLE
postgres=# insert into student_bak select * from student;
INSERT 0 2
postgres=# select * from student_bak;
no | sutdent_name | age | class_no
----+--------------+-----+----------
2 | haha | 15 |
1 | steven | 13 |
(2 rows)

  

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

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

  truncate table 清楚表数据

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. bzoj2879(动态加边费用流)

    参考题解:http://blog.csdn.net/yxuanwkeith/article/details/52254602 //开始跑费用流用的dijkstra,一直错,后来发现动态加边后我不会处理 ...

  2. How to fix "http error 403.14 - forbidden" in IIS7

    If you encounter the following error: "http error 403.14 - forbidden. The Web server is configu ...

  3. 【python-crypto】导入crypto包失败的情况,怎么处理

    [python-crypto]导入crypto包失败的情况,怎么处理 是因为你自己安装的python的版本太高,所以自己降版本吧,捣鼓了一下午 pip install crypto pip insta ...

  4. thrift使用总结

    转自 http://blog.csdn.net/qq_27784479/article/details/73250958 Apache Thrift软件框架用于可扩展的跨语言服务开发,简单来说就是RP ...

  5. 增加显示记录数的label及隐藏refresh按钮

    1. 在UniDBgrid的extEvent属性中写入以下代码: function OnAfterCreate(sender) { var toolbar=sender.getDockedItems( ...

  6. FastReport

    看下面 https://www.cnblogs.com/m0488/category/477792.html

  7. [转载]WIKI MVC模式

    MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller). MVC模式最 ...

  8. SpringMVC源码分析(3)DispatcherServlet的请求处理流程

    <springmvc源码分析(2)dispatcherservlet的初始化>初始化DispatcherServlet的多个组件. 本文继续分析DispatcherServlet解析请求的 ...

  9. Angular build Error:In this configuration Angular requires Zone.js

    Angular cli 运行 build后打开生成的index.html报错:In this configuration Angular requires Zone.js 生成代码如下: ng bui ...

  10. [leetcode.com]算法题目 - Pascal's Triangle

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...