SQL 语句语法简介(一)
语句分类
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 语句语法简介(一)的更多相关文章
- SQL语句语法简介
SQL命令一般分为DQL.DML.DDL几类: DQL:数据查询语句,基本就是SELECT查询命令,用于数据查询 DML:Data Manipulation Language的简称,即数据操纵语言,主 ...
- 【知识库】-数据库_MySQL常用SQL语句语法大全示例
简书作者:seay 文章出处: 关系数据库常用SQL语句语法大全 Learn [已经过测试校验] 一.创建数据库 二.创建表 三.删除表 四.清空表 五.修改表 六.SQL查询语句 七.SQL插入语句 ...
- 关系数据库常用SQL语句语法大全
创建表 语法 CREATE TABLE <表名>(<列名> <数据类型>[列级完整性约束条件] [,<列名> <数据类型>[列级完整性约束条 ...
- 基础SQL语句/语法
SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库 Create DATABAS ...
- mysql数据库之基础SQL语句/语法
SQL是现在进入互联网工作人们的必须技能之一,下面分享自己觉得很nice的SQL基本语句,从网上找了,觉得很不错,就分享给大家!简要介绍基础语句: 1.说明:创建数据库 Create DATABAS ...
- sql语句语法大全
一.基础 1.说明:创建数据库 CREATE DATABASE database-name 2.说明:删除数据库 drop database dbname 3.说明:备份sql server --- ...
- sql语句语法
13.1. 数据定义语句 . ALTER DATABASE语法 . ALTER TABLE语法 . CREATE DATABASE语法 . CREATE INDEX语法 . CREATE TABLE语 ...
- 转载-增删改查sql语句语法
一.增:有2种方法 1.使用insert插入单行数据: 语法:insert [into] <表名> [列名] values <列值> 例:insert into Strdent ...
- 简单实用 “易忘” 的SQL 语句语法,新老皆宜
--创建数据库 create database 数据库名 on primary ( name='数据库名_data', filename='数据库储存路径', size=数据库初始大小(MB), ...
随机推荐
- (概率 01背包) Just another Robbery -- LightOJ -- 1079
http://lightoj.com/volume_showproblem.php?problem=1079 Just another Robbery As Harry Potter series i ...
- Swagger生成WebAPI文档
WebAPI2.0 项目可以使用Swagger能够轻易查看API文档,查看以下配置 1.打开程序包管理控制台输入: Install-Package Swashbuckle 2.在对应项目里的App_S ...
- Codeforces820A Mister B and Book Reading 2017-06-28 09:38 67人阅读 评论(0) 收藏
A. Mister B and Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces812A Sagheer and Crossroads 2017-06-02 20:41 139人阅读 评论(0) 收藏
A. Sagheer and Crossroads time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Scala界面事件处理编程实战详解.
今天学习了一个Scala界面事件处理编程,让我们从代码出发. import scala.swing._import scala.swing.event._ object GUI_Panel exten ...
- Android-Kotlin-枚举enum
案例一 星期: 星期的枚举:enum class 类名 {} package cn.kotlin.kotlin_oop09 /** * 定义星期的枚举类 */ enum class MyEnumera ...
- mooctest项目总结 【转载】
原文链接 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...
- Python--随机生成指定长度的密码
在浏览别人博客时学习了random模块,手痒自我练习下,写个随机生成指定长度的密码字符串的函数,拿出来供各位参考: 废话不多说,上代码: # coding: utf-8 import random i ...
- 可编辑树Ztree的使用(包括对后台数据库的增删改查)
找了很多网上关于Ztree的例子和代码才搞定. 首先,关于Ztree的代码不介绍了,网上下载之后,引用下列四个文件就能使用了. 1.关于配置选项.主要通过回调函数来实现向后台发送数据,实现增删改查. ...
- Distinct Substrings(spoj694)(sam(后缀自动机)||sa(后缀数组))
Given a string, we need to find the total number of its distinct substrings. Input \(T-\) number of ...