SQL基本操作

一数据类型
1整数型 int
2精确数值型 decimal(n,p)n为总位数,p为小数位数
3浮点型 float
4字符型char(n)n最大为4
varchar(n)
5日期型datatime
6文本型text


二基本操作
1.查找数据库的主文件和日志文件所在位置:
右击数据库,点击属性,点击文件。
2.新建数据库:
右击数据库,新建数据库,输入数据库的名称
3.用语句新建数据库:
点击新建查询,在窗口内输入语句并点击执行
create database lianxi --linxi为数据库名字
go
4.使用数据库并创建一个表,表中添加列名
use lianxi
go
create table shucai --shucai为表格的名字。
(
code int not null,--not null为不为空。
name varchar(20) not null,
price decimal(18,2),
chandi varchar(10),
shuliang decimal(18,2),
)
go
5添加数据
insert into shucai values(1,'白菜',0.5,'淄博',100)
insert into shucai values(2,'萝卜',2,'滨州',200)
insert into shucai values(3,'芹菜',0.8,'淄博',300)
insert into shucai values(4,'蒜',5,'滨州',500)
insert into shucai values(5,'油菜',0.6,'烟台',600)
insert into shucai values(6,'青鱼',4,'淄博',800)
insert into shucai values(7,'土豆',0.9,'淄博',700)
go
6查找数据
select *from shucai --查找所有数据
select top 3 *from shucai--查找前3行数据
select *from shucai where code='2' --查找code为2的所有信息
select name,price from shucai where code='3'--查找code为3的 name和price信息
select price from shucai where name='白菜'--查找name为白菜的 price信息
select name,price from shucai --查找表格的name和price列
7修改数据
update shucai set price='1' where code='7'--修改code为7的价格
update shucai set price='5' where name='青鱼'--修改name为青鱼的价格

8删除数据
delete from shucai where code='2' --删除code为2的这一行信息。

三数据库文件:
主文件 存储数据、启动信息 必须有且只能有一个.mdf

次要文件 存储主文件未存储的数 可有可无、可多可少 .ndf

日志文件 记录操作信息 必须有且至少有一个.ldf

四运行cmd

输入net start MSSQLserver 启动数据库服务

输入net stop MSSQLserver 关闭数据库服务