个人整理的sql基本语句(不全)

    技术2024-10-03  73

    --创建数据库 --主文件 create database Fuxi on primary ( -- 名 类型 是否为空 约束 name="Fuxi",--库名 filename="E:/数据库/复习/Fuxi",--地址 size=5MB,--初始空间大小 maxsize=unlimited,--允许最大空间 filegrowth=1MB--增长值 ) log on--日志文件 ( name="Fuxi_log", filename="E:/数据库/复习/Fuxi_log.ldf", size=1MB, maxsize=unlimited, filegrowth=10% ) --.mdf主文件 --.ldf日志文件 --.ndf次要文件

    --创建主表 create table maintable ( userid int identity(1,1),--标识列 username varchar(50) not null,--非空 usersex bit not null, useraddress varchar(50) null ) --修改userid为主键 alter table maintable add constraint PK_userid primary key(userid)

    --创建附表 create table futable ( id int identity(1,1), useremail varchar(50) null ) --修改id为附键 对应主键为maintable中的userid列 alter table futable add constraint FK_id foreign key(id) references maintable(userid)

    --添加列 alter table maintable add userpassword varchar(6) not null

    --删除列 alter table maintable drop column useraddress

    --修改列默认值 alter table maintable add us varchar(50) default'上海'

    --修改列默认值 alter table maintable add Default'上海' for useraddress

    --修改列 alter table maintable alter column usersex char(5) not null

    --添加信息 insert into maintable(username,usersex,userpassword,useraddress,us) values('孙悟空','男','213',default,default)

    --添加时修改default默认值 insert into maintable(username,usersex,userpassword,useraddress,us) values('孙悟空','男','213',default,'dsf')

    --不写列名添加 insert into maintable values('白骨精','女','123','不知道','无')

    --查询 --查询真个表 select * from maintable

    --查询某列 select username as '姓名',usersex as '性别' from maintable

    --查询添加到附表中 insert futable(usex) select usersex from maintable --加上条件 --where usersex='男'

    --insert tablename(列1,列2,列3) --select '列1对应的值','列2对应的值','列3对应的值' union 继续添加 --select '列1对应的值','列2对应的值','列3对应的值' 添加结束

    --查询添加到一个新表中 无需自建新表 select username as '姓名' into new3 from maintable

    --查询添加到一个新表中 需自建新表newtable --insert into newtable(列,列) --select 列,列 --where 列='值'

    --更新附表中的email列 update futable set useremail='tear_eye@163.com' where usex='女'--不加where条件则全部更新

    --删除表 truncate table new

    --删除表new中全部数据 delete from new

    --条件删除

    delete from new3 --加where条件删除复合行 --where 姓名='猪八戒'

    --1、当主表中没有对应的记录时,不能将记录添加到子表 -- ——成绩表中不能出现在学员信息表中不存在的学号;

    --2、不能更改主表中的值而导致子表中的记录孤立 -- ——把学员信息表中的学号改变了,学员成绩表中的学号也应当随之改变;

    --3、子表存在与主表对应的记录,不能从主表中删除该行 -- ——不能把有成绩的学员删除了

    --4、删除主表前,先删子表 -- ——先删学员成绩表、后删除学员信息表

    --高级查询

    --分组查询 --查询最大/小值 select max(userid) as 最大编号 from maintable select min(userid) as 最小编号 from maintable --计算表中成员个数 select count(*) as 行数 from maintable --求平均值 select avg(userid) as 平均值 from maintable --求和 select sum(userid) as 求和 from maintable --平均值按性别分类 select usersex,avg(userid) from maintable group by usersex --Having条件 根据group得到的结果再次筛选 select usersex,avg(userid) from maintable group by usersex having avg(userid)=4

    --连接查询

    --等值连接和不等值连接 select a.username as '姓名',b.useremail as '邮箱' from maintable as a,futable as b where a.userid=b.id -->,<,=,<>,!=,!>,!<,>=,<=

    --连接 --inner join 内连接 --left join 左连接 以左边表为准 --right join 又连接 以右边表为准 --full join 完全连接 --cross join 交叉连接 不加on条件 --on 后跟连接条件 select a.username,b.useremail from maintable as a right join futable as b on a.userid<>b.id

    --子查询 select * from maintable where userid=(select max(id) from futable)--附表中id最大的对应的主表中信息

    --多个连续的值 select * from maintable where userid between 2 and 6

    --多个不连续的值 select * from maintable where userid in (2,6)

    --查询到的重复信息只显示一条 select distinct username from maintable where userid between 2 and 6

    --exists查询结果中是否有数据存在 if exists(select * from maintable where username='白骨精') insert into maintable values('存在','男','dddd',default,default)

    --like查询 select * from maintable where userpassword like '%1%'   --查询空值 --select 列 is null 非空 select 列 is not null

    --查询前几条数据 select top 2 * from maintable --查询前百分之二十 超过平均的值一点就显示下一条 select top 21 percent * from maintable

    --排序查询 select * from maintable order by userid --默认升序ASC select * from maintable order by userid desc --降序显示

    select * from maintable select * from futable select * from new3

    最新回复(0)