数据库基本语法

上传人:夏** 文档编号:501464724 上传时间:2022-08-11 格式:DOCX 页数:15 大小:17.61KB
返回 下载 相关 举报
数据库基本语法_第1页
第1页 / 共15页
数据库基本语法_第2页
第2页 / 共15页
数据库基本语法_第3页
第3页 / 共15页
数据库基本语法_第4页
第4页 / 共15页
数据库基本语法_第5页
第5页 / 共15页
点击查看更多>>
资源描述

《数据库基本语法》由会员分享,可在线阅读,更多相关《数据库基本语法(15页珍藏版)》请在金锄头文库上搜索。

1、创建表create table 表名(列名 1 数据类型, 列名 2 数据类型)create table 表名(列名 1 数据类型 primary key, 列名 2 数据类型)create table 表名(列名 1 数据类型 unique, 列名 2 数据类型)create table 表名(列名 1 数据类型 not null, 列名 2 数据类型)修改表列添加 alter table 表名 add 列名数据类型列删除 alter table 表名 alter column 列名数据类型列修改 alter table 表名 drop column 列名列的重命名exec sp_renam

2、e 表名旧列名,新列名,column数据插入insert into 表名 values(对应值)注意:字符,日期数据加单引号数据更新update 表名 set 列名 1=值,列名 2=值 where 条件create table tbl1col1int,col2int)insert into tbl1 values(1,2)select *from tbl1col1 10col2 col1+20update tbl1set col1 = 10,col2 = col1+20select * from tbl1col1 10col2 21 还是 30数据删除delete from 表名 where

3、 条件数据查询SELECT DISTINCT TOP n select_listINTO new_table_nameFROM table_listWHERE search_conditionsGROUP BY ALL group_by_listHAVING search_conditionsORDER BY order_list ASC | DESC - 2 - 最简单的查询语句- 2.1 - 使用 select 语句查询表中的数据- SELECT * FROM table_name use pubs - 切换当前数据库select * from authors- 2.2 - 使用完全限定名

4、称- SELECT * FROM server_name.database_name.owner.object select * from Northwind.dbo.Employeesselect * from Northwind.Orders- 2.3 - 查询数据表中指定的列- SELECT column_1,column_2 FROM table_nameselect * from authorsselectau_id,au_lname,au_fname,city from authors- 2.4 - 使用 where 字句筛选指定的行- SELECT * FROM table_na

5、me WHERE select * from jobsselect * from jobs where job_id=7select * from authorsselect * from authors where au_lname = White- 2.5- where 字句中的搜索条件比较操作符=, , =, 字符串比较符like, not like逻辑操作符and, or, not值的域between, not between值的列表in, not in未知的值is null, is not nullselect * from jobs where job_id=10select *

6、from jobs where job_desc like %manager%select * from jobs where job_id=10 AND job_desc like %manager%select * from jobs where min_lvl between 100 and 150select * from jobs where job_id in (1,3,5,7,11,13)select * from discounts where stor_id is null- 2.6 - 使用 like- % 代表 0 个或多个字符串- _ 代表任何单个的字符- 代表指定区域

7、内的任何单个字符-A代表不在指定区域内的任何单个字符 select * from authors where au_lname like G%select * from authors where address like %Av.%select * from authors where au_fname like A_select * from authors where au_fname like AS%select * from authors where au_lname like SAm%- 2.7 - 格式化结果集- order by 排序结果集- distinct 消除重复的行-

8、 as改变字段的名字select * from employee order by fnameselect * from employee order by hire_datedescselect distinct type from titlesselectau_id,au_lname as Last Name,au_fname as First Name from authors- 练习 - 1 查询本数据库服务器上有哪些数据库- 2 查询 Northwind 数据库中 Products 数据表中 ProductID 为 14 的 产品信息- 3 查询 Northwind 数据库中 Ord

9、ers 数据表中没有按时送达的订单- 4 查询 Pubs 数据库中 Titles 数据表中书名含有 computer 的书 并按照价格降序排序- 5 查询 Northwind 数据库中 Territories 数据表中的地域信息,按照 地域名升序排序,去掉重复项创建视图create view 视图名 as select 语句创建索引create index 索引名 on 表名(列名)create unique clustered|nonclustered index 索引名 on 表名(列 名)创建存储过程create procedure 存储过程名 as 存储过程定义语句创建触发器creat

10、e trigger 触发器名 on 表名 for 操作 as 触发器定义语句注意: 操作:insert delete update相应对象的删除删除表drop table 表名删除索引drop index 表名.索引名删除视图drop view 视图名删除存储过程 drop procedure 存储过程名删除触发器drop trigger 触发器名例子:1.创建表emp,字段e_id,类型为int;e_name类型为varchar,长度为10;salary 类型为 int,d_id 类型为 intcreate table emp (e_idint,e_namevarchar(10),salar

11、y int,d_idint)2创建表dep,字段d_id,类型为int;d_name类型为char,长度为20; create table dep (d_idint,d_name char(20)3在表dep中添加字段memo,类型为char,长度为200;alter table dep add memo char(200)4修改表dep中d_name字段的字段名为dep_name;sp_rename dep.d_name,dep_name,column5删除表dep中的memo字段;alter table dep drop column memo6修改表dep中字段dep_name的长度为1

12、0;alter table dep alter column dep_name char(10)sp_helpdep7向表emp中插入数据如下:e_ide_name salary d_id1TOM200012ROSE100013Jerry300024Jack60002Mike4500insert into emp values(1,TOM,2000,1)insert into emp values(2,ROSE,1000,1)insert into emp values(3,Jerry,3000,2)insert into emp values(4,Jack,6000,2)insert int

13、o emp values(5,Mike,4500,1)7.向表 dep 中插入数据如下: d_iddep_name1r&d2h&rinsert into dep values(1,r&d)insert into dep values(2,h&r)select * from dep8.查询表 emp 中的所有数据记录;select * from emp9.查询表 emp 中的 e_id,e_name 和 salary; selecte_id,e_name,salary from emp10.查询表 emp 中的所有数据,要求按照 salary 降序排列;select * from emp ord

14、er by salary desc11.查询表 emp 中 e_id 为 5 的数据记录;select * from emp where e_id=512.更新 emp 表中 e_id 为 5 的员工的 salary 为 3600; updateemp set salary=3600 where e_id=513更新表emp中e_id为4的员工的d_id为1,ed为6;updateemp set d_id=1,e_id=6 where e_id=414删除emp表中e_id为4的数据记录delete from emp where e_id=415在表dep的d_id字段上创建索引idxcreate index idx on dep(d_id)16创建视图v1,要求查询表emp中的全部数据记录; create view v1 as select * from emp17创建视图 v2,要求查询 e_id,e_name,salary 和 dep_name;(用 join 和 不用 join);create view v2 as select a.e_id,a.e_name,a.salary,b.dep_name fromempa,dep

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号