SQL Server 学习笔记 第一篇

use master
go
if exists(select * from sysdatabases where name ='stuDB')
drop database stuDB
create database stuDB on primary
(
 name='stuDB_data',--主数据文件的逻辑名
 filename='E:\project\stuDB_data.mdf',--文件物理名
 size=5mb,
 maxsize=100mb,-- 数据文件增长的最大值
 filegrowth=15%
)
log on
(
 name='stuDB_log',
 filename='E:\project\stiDB_data.ldf',
 size=2mb,
 filegrowth=1mb
)
go
use stuDB
if exists(select * from sysobjects where name='stuInfo')
drop table stuInfo
create table stuInfo /*创建学员信息表**/
(
 stuName varchar(20) not null,-- 姓名,非空
 stuNo char(6) not null,-- 学号,非空
 stuAge int not null,-- 年齡,int 默认为4个长度
 stuId numeric(18,0),
 stuSeat smallint ,-- 坐位车,使用自增
 stuAddress text -- 住址 可以为空
)
-- 给stuInfo添加一列
alter table stuInfo add id int identity(1,1) primary key;
-- 以下是给stuInfo 添加约束条件
alter table stuInfo add constraint PK_stuNo primary key(stuNo); -- 添加主键约束
alter table stuInfo add constraint UQ_stuId unique(stuId);-- 添加唯一约束
alter table stuinfo add constraint DF_stuAddress default('地址不详') for stuAddress;-- 添加默认约束
alter table stuInfo add constraint CK_stuAge check (stuAge between 5 and 35); -- 添加check 约束
-- 删除约束
alter table stuInfo drop constraint PK_stuNo
-- 插入数据
insert into stuInfo (stuName,stuNo,stuAge,stuId,stuAddress) values('张三','123456',25,'123456789012345678',default)
insert into stuinfo values('李四','112345',12,'123456789012345687',2,'北京')
-- 给新建的库建立用户名
--添加sql 登陆帐号 用户使用此用户名/密码连接数据库
exec sp_addlogin 'zhangsan','123456'
exec sp_addlogin 'liucheng','123456'
go
-- 创建数据库用户
exec sp_grantdbaccess 'zhangsan','zhangsanDBUser'
exec sp_grantdbaccess 'liucheng','liuchengDBUser'
-- 授权
grant select,insert,update,delete on stuDB to liuchengDBUser
grant select,insert,update on stuInfo to zhangsanDBUser
-- 回收权限
revoke insert on stuInfo  from  zhangsanDBUser

-- T-SQL 使用
declare @name varchar(8)
set @name='李四'
select * from stuInfo where stuName=@name;
declare @seat int
select @seat = stuSeat from stuInfo where stuName=@name
select * from stuInfo where (stuSeat=@seat+1) or (stuSeat=@seat-1)
-- 全局变量
select @@language,@@max_connections,@@rowcount,@@servername,@@version;
print 'sql server 的版本'+@@version
print getDate();
print  '当前错误'+convert(varchar(5),@@error);
select top 2 * from stuInfo order by id desc;
-- 创建索引 在经常查询的列
create index IX_stuName on stuInfo (stuName)
-- 创建视图
create view VIEW_stuInfo as select stuName,stuAge,stuId from stuInfo
grant select on VIEW_stuInfo to zhangsanDBUser
select *  from VIEW_stuInfo where  stuName='张三'
-- 注在java 中查询视图时,返回的是一个Ojbect[],如果查询是一个List ,则返回的是List<Object[]>

-- 存储过程
exec sp_databases; -- 列出当前系统中的数据库



在上边的学习过程中遇到了一些问题

 在建立用户名和密码后使用java程序连接数据库时。com.microsoft.sqlserver.jdbc.SQLServerException: 到主机 的 TCP/IP 连接失败。

原因是我在装完 sql server 2005 后1433 端口设置处为空。使用ser server configuration manager 重新设置一下就ok了。

SQL Server 给字段修改长度。

 alter table "jeecms"."dbo"."CARD_TO_CARD"  alter COLUMN  OUTPUTCARDNO varchar(19);

删除表中的某一列

alter table 表名 drop column name

 sql server 数据库备份与恢复


这个是手动的数据备份。

SQL Server 2005 修改默认端口

  sql server 2005 默认使用1433端口,有时需要修改。直接上图吧。



原文地址:https://www.cnblogs.com/java20130726/p/3218372.html