1、数据库操作

create database student_info  -- 创建数据库
drop database student_info -- 删除数据库

2、表操作

-- 创建表
create table student(
id int not null primary key,
name varchar(20) not null,
age int null,
sex varchar(10)
)
-- 删除表
drop table student
-- 修改表,增加一个列
Alter table student add column address varchar(50)

3、sql语句

简单语句

插入(增):insert into student(id, name, address) values(1, 'Xiaohong', 16)
删除(删):delete from student where age<=6
更新(改):update student set name='Lily' where id=1
查询(查):select * from student

高级语法

模糊查询:select * from student where name like '%Xiao%'
排序:select * from student order by field1,field2 desc
总数:select count(*) as totalcount from student
函数:select sum(age) as sumAge, avg(age) as avgAge, max(age) as maxAge, min(age) as minAge from student
前几: select top 10 * from student order by age desc
去重: select distinct name from student
多个条件: select * from student where name like '%Xiao%' and age=16 or age=20
between: select * from student where age between 10 and 20
in: select * from student where name in ('Lily', 'Amy')
分组: select age, count(*) from student group by age
分组带条件: select age, count(*) from student group by age where age >10 having count(*)<5

4、表连接

JOIN: 如果表中有至少一个匹配,则返回行
LEFT JOIN: 即使右表中没有匹配,也从左表返回所有的行
RIGHT JOIN: 即使左表中没有匹配,也从右表返回所有的行
FULL JOIN: 只要其中一个表中存在匹配,就返回行

create table course(
cno int not null primary key,
cname varchar(20) not null
)
create table StudentCourse(
sno int not null,
cno int not null,
score double
) -- 表连接, 查找所有学生的选课记录
select s.name as 学生姓名,sc.cno as 选修课号,sc.score as 成绩
from student s, StudentCourse sc
where s.id=sc.sno -- 内连接, 查找所有成绩及格的选课记录
select s.name as 学生姓名,sc.cno as 选修课号,sc.score as 成绩
from student s
inner join StudentCourse sc on s.id=sc.sno
where sc.score>60 -- 左连接, 查找所有学生的选课记录
select s.id as 学号,sc.cno as 选修课号,sc.score as 成绩
from student s
left join StudentCourse sc on s.id=sc.sno -- 嵌套查询, 查找王敏同学的选课记录
select *
from StudentCourse
where sno in (
select id from student where name='王敏'
) --查找每个学生大于自身平均分的科目
select cno
from StudentCourse a
where score> (
select avg(score) from StudentCourse b where a.sno=b.sno
)

5、SQL 约束

约束用于限制加入表的数据的类型。可以在创建表时或表创建后规定约束。约束主要有以下几类:

  • NOT NULL   强制列不能为 NULL 值
  • UNIQUE      唯一标识数据库表中的每条记录, 每个表可以有多个 UNIQUE 约束,但是每个表只能有一个 PRIMARY KEY 约束。
  • PRIMARY KEY  唯一标识数据库表中的每条记录,主键必须包含唯一的值,主键列不能包含 NULL 值。
  • FOREIGN KEY  一个表中的 FOREIGN KEY 指向另一个表中的 PRIMARY KEY,如StuentCource 的sno指向Student的id
  • CHECK    在特定的列中对值进行限制
  • DEFAULT  设置默认值
create table student(
id int not null,
name varchar(20) not null,
age int null DEFAULT 1,
UNIQUE (name),
PRIMARY KEY (id),
CONSTRAINT chk_age check (age>0 AND age<200),
CONSTRAINT uq_name unique(name)
)

6、索引

您可以在表中创建索引,以便更加快速高效地查询数据。

-- 创建索引
create index idx_age on student(age asc)
create unique index idx_name on student(name)
-- 删除索引
drop index idx_name

7、视图

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

-- 删除视图
if exists (select * from dbo.sysobjects where id = object_id(N'dbo.young_student') and objectproperty(id, N'isview') = 1)
drop view young_student
-- 创建视图
create view young_student
as
select * from student where age<10

sql 简单语法的更多相关文章

  1. SQL简单语法

    (1)select SELECT 列名称 FROM 表名称 (2)distinct SELECT DISTINCT 列名称 FROM 表名称 SELECT * FROM 表名称 (3)where SE ...

  2. 我的MYSQL学习心得(一) 简单语法

    我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...

  3. Sql常用语法以及名词解释

    Sql常用语法以及名词解释 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) D ...

  4. SQL select 语法(转)

    SQL 里面最常用的命令是 SELECT 语句,用于检索数据.语法是: SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expr ...

  5. SQL基础语法笔记教程整理

    PS:本文适用SQL Server2008语法. 一.关系型数据库和SQL 实际上准确的讲,SQL是一门语言,而不是一个数据库. 什么是SQL呢?简而言之,SQL就是维护和使用关系型数据库中的的数据的 ...

  6. sql 常用语法汇总

    Sql常用语法 SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE,UPDATE,INSERT) DCL—数据控 ...

  7. T-Sql(一)简单语法

    原文:T-Sql(一)简单语法 Sql Server是鄙人学习的第一种数据库,对Sql Server有一种特别的情感,下面就说一下Sql Server的简单语法,适用初学者. 1,创建数据库creat ...

  8. MySQL基本语法(一):和SQL Server语法的差异小归纳

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  9. Spark的Streaming和Spark的SQL简单入门学习

    1.Spark Streaming是什么? a.Spark Streaming是什么? Spark Streaming类似于Apache Storm,用于流式数据的处理.根据其官方文档介绍,Spark ...

随机推荐

  1. 使用OpenSSL进行转换

    使用OpenSSL进行转换 摘自:https://cloud.tencent.com/developer/ask/29886 这些命令允许您将证书和密钥转换为不同的格式,以使它们与特定类型的服务器或软 ...

  2. HTML的DOM树结构

    在面试连续跪了两轮后,我觉得两个月的前端白学了.主要的原因是学而不思,知识是零散的,并没有组织起来.于是,我决定从今天起,复习并总结一下前端的知识点. 一般的网页浏览者看到的是网页的整体外观,前端开发 ...

  3. 单一入口及MVC目录规范

    单一入口指在一个web应用程序中,所有的请求都是指向一个脚本文件,例如我们经常看到某一个网站所有的页面都是index.php?xxxx这样的形式.所有对使用程序的访问都是必须通过这个入口. 目录规范:

  4. 为 pycharm 修改 Theme & Color

    版本: pycharm-community-4.5.2 安装之后先导入 (File --> Import Setting --> django.jar) 将文件pycharm-themes ...

  5. Centos7下安装与卸载Jdk1.8

    安装 去官网下载jdk:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 使用xs ...

  6. lspci通过系统总线查看硬件设备信息

    lspci - 列出所有PCI设备 PCI 的科普: PCI(Peripheral Component Interconnect),是一种连接电子计算机主板和外部设备的总线标准. 常见的PCI卡包括网 ...

  7. C# 过滤SQL 字符串中的 参数

    /// <summary> /// 参数过滤 /// </summary> /// <param name="parameters"></ ...

  8. Atcoder 2566 3N Numbers(优先队列优化DP)

    問題文N を 1 以上の整数とします. 長さ 3N の数列 a=(a1,a2,…,a3N) があります. すぬけ君は.a からちょうど N 個の要素を取り除き.残った 2N 個の要素を元の順序で並べ. ...

  9. 关于HTTP协议传输与接收数据的相关内容

    第一篇: HTTP请求报文和HTTP响应报文 http://www.cnblogs.com/biyeymyhjob/archive/2012/07/28/2612910.html 第二篇: 深入浅出U ...

  10. [LeetCode 题解]: Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...