当前位置:首页 > 站长知识 > 数据库 > 正文内容

SQL Server数据库表格操作

2024-11-29数据库75

表格的创建

代码操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- StudentTwo 库名
use StudentTwo
go
 
-- table 表-- database 数据库 存放表-- 先判断表是否存在,如果存在先删除再创建
-- sysobjects 表-- 判断所有系统表有没有一个表名字为number,如果有删除掉
if exists(select * from sysobjects where name = 'nunber')
drop table number --删除掉
go
 
create table XueShengTable(
    -- 学生ID 整形谁 identity 标识符作用从1000开始 每次递增1 ,以后添加学生信息时候不要标识列添加
    StudentId int identity(1000,1),
 
    -- 姓名
    StudentName varchar(20) not null,
 
    -- 性别
    Gender char(2) not null,
)
注意: 列后面不加 not null,加null是当前列可为空,加not null是当前列不可为空

界面操作

点击数据库 --> 点开使用的数据看 --> 右键击表 --> 新建表

增删改查

解释:

-- 插入、增加学生

-- insert into 表名(列名) values(值)

-- 向哪个表中插入那一列对应值是什么

-- 注意: 列名之间使用逗号隔开,值和列名一一对应,类型也得匹配

写法1:

1
insert into XueShengTable(StudentName,Gender,Binrthday,Age,Phone,StudentAddress,ClassId,StudentCard) values('迪迦','男','2000-09-18',14,'15377300008','河南省南阳市邓州市',2341,12345678910987)

写法2:

-- 也可以将列名省略

1
insert into XueShengTable values('凹凸曼','男','2000-09-18',12345678910987,23,'15377300008','河南省南阳市邓州市',1)

-- 语法

delete from 表名 where 条件

方法1delete: 

-- 把学号1001数据删除

delete from XueShengTable where StudentId = 1001

-- 第二种删除方案 truncate:

truncate table XueShengTable -- 删除整个表格

-- 小提示: delete删除的时候比truncate删除的更快 -- delete删除的时候 不允许这条记录被外键引用,可以先把外键的关系删除掉,再进行删除这条数据 -- truncate 要求删除的表不能有外键的约束

-- 语法:

updaste 表名 set 列名 = 值, 列名 = 值 where 条件

--修改学号为1000学生的姓名改为李白,出生年月

1
update XueShengTable set StudentName = '李白',Binrthday='1975-01-01' where StudentId=1000

-- 查询所有信息 类似于数组遍历

1
select * from XueShengTable

-- 查询具体列的数据查询姓名这一列 select 列名, from 表名 select StudentName,Gender from XueShengTable

-- 条件查询 : 查询年龄等于23的 select 列名, 列名 from 表名 where Age = 23

1
select StudentName from XueShengTable where Age = 23

-- 查询满足条件的所有列

1
select * from XueShengTable where Age <= 23

--逻辑运算符号 and 并且关系 相当于&&,or或者条件 相当于||;

1
select * from XueShengTable where Age>14 and Age<23