SQL Server 数据库基础编程
2016-03-24 10:01
666 查看
[code]usemaster
go
[/code]
[/code]
Ø创建、删除数据库
方法1、
[code] [code]--判断是否存在该数据库,存在就删除
if(exists(select*fromsys.databaseswherename='testHome'))
dropdatabasetestHome
go
--创建数据库,设置数据库文件、日志文件保存目录
createdatabasetestHome
on(
name='testHome',
filename='c:\data\students.mdf'
)
logon(
name='testHome_log',
filename='c:\data\testHome_log.ldf'
)
go
[/code]
[/code]
方法2(设置文件大小)、
[code]
[code]if(exists(select*fromsys.databaseswherename='testHome'))
dropdatabasetestHome
go
createdatabasetestHome
--默认就属于primary主文件组,可省略
onprimary(
--数据文件的具体描述
name='testHome_data',--主数据文件的逻辑名
fileName='c:\testHome_data.mdf',--主数据文件的物理名
size=3MB,--主数据文件的初始大小
maxSize=50MB,--主数据文件增长的最大值
fileGrowth=10%--主数据文件的增长率
)
--日志文件的具体描述,各参数含义同上
logon(
name='testHome_log',
fileName='c:\testHome_log.ldf',
size=1MB,
fileGrowth=1MB
)
go
[/code]
[/code]
方法3(设置次数据文件)、
[code]
[code]if(exists(select*fromsys.databaseswherename='testHome'))
dropdatabasetestHome
go
createdatabasetestHome
--默认就属于primary主文件组,可省略
onprimary(
--数据文件的具体描述
name='testHome_data',--主数据文件的逻辑名
fileName='c:\testHome_data.mdf',--主数据文件的物理名
size=3MB,--主数据文件的初始大小
maxSize=50MB,--主数据文件增长的最大值
fileGrowth=10%--主数据文件的增长率
),
--次数据文件的具体描述
(
--数据文件的具体描述
name='testHome2_data',--主数据文件的逻辑名
fileName='c:\testHome2_data.mdf',--主数据文件的物理名
size=2MB,--主数据文件的初始大小
maxSize=50MB,--主数据文件增长的最大值
fileGrowth=10%--主数据文件的增长率
)
--日志文件的具体描述,各参数含义同上
logon(
name='testHome_log',
fileName='c:\testHome_log.ldf',
size=1MB,
fileGrowth=1MB
),
(
name='testHome2_log',
fileName='c:\testHome2_log.ldf',
size=1MB,
fileGrowth=1MB
)
go
[/code]
[/code]
Ø基本数据类型
精确数字类型
类型 | 描述 |
bigint | bigint数据类型用于整数值可能超过int数据类型支持范围的情况,范围:-2^63到2^63-1,存储空间8字节 |
int | 整数数据类型,范围在-2^31到2^31-1,存储空间4字节 |
smallint | 整数,范围在-2^15到2^15-1,存储空间2字节 |
tinyint | 范围在0到255,存储空间1字节 |
bit | 可以取值为1、0或NULL的整数数据类型,每8个bit占一个字节,16bit就2个字节,24bit就3个字节 |
decimal | 带固定精度和小数位数的数值数据类型,有效值从-10^38+1到10^38-1 |
numeric | 同上 |
money | 货币或货币值的数据类型,范围在-922,337,203,685,477.5808到922,337,203,685,477.5807 |
smallmoney | 货币类型,-214,748.3648到214,748.3647 |
类型 | 描述 |
float | 表示浮点数值数据的大致数值数据类型。浮点数据为近似值;范围-1.79E+308至-2.23E-308、0以及2.23E-308至1.79E+308 |
real | real的SQL-92同义词为float(24),范围在-3.40E+38至-1.18E-38、0以及1.18E-38至3.40E+38 |
类型 | 描述 |
datetime | 表示某天的日期和时间的数据类型,范围在1753年1月1日到9999年12月31日 |
smalldatetime | 范围在1900年1月1日到2079年6月6日 |
类型 | 描述 |
char | 固定长度或可变长度的字符数据类型,范围在范围为1至8,000字节 |
text | 最大长度为2^31-1 |
varchar | 固定长度或可变长度的字符数据类型,最大存储大小是2^31-1个字节 |
类型 | 描述 |
nchar | 字符数据类型,长度固定,在必须在1到4,000之间 |
nvarchar | 可变长度Unicode字符数据。最大存储大小为2^31-1字节 |
ntext | 长度可变的Unicode数据,最大长度为2^30-1(1,073,741,823)个字符 |
类型 | 描述 |
binary | 长度为n字节的固定长度二进制数据,范围从1到8,000的值。存储大小为n字节。 |
varbinary | 可变长度二进制数据。n可以取从1到8,000的值。最大的存储大小为2^31-1字节 |
image | 长度可变的二进制数据,从0到2^31-1(2,147,483,647)个字节 |
[code] [code]--判断某个表或对象是否存在
if(exists(select*fromsys.objectswherename='classes'))
print'存在';
go
if(exists(select*fromsys.objectswhereobject_id=object_id('student')))
print'存在';
go
if(object_id('student','U')isnotnull)
print'存在';
go
--判断该列名是否存在,如果存在就删除
if(exists(select*fromsys.columnswhereobject_id=object_id('student')andname='idCard'))
altertablestudentdropcolumnidCard
go
if(exists(select*frominformation_schema.columnswheretable_name='student'andcolumn_name='tel'))
altertablestudentdropcolumntel
go
[/code]
[/code]
Ø创建、删除表
[code] [code]--判断是否存在当前table
if(exists(select*fromsys.objectswherename='classes'))
droptableclasses
go
createtableclasses(
idintprimarykeyidentity(1,2),
namevarchar(22)notnull,
createDatedatetimedefaultgetDate()
)
go
if(exists(select*fromsys.objectswhereobject_id=object_id('student')))
droptablestudent
go
--创建table
createtablestudent(
idintidentity(1,1)notnull,
namevarchar(20),
ageint,
sexbit,
cidint
)
go
[/code]
[/code]
Ø给表添加字段、修改字段、删除字段
[code] [code]--添加字段
altertablestudentaddaddressvarchar(50)notnull;
--修改字段
altertablestudentaltercolumnaddressvarchar(20);
--删除字段
altertablestudentdropcolumnnumber;
--添加多个字段
altertablestudent
addaddressvarchar(22),
telvarchar(11),
idCardvarchar(3);
--判断该列名是否存在,如果存在就删除
if(exists(select*fromsys.columnswhereobject_id=object_id('student')andname='idCard'))
altertablestudentdropcolumnidCard
go
if(exists(select*frominformation_schema.columnswheretable_name='student'andcolumn_name='tel'))
altertablestudentdropcolumntel
go
[/code]
[/code]
Ø添加、删除约束
[code] [code]--添加新列、约束
altertablestudent
addnumbervarchar(20)nullconstraintno_ukunique;
--增加主键
altertablestudent
addconstraintpk_idprimarykey(id);
--添加外键约束
altertablestudent
addconstraintfk_cidforeignkey(cid)referencesclasses(id)
go
--添加唯一约束
altertablestudent
addconstraintname_ukunique(name);
--添加check约束
altertablestudentwithnocheck
addconstraintcheck_agecheck(age>1);
altertablestudent
addconstraintck_agecheck(age>=15andage<=50)
--添加默认约束
altertablestudent
addconstraintsex_defdefault1forsex;
--添加一个包含默认值可以为空的列
altertablestudent
addcreateDatesmalldatetimenull
constraintcreateDate_defdefaultgetDate()withvalues;
-----多个列、约束一起创建--------
altertablestudentadd
/*添加id主键、自增*/
idintidentityconstraintidprimarykey,
/*添加外键约束*/
numberintnull
constraintuNumberreferencesclasses(number),
/*默认约束*/
createDatedecimal(3,3)
constraintcreateDatedefault2010-6-1
go
--删除约束
altertablestudentdropconstraintno_uk;
[/code]
[/code]
Ø插入数据
[code]
[code]insertintoclasses(name)values('1班');
insertintoclassesvalues('2班','2011-06-15');
insertintoclasses(name)values('3班');
insertintoclassesvalues('4班',default);
insertintostudentvalues('zhangsan',22,1,1);
insertintostudentvalues('lisi',25,0,1);
insertintostudentvalues('wangwu',24,1,3);
insertintostudentvalues('zhaoliu',23,0,3);
insertintostudentvalues('mazi',21,1,5);
insertintostudentvalues('wangmazi',28,0,5);
insertintostudentvalues('jason',null,0,5);
insertintostudentvalues(null,null,0,5);
insertintostudent
select'bulise'name,age,sex,cid
fromstudent
wherename='tony';
--多条记录同时插入
insertintostudent
select'jack',23,1,5union
select'tom',24,0,3union
select'wendy',25,1,3union
select'tony',26,0,5;
[/code]
[/code]
Ø查询、修改、删除数据
[code] [code]--查询数据
select*fromclasses;
select*fromstudent;
selectid,'bulise'name,age,sex,cidfromstudent
wherename='tony';
select*,(selectmax(age)fromstudent)fromstudent
wherename='tony';
--修改数据
updatestudentsetname='hoho',sex=1whereid=1;
--删除数据(from可省略)
deletefromstudentwhereid=1;
[/code]
[/code]
Ø备份数据、表
[code] [code]--备份、复制student表到stu
select*intostufromstudent;
select*intostu1from(select*fromstu)t;
select*fromstu;
select*fromstu1;
[/code]
[/code]
Ø利用存储过程查询表信息
[code] [code]--查询student相关信息
execsp_helpstudent;
execsp_helpclasses;
[/code]
[/code]
相关文章推荐
- oracle数据迁移--复制表--create table as select & select into & insert into
- Oracle--数据量增长过快策略
- oracle 复制一个表数据 到另一个表中,select into与insert into
- MongoDB中的分组
- Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的
- 从数据库表中随机获取N条记录的SQL语句
- memcached的理解与使用
- <php>过时方法连接数据库代码
- oracle EBS采购订单各表作用分析
- hsql错误: user lacks privilege or object not found: LAST_INSERT_ID
- elasticsearch 多列 聚合 归类(类似 sql group by)
- 手动修改SQLITE数据库的版本
- Oracle session inactive状态临时表数据未清空问题
- Oracle生产固定GUID同时生成随机的GUID
- Mysql数据库中图片字段Blob类型和String类型相互转换
- MySql 里的IFNULL、NULLIF和ISNULL
- 防SQL注入过滤器的实现
- cmd中mysql常用操作
- 青云QingCloud RDS 服务率先支持 MySQL 5.7
- MyBatis的分页操作(MySQL)