--创建 MyCompany数据库

use master

execute sp_configure 'show advanced options',1  --开启权限
reconfigure
execute sp_configure 'xp_cmdshell',1
reconfigure execute xp_cmdshell 'mkdir e:\作业数据库'--自动创建文件夹
execute sp_configure 'xp_cmdshell',0--关闭权限,先关
reconfigure
execute sp_configure 'show advanced options',0 后关,一句话,先开后关
reconfigure if exists(select * from sysdatabases where name='MyCompany')
drop database MyCompany
go
create database MyCompany
on primary
(
name='MyCompany_data',
size=3mb,
fileGrowth=10%,
Maxsize=100mb,
filename='e:\作业数据库\MyCompany_data.mdf'
)
log on
(name='MyCompany_log',
size=3mb,
fileGrowth=10%,
Maxsize=100mb,
filename='e:\作业数据库\MyCompany_log.ldf'
)
--部门表 department
-- dId
-- dName
use MyCompany
if exists(select * from sysobjects where name='department')
drop table department
go
create table department
(dId int not null primary key identity(1,1),
dName nvarchar(50) not null )
--员工表 employee
-- eId
-- eName
-- eSex
-- eAge
-- eSalary
-- eDepId
-- eInTime 入职时间 if exists(select * from sysobjects where name='employee')
drop table employee
go
create table employee
( eId int not null primary key identity(1,1),
eName nvarchar(50),
eSex char(2),
eAge int,
eSalary money,
eDepId int,
eInTime datetime )
添加外键FK_employee_department_eDepId
alter table employee
add constraint FK_employee_department_eDepId foreign key(eDepId) references department(dId) insert into department values ('教学部')
insert into department values ('业务部')
insert into department values ('学工部') insert into employee values ('李定','男',28,5000,1,'2014-8-8')
insert into employee values ('张月月','女',28,5000,2,'2013-8-8')
insert into employee values ('李定山','男',18,3000,3,'2014-8-1')
insert into employee values ('张三','男',18,1800,3,'2014-7-1')
insert into employee values ('张三1','女',18,1800,3,'2013-7-1') insert into employee values ('张三2','女',28,4800,2,'2011-7-1') --建库 建表 建约束 添加测试数据 更新 和删除
--1、查询所有员工
select * from employee --2、查询工资超过2000快钱的员工
select * from employee where eSalary>2000
--3、查询最新进来的5个员工 ---降序排序
select top 5 * from employee order by eInTime desc --4、查询员工的平均工资 select avg() from
select avg(eSalary) as 平均工资 from employee --5、查询总共有多少员工 count
select count(*) as 员工数 from employee --6、查询每个部门有多少员工 ---按部门分组
select eDepId,count(*) as from employee group by eDepId --7、查询每个部门的平均工资
select eDepId,avg(eSalary) from employee group by eDepId --8、查询每个部门男员工的平均工资
select eDepId,eSex,avg(eSalary) from employee where eSex='男' group by eDepId,eSex --9、查询平均工资超过2000的那些部门 having 对分组之后的数据再进行筛选 select eDepId,avg(eSalary) from employee group by eDepId having avg(eSalary)>200 --10、查询员工工资(姓名,工资)的同时,同一结果集显示平均工资和最高工资
--select ' '+姓名,工资 from 表
--union
--select cast(平均工资 as varchar()),最高工资 from 表
select eName,eSalary from employee
union all select '平均工资',avg(eSalary) from employee
union all
select '最高工资',max(eSalary) from employee
--这个方式也行
select eName,eSalary from employee
union all
select convert(varchar(20),avg(eSalary)) ,max(eSalary) from employee
--11、查询名字里包含'定,月'的这些员工,并对结果按照年龄排序 select * from employee where eName like'%三%' or eName like '%定%'order by eAge ----好吧没有做更新和删除 update 表名 set 字段=新赋值 where 字段=‘’
例如:把张三的名字改为逗碧
update employee set eName='逗碧' where  eName='张三'

delete table 表名  ===等开除吧,后面加where 否则全删除 -

--删除数据时候 把自增长列的值还原成种子

truncate table 表名 --   强迫症而已

  

SQL语句创建数据库以及一些查询练习的更多相关文章

  1. 2-05使用SQL语句创建数据库2

    使用SQL语句创建多个数据文件和日志文件: USE master--指向当前使用的数据库 GO--批处理的标志 CREATE DATABASE E_Market--创建E_market数据库 ON P ...

  2. 2-06使用SQL语句创建数据库3

    向现有数据库中添加文件组和数据文件几种方式以及步骤: 第一种:在视图下添加文件组和数据文件. 添加文件组的步骤: 右击你想要添加文件组的数据库点属性,然后点文件组就可以添加. 添加数据文件的步骤: 下 ...

  3. sql server2008中怎样用sql语句创建数据库和数据表

    这是简单用代码实现创建数据库和数据表的sql语句,如下: --调用系统数据库-- use master go /***防止你要创建的数据库同名,先把它删除掉****/ if Exists(select ...

  4. SQL语句创建数据库,SQL语句删除数据库,SQL语句创建表,SQL语句删除表,SQL语句添加约束,SQL语句删除约束

    创建数据库: CREATE DATABASE Test --要创建的数据库名称 ON PRIMARY ( --数据库文件的具体描述 NAME='Test_data', --主数据文件的逻辑名称 FIL ...

  5. 使用SQL语句创建数据库2——创建多个数据库文件和多个日志文件

    在matser数据库下新建查询,输入的命令如下: USE master GOCREATE DATABASE E_MarketON PRIMARY--主文件组( NAME ='E_Market_data ...

  6. 使用SQL语句创建数据库1——创建一个数据库文件和一个日志文件的数据库

    目的:创建一个数据库文件和一个日志文件的数据库 在matser数据库下新建查询,输入的命令如下: USE master——指向当前使用的数据库.创建数据库实际上是向master数据库中增加一条数据库信 ...

  7. C# 读取文件中的sql语句 创建数据库以及表结构

    大概思路是: 读取文件 根据文件中行内容为GO 作为分割  一条条放到list中 然后在程序中逐条执行sql语句; 值得一提的是 创建数据库的语句是不允许放到程序事务中执行的 所以目前我是分了两个文本 ...

  8. MySQL数据库执行sql语句创建数据库和表提示The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to have it working

    MySQL创建数据库 只想sql文件创建表时候提示 The 'InnoDB' feature is disabled; you need MySQL built with 'InnoDB' to ha ...

  9. 2-04使用SQL语句创建数据库

    下面是创建数据库的一些语法: USE master--指向当前使用的数据库 GO--批处理的标志 CREATE DATABASE E_Market--创建E_market数据库 ON PRMARY-- ...

随机推荐

  1. 项目积累demo-01

    1 搭建Spring-Boot项目 在这里我使用intellij新建spring boot工程: 点击next; 输入Group以及artifact之后.点击next.之后点击web.接着finish ...

  2. 在重命名SqlServer数据库时,报5030错误的解决办法

    数据库不能重名名5030的错误,其实很简单原因就是有应用程序正在占用这个连接,使用这样一行命令就可以查询出正在占用的连接 use master select spid from master.dbo. ...

  3. 全文检索技术---solr

    1       Solr介绍 1.1   什么是solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr可以独立运行在Jetty.Tom ...

  4. google广告尺寸

    谷歌AdMob广告支持三种tablet-only旗帜大小除了320×50显示在手机: 大小(WxH) 描述 可用性 AdSize常数 320×50 标准的旗帜 手机和平板电脑 横幅 300 x250 ...

  5. layui js中求某一天距今天有多少天

    <script> lay('#version').html('-v'+ laydate.v); //执行一个laydate实例 laydate.render({ elem: '#test1 ...

  6. Python开发【第六篇】:文件处理

    1. 文件   文件处理流程: 打开文件,获得文件句柄,并赋值 通过句柄对文件进行操作 关闭文件 1.1 打开文件   在 Python 中使用 open()函数打开文件,并返回文件对象: open( ...

  7. 【转-mysql-explain介绍】

    explain显示了MySQL如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 先解析一条sql语句,看出现什么内容 EXPLAINSELECTs.uid, ...

  8. Protocol Buffers官方文档(开发指南)

    本文是对官方文档的翻译,然后截取了一篇非常优秀的文章片段来帮助理解,本人英文水平有限,基本都是直译,如果有不理解的地方请参考英文官方文档,参考的文章链接在文章末尾 protocol buffers简介 ...

  9. [Java]构造函数内部多态的行为所引起的灾难

    构造函数内部的多态行为所产生的意想不到的结果 一.Java版本 1 package com.company; 2 import static com.brianyi.util.Print.*; 3 4 ...

  10. 洛谷P3792 由乃与大母神原型和偶像崇拜

    P3792 由乃与大母神原型和偶像崇拜 题目背景 由乃最近没事干,去研究轻拍学去了 就是一个叫做flip flappers,轻拍翻转小膜女的番 然后研究的过程中她看到了一个叫做大母神原型的东西 大母神 ...