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), ...
随机推荐
- (树)Subtrees -- hdu -- 5524
http://acm.hdu.edu.cn/showproblem.php?pid=5524 Subtrees Time Limit: 2000/1000 MS (Java/Others) Me ...
- springMvc里的mvc:resources与静态资源的访问
在进行Spring MVC的配置时,通常我们会配置一个dispatcher servlet用于处理对应的URL.配置如下: <servlet> <servlet-name&g ...
- 谈谈HashMap线程不安全的体现
原文出处: Hosee HashMap的原理以及如何实现,之前在JDK7与JDK8中HashMap的实现中已经说明了. 那么,为什么说HashMap是线程不安全的呢?它在多线程环境下,会发生什么情况呢 ...
- mysql_结构
代码执行结构:顺序结构.分支机构.循环结构 分支结构:实现准备多个代码块,按照条件选择执行某段代码 在mysql中只有if分支 基本语法 if 条件判断 then -- 满足条件要执行的代码; els ...
- 【译】Surface中你也许不知道的五件事
Bring up the Quick Link Menu - Select the Windows Key + X or right click the Start Button to bring u ...
- 关于分页插件PageHelper
上课的时候学习了分页插件,感受到了它的强大,这里总结如下: 1.首先在spring配置文件中引入依赖jar包: <dependency> <groupId>com.github ...
- yum安装jdk如何配置JAVA_HOME
安装Java环境 yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 查看安装后的配置 java -ver ...
- ASP.Net MVC OA项目笔记<二>
1.1.0 创建数据层 1.1.1 CZBK.ItcastOA.IDAL 引用 CZBK.ItcastOA.Model 1.2.1 给IDAL添加一个接口IUserInfoDal 里面写增删改查分页的 ...
- LVS健康检查脚本
#!/bin/bash #============================================================================= VIP=10.10 ...
- 在redis中使用lua脚本
在实际工作过程中,可以使用lua脚本来解决一些需要保证原子性的问题,而且lua脚本可以缓存在redis服务器上,势必会增加性能. 不过lua也会有很多限制,在使用的时候要注意. 在Redis中执行Lu ...