目录
建表sql语句(需要自己插数据)
一,增加数据
1,插入单条数据
2,插入多条数据
二,修改数据
1,修改单列
修改刘德华的密码为123456
2,修改多列
修改小红的性别为女,年龄为30
三,删除数据
1,删除用户编号为3的用户信息
2,删除用户表中的所有数据
3,删除用户的表结构
四,查询数据
1,查询所有
查询所有用户
2,查询列
查看用户表中所有用户的用户名及密码
3,查询并起别名
查看用户表中所有用户的用户名及密码并起别名
4,where条件查询
在登记表中查所有住在101的客户
在客房表中查找所有的空房间(where后面使用and连接)
查询刘德华的密码
5,排序查询(order by)
客房表按入住次数排降序
客房表按入住次数排升序
登记表中按房间号升序和入住时间降序
6,模糊查询(like)
查询姓张的用户(第一位为张)
查询姓名中有‘来’字的用户
查询姓名中第二字为‘来’的用户( 使用 _ 占位符)
7.范围查询(=,!=,>,>=,<,<=,between and)
查询年龄大于28岁的用户
查询年龄等于28岁的用户
查询年龄在38到50岁之间的用户姓名
8,按条件查询显示前或后几条数据
客户表按入住次数排降序,但只显示前三甲
客户表按入住次数排升序,但只显示前三甲
9,空值查询(is null / is not null)
查询所有姓名为空的客户信息
查询所有姓名不为空的客户信息
建表sql语句(需要自己插数据)
create table tb_users(
uid int primary key identity(1,1),
uname varchar(20) not null,
upwd varchar(16) default('888888') not null,
usex varchar(2) default('男') not null,
uage int check(uage>=0 and uage<=150) not null,
usf varchar(20) not null
)
create table tb_type(
tid int primary key identity(1,1),
tname varchar(20) not null,
tmeu varchar(20) null
)
create table tb_kefang(
fid int primary key identity(1,1),
tid int foreign key references tb_type(tid) not null,
fprice int not null,
fkzrs int not null,
fyzrs int default(0) not null,
fcnt int default(0) not null,
fmenu varchar(100) not null
)
create table tb_dengji(
did int primary key,
dname varchar(20) not null,
dfid int foreign key references tb_kefang(fid) not null,
dsfz varchar(18) not null,
dyj float not null,
dtf date null
)
一,增加数据
1,插入单条数据
语法:insert into 表名 values(列对应的值)
insert into tb_users values('张三','123','男',18 ,'学生')
2,插入多条数据
语法:insert into 表名
select 列对应的值 union
select 列对应的值 union
select 列对应的值
insert into tb_users
select '李四','123','女',20,'管理员' union
select '王五','123','男',28,'超级管理员' union
select '马六','123','男',44,'保洁'union
select '小红','123','男',24,'前台'union
select '小黄','123','男',38,'管理员'
二,修改数据
语法:update 表名 set 列名=修改后的值 where 条件
1,修改单列
修改刘德华的密码为123456
update tb_users set upwd='123456' where uname='刘德华'
2,修改多列
修改小红的性别为女,年龄为30
update tb_users set usex='女',uage=30 where uname='小红'
注意:修改时后面不带where条件会将整列数据全部修改!!!
三,删除数据
语法:delete from 表名 where 条件
1,删除用户编号为3的用户信息
delete from tb_users where uid=3
2,删除用户表中的所有数据
有主外键关系,要先删除从表数据才能删除主表数据
delete from tb_users
3,删除用户的表结构
有主外键关系,要先删除从表才能删除主表
drop table tb_users
四,查询数据
1,查询所有
语法:select * from 表名 ( * 代表所有字段 )
查询所有用户
select * from tb_users
2,查询列
语法:select 列字段名 from 表名
查看用户表中所有用户的用户名及密码
select uname,upwd from tb_users
3,查询并起别名
语法:select 列字段名 别名 from 表名(用空格起别名)
select 列字段名=别名 from 表名(用=起别名)
select 列字段名as别名 from 表名(用as起别名)
查看用户表中所有用户的用户名及密码并起别名
select uname 用户名,upwd 密码 from tb_users
select uname=用户名,upwd=密码 from tb_users
select unameas用户名,upwdas密码 from tb_users
4,where条件查询
语法:select 列字段名 from 表名 where 条件句
在登记表中查所有住在101的客户
select * from tb_dengji where dfid=101
在客房表中查找所有的空房间(where后面使用and连接)
select fid from tb_kefang where fkzrs!=0 and fyzrs=0
查询刘德华的密码
select upwd from tb_users where uname='刘德华'
5,排序查询(order by)
语法:select 列字段名 from 表名 where 条件句 order by 字段名 asc/desc(数据库默认为升序)
客房表按入住次数排降序
select * from tb_kefang order by fcnt desc
客房表按入住次数排升序
数据库默认为升序,order by 列字段名 asc 可省略不写
select * from tb_kefang order by fcnt asc
select * from tb_kefang
登记表中按房间号升序和入住时间降序
必须使用逗号连接,不能用and连接
select * from tb_dengji order by dfid asc,drz desc
6,模糊查询(like)
语法:select 列字段名 from 表名 where 字段名 like 条件
查询姓张的用户(第一位为张)
select * from tb_users where uname like '张%'
select * from tb_users where left(uname,1)='张'(取左边第一位为张)
查询姓名中有‘来’字的用户
select * from tb_users where uname like '%来%'
查询姓名中第二字为‘来’的用户( 使用 _ 占位符)
select * from tb_users where uname like '_来%'
7.范围查询(=,!=,>,>=,<,<=,between and)
语法:select 列字段名 from 表名 where 字段名 条件
查询年龄大于28岁的用户
select * from tb_users where uage>28
查询年龄等于28岁的用户
select * from tb_users where uage=28
查询年龄在38到50岁之间的用户姓名
select uname from tb_users where uage>=38 and uage<=50
select uname from tb_users where uage between 38 and 50
8,按条件查询显示前或后几条数据
语法:select top 要显示的数据条数 * from 表名 where 条件 order by asc/desc
客户表按入住次数排降序,但只显示前三甲
select top 3 * from tb_kefang order by fcnt desc
客户表按入住次数排升序,但只显示前三甲
select top 3 * from tb_kefang order by fcnt asc
select top 3 * from tb_kefang
9,空值查询(is null / is not null)
语法:语法:select * from 表名 where 列字段 is null / is not null
查询所有姓名为空的客户信息
select * from tb_users where uname is null
查询所有姓名不为空的客户信息
select * from tb_users where uname is not null