学习网站:SQL教程 (yiibai.com)
练习内容
--select top 1000 * from table_Plant;
--alter table table_Plant
-- add plant_HP int not null,
-- plant_CD int not null,
-- plant_ATK int not null,
-- plant_Temp varchar(20);
--alter table table_Plant
--alter column plant_Temp int not null;
--alter table table_Plant
-- drop column plant_Temp;
--insert into table_Plant
-- values
-- ('豌豆射手',100,5,10),
-- ('双发射手',100,5,10),
-- ('三线射手',125,5,10),
-- ('机枪射手',100,20,10),
-- ('香蒲',100,20,10),
-- ('向日葵',100,5,0);
--create table table_Plant2
--(
-- plant_id int not null primary key,
-- plant_name varchar(50) not null,
-- plant_HP int not null,
-- plant_CD int not null,
-- plant_ATK int not null,
--);
--insert into table_Plant2
--select * from table_Plant;
--update table_Plant
--set plant_ATK = 15
--where plant_name='香蒲';
--update table_PlantCopy
--set plant_ATK = 15
--where plant_name='三线射手';
--多行同理
--delete
--from table_plant
--where plant_id=1;
--select plant_name,plant_ATK
--from table_Plant
--AS 取别名
--select 中还可以进行简单计算
--默认ASE升序,DESC降序,可以多列排序,依次写就好了
--select plant_name,plant_ATK
--from table_Plant
--order by plant_ATK;
----无重复 distinct
--select distinct
-- plant_ATK
--from table_Plant
--order by plant_ATK;
--Limit 取几个,Offset 偏移量 SQLServer 不支持,OFFSET可以忽略
--select plant_name,plant_ATK
--from table_Plant
--order by plant_ATK desc
----limit 3 offset 1;
--与limit 同作用
--select plant_name,plant_ATK
--from table_Plant
--order by plant_ATK desc
--offset 2 rows
--fetch next 2 rows only;
--条件筛选 where 紧接from 后面,里面写condition(条件)
-- and 关键字,支持短路
-- or 关键字
-- between 关键字
--select *
--from table_Plant
--where plant_ATK between 10 and 15;
-- in 关键字 可以用一个或多个 or 重写 里面常嵌套子查询使用
-- like 关键字 % 匹配任意个字符, _ 匹配任意单个字符
-- null 不能直接用等号(=),要使用 is null 或者 is not null
-- not 可用于否定bool 表达式
-- as 关键字,起别名,可选的(就是可以忽略)
--create table table_Zomble(
-- zomble_id int identity primary key,
-- zomble_name varchar(20) not null,
-- zomble_HP int not null,
-- zomble_ATK int not null,
-- zomble_speed int not null
--);
--insert into table_Zomble
--values
-- ('普通僵尸',100,20,10),
-- ('路障僵尸',200,20,10),
-- ('铁桶僵尸',400,20,10),
-- ('撑杆跳僵尸',150,20,20),
-- ('橄榄球僵尸',600,20,20),
-- ('旗帜僵尸',100,20,15);
--select *
--from
-- table_Plant tp
--inner join table_Zomble tz on tp.plant_id = tz.zomble_id;
--select *
--from
-- table_Plant tp
--inner join table_Zomble tz on tp.plant_id = tz.zomble_id
--inner join table_PlantCopy tpc on tp.plant_ATK = tpc.plant_ATK;
-- inner join 表名 on 条件 内连接,交集
-- left join xxx on xxx 左连接,交集加上左边的表
-- right join xxx on xxx 右连接,交集加上右边的表
-- full orther join xxx on xxx 外连接,就两个表相连,有相同的就填充进去,没有就直接上null
--select top 1000 * from table_Plant;
--exec sp_help table_Plant;