use airticket
go
-----不错的sql语句,(into 后面目标表,from后是来源表)
-----(1)只复制表结构于新创建的表中
----法一:select top 0 * into b from a ----
select top 0 * into city_Test from city ----select * from city_Test
----法二:select * into b from a where 1<>1
select * into City_Test from city where 1<>1
-----(2)复制表结构和内容到创建的新表中(但主键是不给复制的)
select * into city2 from city ----*也可以用旧表中的列名代替,列名不写全,结果就只复制写了的列名哦
-----(3)向新表中插入数据时,同时插入自己的标识列。
--------(注意此方法要在表中没有标识列时用,有就没有必要再弄自己的标识列了)
--select identity(数据类型,标识种子,标识增长量) as 列名
--into 新表
--from 原始表
drop table city3;
select identity(int,1000,1) as TestId,cityname,provinceid
into city3
from city -----select * from city3
-----查插入的新数据的标识列
select * from City_Test
insert city3(cityname,provinceid)values('泰州',4);select @@identity
-----(4)说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用):
-----【注意通过Insert into select from 语句将现有表中的数据是添加到已经存在的表中,不能复制标识列哦】
-----insert into b(a, b, c) select d,e,f from b;
insert into city_test(CityName, ProvinceId) select CityName, ProvinceId from city
----4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)根据已有的表创建新表:
------对表常用操作
----以下表做为源参考
select cityname,provinceid into CityTable from city
----a.添加新列 ---- select * from city_test
alter table citytable
add cityId int
----k.删除列
alter table tablename
drop column 列名
----u.修改列类型(要兼容数据类型)
alter table 表名
alter column 列名 类型
如
alter table city_test
alter column cityname nvarchar(40)
----b.添加主键: Alter table tabname add constraint __ primary key(col)
Alter table city_test
add constraint PK_CityID primary key(cityId)
---c.删除主键Alter table tabname drop primary key(col)
Alter table city_test drop PK_CityID ----删除所有约束都是这样方法,注意要是有外键时就不会成功
---17、说明:随机取出10条数据
----select top 10 * from tablename order by newid()
select top 5 * from city order by newid()
drop table city3 select * from city
go
-----不错的sql语句,(into 后面目标表,from后是来源表)
-----(1)只复制表结构于新创建的表中
----法一:select top 0 * into b from a ----
select top 0 * into city_Test from city ----select * from city_Test
----法二:select * into b from a where 1<>1
select * into City_Test from city where 1<>1
-----(2)复制表结构和内容到创建的新表中(但主键是不给复制的)
select * into city2 from city ----*也可以用旧表中的列名代替,列名不写全,结果就只复制写了的列名哦
-----(3)向新表中插入数据时,同时插入自己的标识列。
--------(注意此方法要在表中没有标识列时用,有就没有必要再弄自己的标识列了)
--select identity(数据类型,标识种子,标识增长量) as 列名
--into 新表
--from 原始表
drop table city3;
select identity(int,1000,1) as TestId,cityname,provinceid
into city3
from city -----select * from city3
-----查插入的新数据的标识列
select * from City_Test
insert city3(cityname,provinceid)values('泰州',4);select @@identity
-----(4)说明:拷贝表(拷贝数据,源表名:a 目标表名:b) (Access可用):
-----【注意通过Insert into select from 语句将现有表中的数据是添加到已经存在的表中,不能复制标识列哦】
-----insert into b(a, b, c) select d,e,f from b;
insert into city_test(CityName, ProvinceId) select CityName, ProvinceId from city
----4、说明:创建新表
create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)根据已有的表创建新表:
------对表常用操作
----以下表做为源参考
select cityname,provinceid into CityTable from city
----a.添加新列 ---- select * from city_test
alter table citytable
add cityId int
----k.删除列
alter table tablename
drop column 列名
----u.修改列类型(要兼容数据类型)
alter table 表名
alter column 列名 类型
如
alter table city_test
alter column cityname nvarchar(40)
----b.添加主键: Alter table tabname add constraint __ primary key(col)
Alter table city_test
add constraint PK_CityID primary key(cityId)
---c.删除主键Alter table tabname drop primary key(col)
Alter table city_test drop PK_CityID ----删除所有约束都是这样方法,注意要是有外键时就不会成功
---17、说明:随机取出10条数据
----select top 10 * from tablename order by newid()
select top 5 * from city order by newid()
drop table city3 select * from city