1、数据库操作

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

2、表操作

  1. -- 创建表
  2. create table student(
  3. id int not null primary key,
  4. name varchar(20) not null,
  5. age int null,
  6. sex varchar(10)
  7. )
  8. -- 删除表
  9. drop table student
  10. -- 修改表,增加一个列
  11. Alter table student add column address varchar(50)

3、sql语句

简单语句

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

高级语法

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

4、表连接

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

  1. create table course(
  2. cno int not null primary key,
  3. cname varchar(20) not null
  4. )
  5. create table StudentCourse(
  6. sno int not null,
  7. cno int not null,
  8. score double
  9. )
  10.  
  11. -- 表连接, 查找所有学生的选课记录
  12. select s.name as 学生姓名,sc.cno as 选修课号,sc.score as 成绩
  13. from student s, StudentCourse sc
  14. where s.id=sc.sno
  15.  
  16. -- 内连接, 查找所有成绩及格的选课记录
  17. select s.name as 学生姓名,sc.cno as 选修课号,sc.score as 成绩
  18. from student s
  19. inner join StudentCourse sc on s.id=sc.sno
  20. where sc.score>60
  21.  
  22. -- 左连接, 查找所有学生的选课记录
  23. select s.id as 学号,sc.cno as 选修课号,sc.score as 成绩
  24. from student s
  25. left join StudentCourse sc on s.id=sc.sno
  26.  
  27. -- 嵌套查询, 查找王敏同学的选课记录
  28. select *
  29. from StudentCourse
  30. where sno in (
  31. select id from student where name='王敏'
  32. )
  33.  
  34. --查找每个学生大于自身平均分的科目
  35. select cno
  36. from StudentCourse a
  37. where score> (
  38. select avg(score) from StudentCourse b where a.sno=b.sno
  39. )

5、SQL 约束

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

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

6、索引

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

  1. -- 创建索引
  2. create index idx_age on student(age asc)
  3. create unique index idx_name on student(name)
  4. -- 删除索引
  5. drop index idx_name

7、视图

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

  1. -- 删除视图
  2. if exists (select * from dbo.sysobjects where id = object_id(N'dbo.young_student') and objectproperty(id, N'isview') = 1)
  3. drop view young_student
  4. -- 创建视图
  5. create view young_student
  6. as
  7. 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. RobotFramework学习-问题

    RobotFramework,基于Python的自动化测试框架.近期学习中遇到过一些问题. 1.运行ride时,报错[ ERROR ] option --monitorcolors not recog ...

  2. javascript总结26:Date

    1 获取Date对象 Date-引用类型,JavaScript中的内置对象 获取当前时间 var date = new Date(); //UTC的时间 //返回数字,时间的毫秒形式 var date ...

  3. 设计模式17:Iterator 迭代器模式(行为型模式)

    Iterator 迭代器模式(行为型模式) 动机(Motivation) 在软件构建过程中,集合对象内部结构常常变化各异.但对于这些集合对象,我们希望在不暴露其内部结构的同时,可以让外部客户代码可以透 ...

  4. git 只merge一个commit的方法

    https://git-scm.com/book/tr/v2/Git-Basics-Viewing-the-Commit-History gil log 来查看commit的记录 Other main ...

  5. 设计模式12---享元模式(Flyweight Pattern)

    享元模式 定义:共享元对象,运用共享技术有效地支持大量细粒度对象的复用.如果在一个系统中存在多个相同的对象,那么只需要共享一份对象的拷贝,而不必为每一次使用创建新的对象. 享元模式是为数不多的.只为提 ...

  6. memcached整理の编译

    memcached是一个自由&开放源码, 高性能,分布式的内存对象缓存系统. nosql相对于传统关系型数据库的"行与列",NoSQL 的鲜明特点为k-v 存储(memca ...

  7. [SIP01]SIP Header Fields里面各字段用途

    INVITE sip:bob@biloxi.com SIP/2.0 Via: SIP/2.0/UDP pc33.atlanta.com;branch=z9hG4bK776asdhds Max-Forw ...

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

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

  9. Qt学习(一)

    1. 简介 跨平台 GUI 通常有三种实现策略 API 映射 相当于将不同平台的 API 提取公共部分.界面库使用同一套 API,将其映射到不同的底层平台上面.相当于取交集 如wxWidgets. 优 ...

  10. 牛客网提高组模拟赛第七场 T2 随机生成树

    其实看懂题就很水啦qwq,就是求\(1-N\)的约数啦. 暴力求的话时间复杂度是\(O(NlogN)\)的,其实正解是枚举每个数的倍数......这样的时间复杂度是\(\frac{N}{1}+\fra ...