SQL server期末复习(考点汇总)

1、基础数据类型

1.1、数值类型

数据类型说明
int短整型,存储范围 -2147483648~2147483647(4个字节)
tinyinttinyint 数据类型能存储从0到255 之间的整数(1个字节)
smallintsmallint 数据类型可以存储从- 2的15次幂(-32768)到2的15次幂(32767)之间的整数(2个字节)
numeric(p,s)范围:储存数字总位数,精度:指定小数点后的位数(17个字节)

1.2、字符串类型

数据类型说明
char(n)char数据类型用来存储指定长度的定长非统一编码型数据,n表示字符串的最大长度。
varchar(n)可变长度的字符串类型,n表示字符串的最大长度,取值范围为1~8000。
texttext 数据类型用来存储大量的非统一编码型字符数据。这种数据类型最多可以有231-1或20亿个字符.

2、创建数据库

create database test_db;	#创建一个test_db数据库

3、创建表

# 创建教师表(teacher)
create table teacher
(
	`id` int not null primary key identity(1,1) comment '主键自增(identity(1,1)表示从1开启每次自增1)',
    `t_name` varchar(30) not null comment '教师名称',
    `sex` char(2) comment '性别(男,女)',
    `birthday` datetime comment '出生日期',
    `title` char(10) comment '职称',
    `depart` varchar(30) comment '所属院系'
);

# 给教师表(teacher)添加列 phone_number char(11),表示教师联系电话!
alter table teacher add phone_number char(11);

# 给教师表(teacher)添加列 salary numeric(8,2),表示教师工资!
alter table teacher add salary numeric(8,2);

# 添加约束,指定title取值范围(教授、副教授、讲师、助教)!
alter table teacher add constraint title check(title in('教授', '副教授', '讲师', '助教'));

4、CRUD

4.1、插入数据

inster into teacher(`id`, `t_name`, `sex`, `birthday`, `title`, `depart`, `phone_number`, `salary`) 
values(10001, '刘日辉', '男', '1977-12-01', '副教授', '信息工程学院', '18778837831', 8888.88);

inster into teacher(`id`, `t_name`, `sex`, `birthday`, `title`, `depart`, `phone_number`, `salary`) 
values(10002, '陈老师', '女', '1989-09-06', '讲师', '信息工程学院', '18778837838', 7777.67);

4.2、查询数据

# 查询表所有数据
select * from teacher;

# 按姓名查询
select * from teacher where t_name = '刘日辉';

# 查询指定字段信息
select `t_name`, `salary` from teacher;

# 统计各类职称教师人数(group by title:按title进行分组,count(*) 统计每个组人数)
select `title`,count(*) from teacher group by title;

# 查看最高工资,最低工资,平均工资
select max(salary) as 最高工资, min(salary) 最低工资, avg(salary) 平均工资 from teacher;

4.3、删除数据

# 按ID删除
delete from teacher where id=1;

# 按名字删除
delete from teacher where t_name='刘日辉';

4.4、更新数据

# 根据教师名称更新教师电话(更新多个字段就在set后面添加多个字段信息)
update teacher set `phone_number`='18778837888' where t_name = '刘日辉';

update teacher set `phone_number`='18778837889', `salary`=9999.99 where t_name = '刘日辉';
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值