use master --选择需要操作的数据库 master:系统数据库,记录了SQL server系统的所有的系统级信息,还记录了所有其他数据库的存在,数据库文件的位置,SQLserver的初始化信息
go --批处理命令
--创建新数据库
create database TestNewDB --数据库名称
on primary --主文件组
(
name='TestNewDB', --数据库主要数据文件的逻辑名
filename='C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TestNewDB.mdf', --主要数据文件的绝对路径
size=5MB, --数据库文件的初始大小
filegrowth=1MB --数据库文件的增量
)
log on --创建日志文件
(
name='TestNewDB_log', --数据库日志文件的逻辑名
filename='C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\TestNewDB_log.ldf', --日志问文件的绝对路径
size=1MB, --数据库日志文件的初始大小
filegrowth=10% --文件的增量
)
go
--删除数据库
drop database TestNewDB
use TestNewDB
go
--数据定义语言 DDL
--新增表
create table ProductInfos
(
Id int identity(1002,1) primary key not null, --参数1:标识种子 参数2:增量
ProNo nvarchar(50) not null,
ProName nvarchar(50) not null,
TypeId int not null,
Price decimal(18,2) default 0.00 null,
ProCount int default 0 null,
)
go
create table ProductType
(
TypeId int identity(1,1) primary key not null,
TypeName nvarchar(20) not null
)
go
-- 删除表
drop table ProductInfos
go
-- 修改表,添加一列
alter table ProductInfos add ProRemark nvarchar(max) null
go
--删除一列
alter table ProductInfos drop ProRemark
go
-- 修改一列
alter table ProductInfos alter column ProNo nvarchar(50) null
-- 修改列名 --注意: 更改对象名的任一部分都可能会破坏脚本和存储过程。
-- exec sp_rename 'ProductInfos.ProCount','Count','column'
--修改主键约束
alter table ProductInfos add constraint PK_ProductInfos primary key(Id)
--修改外键约束
alter table ProductInfos add constraint FK_ProductInfos foreign key(TypeId) references ProductType(TypeId)
--唯一约束 unique
alter table ProductInfos add constraint IX_ProductInfos_ProNo unique(ProNo)
-- check 约束
alter table ProductInfos add constraint Ck_ProductInfos_Price check(Price<1000);
--default 约束
alter table ProductInfos add constraint DF_ProductInfos_Count default(0) for Count;
--插入数据
--单条数据插入
--1)insert into 表名(列名...)values (值...)
insert into ProductType(TypeName) values('服装类')
--2) insert into 表名(列名,列名...) select 值,值...
insert into ProductType(TypeName) select '工具类'
-- 一次性插入多条数据
insert into ProductType(TypeName) values('手机类'),('摩托车类')
insert into ProductType(TypeName)
select '笔记本' union
select '袜子' union
select '袜子' --union可以去重,只有2行受影响
insert into ProductType(TypeName)
select '电饭煲' union all
select '滑板' union all
select '滑板' --union all不可以去重,3行受影响 union all比union效率更高。
--克隆 表数据
--1)目标表已存在
insert into test(tName) --目标表的列
select TypeName from ProductType --源表
--2)目标表不存在
select TypeName into test2 --目标表
from ProductType --源表
--更新数据 update
update test set tName='1233132' where id=1;
-- 删除数据 :1只删除数据 2连同表一起删除
--1)delete 删除数据
delete from test where id =8; --标识列的值还是接着删除前的值继续自增,会造成标识列的值不连续
--2)truncate
truncate table test --表数据清空,恢复到初始化,
--truncate 效率比delete高,delete没删除一条数据,都会在日志里记录。truncate不会记录日志,不激活触发器。
--drop、truncate是即时操作,不能回滚。delete update、insert 是在事务中 没commit前都能rollback回滚恢复
--查询数据
-- 查询所有列
select * from ProductType;
--查询部分列
select typename from ProductType;
-- 给列取别名
select TypeId as 编号,typename 类别, '类别'=typename from ProductType;
--排序 order by
--升序asc
select * from ProductType order by TYPEID asc;
--降序desc
select * from ProductType order by TYPEID desc;
--模糊查询 like
--1、 % 匹配0个或任意多个
--2、 _ 匹配单个字符串
--3、 [da] 匹配中括号内中的所有字符中的一个
--4、 [^adfa] 匹配不在中括号的字符中的一个
--聚合函数:对一组值执行计算并返回单一的值
--count() 当数据量大的时候count(1)比count(*) 要快的多 一般统计表的记录数都用count(1)
-- count(1) 是伪造列
select 1 from ProductType;
select count(1) from ProductType;
select count(*) from ProductType;
--5中常用的聚合函数 聚合函数一般与分组结合起来。
--count 计数
select count(1) from ProductType;
--sum求和
select sum(typeId) from ProductType;
--avg平均
select avg(typeId) from ProductType;
--max最大值
select max(typeId) from ProductType;
--min最小值
select min(typeId) from ProductType;
--分组 group by
--select..from...where...group by ... having ...limit ... order by ...
--select 后出现的列名,必须出现在group by 之后或包含在聚合函数中
--select deptId ,count(1) 用户数 from userInfos
-- where age>20
-- group by deptId
-- having deptId>1
-- limit 10
-- order by deptId desc;
--类型转换
--1)convert(目标类型,表达式)
--数字+数字 求和
select 1+1;
--字符串+字符串 拼接
select 'a'+'cs';
--字符串+数字
select 'adf'+1; --在将 varchar 值 'adf' 转换成数据类型 int 时失败。
select 'abc' + convert(varchar,2);---convert(目标类型,表达式):将原始数据转换为目标类型的数据;参数不规范可能导致转换失败。
select convert(varchar(20),getdate(),100);
select 1 + convert(int,'ad'); --在将 varchar 值 'ad' 转换成数据类型 int 时失败。
--2)cast(表达式 as 目标类型)
select cast(1232 as varchar);