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. 使用BBED理解和修改Oracle数据块

    1.生成bbed list file文件: SQL> select file#||' '||name||' '||bytes from v$datafile; $ vim dbfile.txt ...

  2. Postman -- HTTP请求的Chrome插件

    摘要 : Postman是一款功能强大的网页调试与发送网页HTTP请求的Chrome插件. 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候是需要一些方法来跟踪网页请求的,用户可以使用一些网 ...

  3. Gym - 100989G (二分法)

    There are K hours left before Agent Mahone leaves Amman! Hammouri doesn't like how things are going ...

  4. .net 程序集的加载与反射

    一. 程序集的加载: 在CLR内部使用System.Reflection.Assembly类的静态LoadFrom方法尝试加载程序集. LoadFrom方法在内部调用Assembly的Load方法,将 ...

  5. github 上中国互联网公司的开源项目

    github上 那个 watch和 follow功能 不太好用啊. 是我用的 不好,还是 怎么的.有时候 找不到 watch 和 follow. 秉持 开源 精神,省的大家 和 我 查找. 我只关注 ...

  6. jquery 实现抖动效果

    jQuery.fn.shake = function (intShakes /*Amount of shakes*/, intDistance /*Shake distance*/, intDurat ...

  7. [IE bug] ajax请求 304解决方案

    最近和筒子们做了个校园电台,进去之后会自动播放歌曲,每首放完了的话会随机get新的json,然后再播放下一首 整体做成了命令行的风格,在最后输入next,start等命令来操作,5+M/s校园网+W级 ...

  8. 微信开发之c#下获取jssdk的access_token

    获取access_token是调用微信JS接口的基础,只有得到了它才能得到我们需要的jsapi_ticket并生成签名,然后注入配置信息config. 微信官方文档我就不多做介绍,反正我是踩了不少坑. ...

  9. .net core 2.0 mvc 获取配置信息

    mvc_core_config *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 ...

  10. Windows上传文件到linux 使用winscp

    Windows上传文件到linux 使用winscp, winscp下载目录 https://sourceforge.net/projects/winscp/postdownload?source=d ...