--新建数据库--
create database T_datadase;
--删除数据库--
drop database T_datadase;
go
--打开数据库--
use T_datadase;
--新建数据表--
create table T_datatable(IdCard int primary key identity(1,1),Name nvarchar(50),Sex bit,Age int,ClassID int);
go
--插入一行数据--
insert into T_datatable values('康凯',1,20,3);
--差入多数据--
insert into T_datatable
select '刘备',1,40,2 union
select '关羽',1,38,1 union
select '张飞',1,36,4;
--删除数据--
delete T_datatable where Name='康凯'--可以删除指定行的数据单无法删除种子变量
truncate table T_datatable --不可定向删除清除表同时清除种子变量
--修改数据--
update T_datatable set ClassID=3 where Name='张飞';
--查找数据--
select * from T_datatable;
select * from T_datatable where Age>38;
select * from T_datatable where Name like '张%';--通配符查询
select GETDATE();-------------------------------------------日期函数
select DATEADD(day,5,getdate());
select DATEDIFF(YEAR,2012-11-4,GETDATE());
select DATENAME(day,getdate());------------------------------
select convert(nvarchar(50),age) from T_datatable
select CAST(age in nvarchar(50))
select ISNULL(
--copy表数据--
select * into T_copy2 from T_datatable;--copy表T_datatable数据到T_copy1 前提无T_copy这个表 方法一
select TOP 0 * into T_copy3 from T_datatable;--copy表数据结构 方法1
select * into T_copy4 from T_datatable where 1<>1;--copy表数据结构 方法2
set IDENTITY_INSERT copy on;--随开
insert into T_copy2(Name,Sex,Age,ClassID)-------------copy表 前提有T_copy2这个表 方法二
select Name,Sex,Age,ClassID from T_datatable--不可以用*代替
set IDENTITY_INSERT copy off;--随关
go
--删除数据表--
drop table T_datatable;
go
--修改数据表结构--
alter table T_datatable alter column ClassID int not null;--修改表属性--
alter table t_datatable add Math float not null;--添加一成绩列--
alter table T_datatable add constraint CK_T_datatable_Age check(Age>=16 and Age<=28)--设置条件约束--
alter table T_datatable drop constraint CK_T_datatable_Age;--删除约束--
go