sql server2008中怎样用sql语句创建数据库和数据表
这是简单用代码实现创建数据库和数据表的sql语句,如下:
–调用系统数据库–
use master
go
/***防止你要创建的数据库同名,先把它删除掉****/
if Exists(select * from dbo.sysdatabases where name=\’TestDB\’)
begin
drop database TestDB
end
go
–创建数据库TestDB—-
create databasse TestDB
on(
–数据库的名称–
name=\’testdb_mdf\’;
–设置数据库存放的路径–
filename=\’D:\SQL\DB\testdb_mdf.mdf\’;
)
log on(
name=\’testdb_ldf\’;
filename=\’D:\SQL\DB\testdb_ldf.ldf\’;
)
go
/***创建数据库表*****/
—使用新创建的数据库–
use TestDB
go
–创建Student表–
create table Student(
–identity(1,1)自增,primary key是主键–
stuId int identity(1,1) primary key,
stuName varchar(20) not null,
stuAge int not null,
stuSex varchar(2) not null,
Address varchar(50) not null
)
go
/***数据表的添加,查询。修改、删除的操作***/
–添加操作–
insert into Student values(\’张三\’,23,\’男\’,‘广州’);
insert into Student(stuName,stuAge,stuSex,Address) values(\’李四\’,21,\’女\’,‘海南’);
–查询操作–
select * from Student;
select * from Student where stuId=\’2\’;
–修改操作–
update Student set stuAge=27 where stuId=\’1\’;
–删除操作–
delete from Student where stuId=\’2\’