SQL
掌握一门编程语言:
C C++ Java C#
...

数据库
数据结构/算法 链表 队列 栈 数组
面向对象
网络
(界面、业务逻辑)

关系型数据库:
以二维表的形式组织数据
表、索引、视图、触发器...

记录(行)
字段(例)

DDL(数据定义语言)
创建表
删除表
修改表
创建索引
删除索引
DML(数据操纵语言)




--------------------------------------
创建表:
create table 表名(字段名 字段类型 [约束],...);

字段类型(5):
integer 整型int
real 实型float double
text 文本类型char varchar nchar
NULL 空类型
blob 二进制类型

约束:
primary key 主键,用来在表中唯一标识一条记录
not null 不为空
unique 唯一
default 默认
check 检查
foreign key 外键(了解)

create table student(id integer primary key autoincrement, name text not null, age integer check(age > 6 and age < 80), addr text default "CHINA" );

删除表:
drop table 表名;
drop table student;

修改表:
sqlite3可改变表名,也可以增加字段
1.修改表名:
alter table 表名 rename to 新的表名;
例:
alter table student rename to stud;
2.增加字段
alter table 表名 add column 字段名 字段类型;
alter table stud add column tel int;

--------------------------------------------------------
插入数据:
insert into 表名(字段1,字段2,...)values(字段值1,字段值2,...);
insert into 表名 values(字段值,...);
例:
insert into stud(id,name,age,tel) values(1001,"limu",24,13100000000);

修改记录:
update 表名 set 字段=新值 [字段n=新值] [where 条件];
例:
update stud set tel=1380000000;
条件:
字段 运算符 值 and/or 字段 运算符 值
运算符: = != <> >= <=
update stud set tel=1380000000 where name="limu" and age=24;

删除记录:
delete from 表名 [where 条件];
例:
delete from stud where id <> 1001;

查询记录:
select * from 表名 [where 条件];
*:通配符,表示所有的字段
select 字段1,字段2,字段n,...from 表名 [where 条件];
例:
select name,age,tel from stud where id=1000;

-------------------------------------------------------------

模糊查询:
like
where name like '张%'

SQL通配符:
% 替代一个或多个字符
_ 仅替代一个字符

例:
select * from stud where name like 'b%';

限定和排序:
limit 和 order by
limit:指定返回记录的最大数量,offset指定偏移的记录数
select * from stud limit 2;

order by:排序(升序asc 降序desc)
select * from 表名 order by 字段 asc/desc;
select * from stud order by age desc;

限定+排序:
select * from stud order by age desc limit 3;

函数:
abs(),length(),upper(),lower(),typeof()
sum(),avg(),count(),min(),max()
这些函数的参数为字段名
别名(as):select count() as count from stud;

日期/时间函数
date()
time()
datetime()
这几个函数的参数:(timestring,modifier,...)

例:
datetime('now','localtime');
select datetime('now','localtime','+1 day','+1 month');

分组:group by
用于结合上述函数,根据一个或多个字段对结果集进行分组。

select id,sum(score) from cj group by id;
select id,sum(score) as sum
from cj
group by id
order by sum desc
limit 1;

多表查询:
select stud.id,name from stud,cj where cj.id=stud.id and cj.score=90;

子查询:
指select 语句中以嵌套select语句
子查询最常用的地方是where子句中,特别是在in操作符中。
select 9 in(1,2,3,4,5);
select * from stud where age in(select age from stud where id < 1003);

作业(选修):

1.统计班上有多少人?

2.删除姓名是张飞的语文成绩

3.将数学改成高等数学

4.查总成绩排名前三的学生学号、名字和分数,要求降序输出

5.查不及格的学生姓名和不及格的课程名

学生表(id,name,age);
课程表(cid,cname,score);
成绩表(id,cid,fenshu);

create table student(id integer primary key,name text not null,age integer);

insert into student values(1000,"limu",23);

insert into student values(1001,"baiqi",22);

insert into student values(1002,"wangjian",24);

insert into student values(1003,"lianpo",25);

select * from student;

create table course(cid integer primary key,cname text not null,sorce integer);

.tab

insert into course values(1,"yuwen",2);

insert into course values(2,"shuxue",3);

insert into course values(3,"english",1);

select * from course;

create table card(id integer,cid integer,fenshu integer);

.tab

insert into card values(1000,1,80);

insert into card values(1000,2,90);

insert into card values(1000,3,70);

select * from card;

insert into card values(1001,1,60);

insert into card values(1001,2,70);

insert into card values(1001,3,80);

insert into card values(1002,1,80);

insert into card values(1002,2,90);

insert into card values(1002,3,100);

insert into card values(1003,1,10);

insert into card values(1003,2,90);

insert into card values(1003,3,70);

select * from card;

.tab;

1.统计班上有多少人?

select count(id) as count from student;

2.删除姓名是limu的yuwen成绩

delete fenshu from student,course,card

where student.id=card.id and card.cid=course.cid and name='limu' and cname='yuwen'

and fenshu=80;

3.将shuxue改成gaodengshuxue

update course set cname='gaodengshuxue' where cid=2;

select student.id student.name card.fenshu from student,card where sum(fenshu)

and order by age asc limit 3;

sql相关语言的更多相关文章

  1. SQLAlchemy 学习笔记(一):Engine 与 SQL 表达式语言

    个人笔记,如有错误烦请指正. SQLAlchemy 是一个用 Python 实现的 ORM (Object Relational Mapping)框架,它由多个组件构成,这些组件可以单独使用,也能独立 ...

  2. PySpark SQL 相关知识介绍

    title: PySpark SQL 相关知识介绍 summary: 关键词:大数据 Hadoop Hive Pig Kafka Spark PySpark SQL 集群管理器 PostgreSQL ...

  3. SQL设置语言,返回中文”星期几”格式

    SQL中语言表: SELECT * FROM sys.syslanguages   eg: SET LANGUAGE 简体中文 --设置语言 PRINT DATENAME(weekday,GETDAT ...

  4. SQL Server数据库(SQL Sever语言 CRUD)

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  5. 使用ttXactAdmin、ttSQLCmdCacheInfo、ttSQLCmdQueryPlan获取SQL相关具体信息[TimesTen运维]

    使用ttXactAdmin.ttSQLCmdCacheInfo.ttSQLCmdQueryPlan获取SQL相关具体信息,适合于tt11以上版本号. $ ttversion TimesTen Rele ...

  6. 用sql语句查出和sql相关的性能计数器

    一台服务器上,用性能监视器死活显示不出来一部分计数器,没办法,用sql语句查了 --所有和sql相关的计数器 select * from sys.dm_os_performance_counters ...

  7. SQL的语言分类

    SQL的语言分类 DQL DQL(Data Query Language):数据查询语言 select DML DML(Data Manipulate Language):数据操作语言 insert ...

  8. Django框架08 /聚合查询、分组、F/Q查询、原生sql相关

    Django框架08 /聚合查询.分组.F/Q查询.原生sql相关 目录 Django框架08 /聚合查询.分组.F/Q查询.原生sql相关 1. 聚合查询 2. 分组 3. F查询和Q查询 4. o ...

  9. Oracle学习笔记四 SQL命令(二):SQL操作语言类别

    SQL分为下列语言类别 1.数据定义语言(DDL) Create.Alter.Drop 2.数据操纵语言(DML) Insert.Select.Delete.Update 3.事务控制语言(TCL) ...

随机推荐

  1. 在linux下用tomcat部署java web项目的过程与注意事项

    在linux下用tomcat部署java web项目的过程与注意事项 一.安装JDK 到http://www.oracle.com/technetwork/java/javase/downloads/ ...

  2. css text-indent:999em

    em是个单位,是字符宽度text-indent:999em首行缩进999个字符 大约多长?大约相当于多少PX?能不能用PX来表示这个缩进? 等于当前的字体大小.当font-size:12px; 1em ...

  3. STM32软件复位(基于库文件V3.5)

    源:STM32软件复位(基于库文件V3.5) void SoftReset(void) { __set_FAULTMASK(); // 关闭所有中端 NVIC_SystemReset();// 复位 ...

  4. ASP.NET中的Response

    Response.BufferOutput=true.false  是否设置缓存 Response.Write("")   输出字符串 Response.IsClientConne ...

  5. AXIS-web.xml里配置axis报错addChild: Child name 'AxisServlet' is not unique 解决办法

    报错这个那么就表示,web.xml中有相同的AxisServlet的这个名字,可以把原来的删除配置自己的,也可以保留原来的,自己的不配置 Ctrl+f 搜索下就知道了

  6. android脚步---设置layout隐藏属性

    设置layout的属性,应用到android view的setVisibility 有三个值 visibility  VISIBLE, INVISIBLE, GONE. 可见的     不可见的    ...

  7. 有向图强连通分支的Tarjan算法讲解 + HDU 1269 连通图 Tarjan 结题报告

    题目很简单就拿着这道题简单说说 有向图强连通分支的Tarjan算法 有向图强连通分支的Tarjan算法伪代码如下:void Tarjan(u) {dfn[u]=low[u]=++index//进行DF ...

  8. HDU 1166 敌兵布阵(树状数组)

    之前用过了线段树的做法,树状数组的也补上吧 #include<iostream> #include<cstdio> #include<cstring> using ...

  9. DWR 整合之Struts2.3.16

    DWR 能够和任何框架结合. DWR 和 Struts 整合有 2 个层次.最基础的层次就是同时使用这两个框架,这是非常容易的,但是这样就不允许在 DWR 和 Struts 之间共享 Action 了 ...

  10. 自动化运维工具Ansible详细部署

    本文来源:http://sofar.blog.51cto.com/353572/1579894/ 前言 一个由 Python 编写的强大的配置管理解决方案.尽管市面上已经有很多可供选择的配置管理解决方 ...