SQLite语句学习

前天赶项目的时候,发现好多语句都忘了。
今天重新复习一下,原来我不是真的喜欢数据库设计,叶公好龙而已,很多东西我都不知道。
从头滚一遍。

测试数据库在这里:
链接:在这里
提取码:dait

select * from AAEM;             --查询表中所有内容
select se_id from AAEM;         --查询某列
select se_id from AAEM where ath_id>=10;      --条件查询
select count(*) from AAEM where ath_id<=10;   --查询id小于10的运动员总数
select avg(ath_id) from AAEM;   --查询所有运动员id的平均值
select sum(ath_id) from AAEM;   --查询所有运动员id的总和
select max(ath_id) from AAEM;   --查询运动员中id最大者
select min(ath_id) from AAEM;   --查询运动员中id最小者

select current_timestamp;  --查询当前时间

select se_id from AAEM where ath_id>=10 and ath_id<=20; --多条件交查
select se_id from AAEM where ath_id<=10 or ath_id>=30;  --多条件并查
select se_id from AAEM where ath_id is not 10;          --否定查询

select se_id from AAEM where ath_id like "3%";          --正则表达式
select se_id from AAEM where ath_id like "%3";          --还是正则表达式
select se_id from AAEM where ath_id like "%3%";         --这样也一样 那个百分号,匹配任意数量任意字符,包括空
select se_id from AAEM where ath_id like "3_";          --下划线代表一个字符

select ath_id from AAEM where ath_id GLOB "3*";         --也是正则表达式,不过GLOB是大小写敏感的,*相当于like的%,?相当于like的_

select se_id from AAEM where ath_id in (20,30);         --不是一个区间,真不好意思,就那俩数
select se_id from AAEM where ath_id not in (20,30);     --心照不宣
select se_id from AAEM where ath_id between 20 and 30;  --喏,一个区间

--插入我就不多说
insert into AAEM(flag) values(100); --挑着插

update AAEM set ath_id = 5 where se_id = 2; --纪录修改
update AAEM set score = 90;                 --一串全改了

delete from AM where sex = 'M';             --删除操作

select ath_id from AAEM limit 6;            --限制提取六行
select ath_id from AAEM limit 6 offset 2;   --从第2行的下一行开始,提取六行

select ath_id from AAEM order by ath_id desc;                 --按照ath_id列降序输出
select ath_id from AAEM order by ath_id,score desc;           --如果出现俩相等,以第二个条件排
select ath_id from AAEM order by ath_id asc;                  --升序输出

select sum(ath_id),se_id from AAEM where score = 90 group by se_id order by ath_id desc;    
--这个group,用于对多个查询结果进行分组组合,至于怎么组合,只可意会。放在where之后,order by之前,常用于联表查询

select sum(ath_id),se_id from AAEM where score = 90 group by se_id having se_id>3 order by ath_id desc; 
--having 语句,允许指定条件来过滤将输出的数据,不过是为group by语句配备的

select distinct score from AAEm;   --dintinct语句,清除重复项

--以上部分为单表查询,以下部分为联表查询
select ranking from AAEM,AM where AAEM.[ath_id] = AM.[ath_id] and AM.[name] = "男一";
-- 其他请参照单表查询融会贯通,我这儿没啥题目

--以上部分为初级教程,以下部分为一些高级教程
--SQLite约束
CREATE TABLE COMPANY(
   ID INT PRIMARY KEY,                 --主键约束,一个表只能有一个主键,主键唯一,可以为NULL(sqlite特有)。
   NAME           TEXT    NOT NULL,    --非空约束
   AGE            INT     NOT NULL UNIQUE, --unique,数据唯一性约束
   ADDRESS        CHAR(50),
   SALARY         REAL     DEFAULT 50000.00   --如果输入为空,设置一个默认值
   SALARY2        REAL     CHECK(SALARY > 0)  --check,数据输入约束  
);


--SQLite Joins
select name,AAEM.[ath_id],ath_score,ranking from AAEM cross join AM;
--cross join,交叉连接,把第一个表的每一行与第二个表的每一行进行匹配。要谨慎使用,因为表的大小会是两个表相乘
  
select name,ranking from AAEM inner join AM on AAEM.[ath_id] = AM.[ath_id];
--inner join,内连接,是默认的连接

select SE_name,rk_rl,ath_id from AAEM left outer join SEM on AAEM.[se_id] = SEM.[se_id];
--outer join,外连接,是内连接(INNER JOIN)的扩展。虽然 SQL 标准定义了三种类型的外连接:LEFT、RIGHT、FULL,但 SQLite 只支持 左外连接(LEFT OUTER JOIN)。

--SQLite 的 NULL 是用来表示一个缺失值的项。SQLite 的 NULL 是用来表示一个缺失值的项。

--触发器、索引
--目前是个缺失值

--Alter命令
ALTER TABLE database_name.table_name RENAME TO new_table_name; --用来重命名已有的表的 ALTER TABLE 
ALTER TABLE database_name.table_name ADD COLUMN column_def...; --用来在已有的表中添加一个新的列的 ALTER TABLE

--剩下都是缺失值,过两天再补,对于我现在这个水平,前面那些暂时够用

猜你喜欢

转载自blog.csdn.net/qq_43762191/article/details/106605372