SQL学习
- 格式:docx
- 大小:16.21 KB
- 文档页数:5
数据库: 1. 创建数据库 Create database database-name 例子1: 创建一个数据库sample,主要数据文件sample_dat CREATE DATABASE sample ON PRIMARY (NAME=sample_dat, FILENAME=’d:\SQL\sample_data.mdf’, SIZE=5, MAXSIZE=50, FILEGROWTH=10) 说明:数据文件的位置为d:\SQL\sample_data.mdf,默认容量单位为MB, 主要数据文件的初始容量为5MB, 最大容量为50MB,增幅为10MB. 由于在创建时没有指定日志文件, 因此系统将自动创建一个初始容量为1.25MB的日志文件并没有最大容量限制 例子2 创建一个只包含一个数据库文件和一个日志文件的数据库, 数据库名为sales, 数据文件的逻辑文件名为sales_data,数据文件的操作系统名为sales_data.mdf, 初始大小为10MB, 最大可增至500MB, 增幅为10%;日志文件的逻辑名为sales_log,操作系统名为sales_log.ldf, 初始大小为5MB, 最大值为100MB, 日志文件大小为2MB增幅增加 CREATE DATABASE sales ON (NAME=sale_data, FILENAME=’d:\SQL\sale_data.mdf’, SIZE=10MB, MAXSIZE=500MB, FILEGROWTH=10%) LOG ON (NAME=sale_log, FILENAME=’d:\SQL\sales_log.ldf’, SIZE=5MB, MAXSIZE=100MB, FILEGROWTH=2MB) 说明:由于省略了PRIMARY关键字, 因此系统默认第一个文件sale_data.mdf为主要数据文件 例子3 创建一个包含多个数据文件和日志的数据库, 该数据库名为student含有三个初始大小为10MB为数据文件和两个8MB的日志文件 CREATE DATABASE student on PRIMARY (NAME=std_dat1, FILENAME=’d:\SQL\student1.mdf’, SIZE=10MB, MAXSIZE=200MB, FILEGROWTH=20), (NAME=std_dat2, FILENAME=’d:\SQL\student2.ndf’, SIZE=10MB, MAXSIZE=200MB, FILEGROWTH=20), (NAME=std_dat3, FILENAME=’d:\SQL\student3.ndf’, SIZE=10MB, MAXSIZE=200MB, FILEGROWTH=20) LOG ON (NAME=std_log1, FILENAME=’d:\SQL\stdlog.ldf’, SIZE=8MB, MAXSIZE=100MB, FILEGROWTH=10MB) 说明: 在filename选项中所用的文件扩展名,主要数据文件使用.mdf, 次要数据文件使用.ndf,日志文件使用.ldf 例子4 创建一个包含两个文件组的数据库, 该数据库名为business, 主文件组包含bussiness_dat1和business_dat2两个数据文件, 文件组business_group包含数据文件business_dat3, 改数据库还好汉一个日志文件bussiness_log
CREATE DATABASE business ON PRIMARY (NAME=business_dat1, FILENAME=’d:\SQL\bussiness_dat1.mdf’, SIZE=10MB, MAXSIZE=50MB, FILEGROWTH=10), (NAME=business_dat2, FILENAME=’d:\SQL\bussiness_dat2.ndf’, SIZE=10MB, MAXSIZE=50MB, FILEGROWTH=10), FILEGROUP business_group (NAME=business_dat3, FILENAME=d:\SQL\ bussiness_dat3.ndf’’, SIZE=10MB, MAXSIZE=50MB, FILEGROWTH=10) LOG ON (NAME=business.log FILENAME=’d:\SQL\bussinesslog.ldf’, SIZE=8MB, MAXSIZE=100MB, FILEGROWTH=10MB) 说明:当FILEGROWTH没有单位的时候表示增幅是按百分比计算
2. 删除数据库 Drop database database-name 表 1. 创建新表 Create table table-name(column1 type1 [not null] [primary key], column2 type2 [not null], …) 例子: Create table student(ID varchar(14) primary key, name varchar(8)…) 2. 根据已有表创建表 Create table new-table-name like old-table-name Create table new-table-name as select column1, column2… from old-table definition only 3. 删除新表 drop table table-name 4. 增加一列 Alter table table-name add column column-name type 5. 添加主键 Alter table table-name add primary key(column-name) 6. 删除主键 Alter table table-name drop primary key(column-name) 7. 创建索引 Create [unique] index id-name on table-name(column-name) 8. 删除索引 Drop index index-name 9. 创建视图 Create view view-name as select statement 10. 删除视图 Drop view view-name 基本语句 1. 选择语句 Select * from table-name where… 2. 插入语句 Insert into table-name(field1, filed2) values (value1, value2) 例如:Insert into table student values (1, ‘name1’) 3. 删除 Delete from table1 where… 4. 更新 Update table1 set field1=value1 where 5. 查找 Select * from table1 where field1 like ‘%value1%’ 6. 排序 Select * from table order by filed1, feild2 [desc/asc] 7. 总数 Select * as totalcount from table1 8. 求和 Select sum(field1) as sumvalue from table1 9. 平均 Select avg(field) as avgvalue from table1 10. 最大 Select max(filed) as maxvalue from table1 11. 最小 Select min(filed) as minvalue from table1 特殊语句 1. Union 组合其他两个结果表(table1
和table2),并消去表中任何重复行而派生出来的一个结果被 2. Union all 不消除重复行 3. Except 包括所有在table1中但不在table2中的行并消除所有重复行而派生出一个结果表 4. Except All 不消除重复行 5. Intersect 包括table1和table2中都有的行并消除所有重复行而派生出一个结果表 6. Intersect all 不消除重复行 7. 左连接 left outer join 结果集包括连接表的匹配行, 也包括左连接表的所有行 例如: select a.a, a.b, a.c, b.c, b.d, b.f from a left out join b on a.a=b.c 8. 右连接(right outer join) 结果集包括连接表的匹配连接行, 也包括右连接表中的所有行 9. 全外连接(full outer join) 不仅包括符号连接表的匹配行,还包括两个连接表中的所有记录 10.复制表(复制表结构) select * into b from a where 1<>1 select top 0 * into b from a 11. 拷贝表(拷贝数据) Insert into b(a,b,c) select d,e,f from b; 12.跨数据库的拷贝