1、创建名为gongsi的数据库:

初始大小1MB,上不封顶,每次扩展64MB,同时有辅助文件

create database gongsi

on primary(

name='gongsi_dbf',

filename='E:\gongsi\gongsi.mdf',

size=1MB,

maxsize=unlimited,

filegrowth=64MB

)

log on

(

name='gongsi_ldf',

filename='E:\gongsi\gongsi.ldf',

size=1MB,

maxsize=unlimited,

filegrowth=64MB

);

2、连接至gongsi数据库:

use gongsi

3、创建表dangan至gongsi数据库:

create table dangan   创建表,表名为dangan

(

ID int primary key,     设置ID为主键

name char (12) not null,    设置name为非空

sex  char (2) default '男' not null ,  设置sex默认值为男,并且非空

age  int not null,         设置age为非空

csny date,

jiguanid int,

zhiweiid int,

gongling int

);

select * from dangan   查看dangan表项是否正确

4、将数据写入dangan表中

insert into dangan

select     1,'李晨','男',35,'1982-5-30',1,1,15

union select 2,'陈赫','男',34,'1983-4-22',1,2,14

union select 3,'张敏','女',33,'1984-1-20',2,3,13

union select 4,'林青霞','男',28,'1989-6-26',1,2,8

union select 5,'陆毅','男',40,'1977-4-14',2,5,20

union select 6,'王菲','女',32,'1985-3-21',2,3,12

union select 7,'刘亦菲','女',30,'1987-9-30',3,3,10

union select 8,'王祖贤','女',50,'1967-8-30',4,4,30

union select 9,'高晓松','男',21,'1996-5-30',6,6,1

union select 10,'邱淑贞','女',20,'1997-5-30',8,6,0

5、创建zhiwei表 

create table zhiwei

(

zw_id int primary key,

zw  char (10) not null,

);

6、将数据写入zhiwei表中:

insert into zhiwei

select  1,'技术支持'

union select  2,'运维'

union select  3,'销售'

union select  4,'财务'

union select  5,'保安'

union select  6,'开发'

7、创建jiguan表:

create table jiguan

(

jg_id int primary key,

jg  char (10) not null,

)

8、将数据写入jiguan表中:

insert into jiguan

select  1,'北京'

union select  2,'河北'

union select  3,'天津'

union select  4,'上海'

union select  5,'哈尔滨'

union select  6,'广东'

union select  7,'福建'

union select  8,'云南'

union select  9,'甘肃'

9、将dangan表的zhiweiid外键约束设置为zhiwei表的zw_id

alter table dangan

add constraint zw foreign key (zhiweiid) references zhiwei(zw_id)

10、将dangan表的jiguanid外键约束设置为jiguan表的jg_id

alter table dangan

add constraint jg foreign key (jiguanid) references jiguan(jg_id)

11、创建yeji表并设置ID的外键约束为dangan表的ID

create table yeji

(

ID int not null,

nian int not null,

yue int not null,

yeji int not null

constraint yj foreign key (ID) references dangan(ID)

)

12、将数据写入至yeji表中:

insert into yeji

select  1,2017,1,89067

union select  1,2017,2,77800

union select  1,2017,3,80000

union select  1,2017,4,76900

union select  1,2017,5,88097

union select  1,2017,6,98078

union select  1,2017,7,89067

union select  3,2017,1,47694

union select  3,2017,2,98576

union select  3,2017,3,65967

union select  3,2017,4,69857

union select  3,2017,5,58776

union select  3,2017,6,97877

union select  3,2017,7,58756

union select  6,2017,1,56897

union select  6,2017,2,57836

union select  6,2017,3,54966

union select  6,2017,4,56807

union select  6,2017,5,60987

union select  6,2017,6,67987

union select  6,2017,7,67989

union select  7,2017,1,56976

union select  7,2017,2,56860

union select  7,2017,3,56657

union select  7,2017,4,88980

union select  7,2017,5,80808

union select  7,2017,6,78796

union select  7,2017,7,80796

13、创建kaohe表并设置ID的外键约束为dangan表的ID

create table kaohe

(

ID int not null,

nian int not null,

yue int not null,

kaohe int not null

constraint kh foreign key (ID) references dangan(ID)

)

14、将数据添加至kaohe表中:

insert into kaohe

select  1,2017,1,89

union select  1,2017,2,88

union select  1,2017,3,78

union select  1,2017,4,78

union select  1,2017,5,87

union select  1,2017,6,67

union select  1,2017,7,67

union select  2,2017,1,77

union select  2,2017,2,89

union select  2,2017,3,98

union select  2,2017,4,88

union select  2,2017,5,89

union select  2,2017,6,87

union select  2,2017,7,78

union select  3,2017,1,87

union select  3,2017,2,89

union select  3,2017,3,87

union select  3,2017,4,89

union select  3,2017,5,87

union select  3,2017,6,89

union select  3,2017,7,76

union select  4,2017,1,65

union select  4,2017,2,67

union select  4,2017,3,86

union select  4,2017,4,78

union select  4,2017,5,86

union select  4,2017,6,78

union select  4,2017,7,98

union select  9,2017,1,89

union select  9,2017,2,88

union select  9,2017,3,78

union select  9,2017,4,78

union select  9,2017,5,87

union select  9,2017,6,67

union select  9,2017,7,67

union select  6,2017,1,77

union select  6,2017,2,89

union select  6,2017,3,98

union select  6,2017,4,88

union select  6,2017,5,89

union select  6,2017,6,87

union select  6,2017,7,78

union select  7,2017,1,87

union select  7,2017,2,89

union select  7,2017,3,87

union select  7,2017,4,89

union select  7,2017,5,87

union select  7,2017,6,89

union select  7,2017,7,76

union select  10,2017,1,65

union select  10,2017,2,67

union select  10,2017,3,86

union select  10,2017,4,78

union select  10,2017,5,86

union select  10,2017,6,78

union select  10,2017,7,98

15、创建gongzi表并设置ID的外键约束为dangan表的ID

create table gongzi

(

ID int not null,

nian int not null,

yue int not null,

gongzi int not null

constraint gz foreign key (ID) references dangan(ID)

)

16、将数据添加至gongzi表:

insert into gongzi

select  1,2017,1,8000

union select  1,2017,2,7900

union select  1,2017,3,7900

union select  1,2017,4,8500

union select  1,2017,5,7500

union select  1,2017,6,8100

union select  1,2017,7,7900

union select  2,2017,1,6000

union select  2,2017,2,6000

union select  2,2017,3,6000

union select  2,2017,4,6000

union select  2,2017,5,6000

union select  2,2017,6,6000

union select  2,2017,7,6000

union select  3,2017,1,6500

union select  3,2017,2,6500

union select  3,2017,3,6500

union select  3,2017,4,6500

union select  3,2017,5,6500

union select  3,2017,6,6500

union select  3,2017,7,6500

union select  4,2017,1,5800

union select  4,2017,2,5700

union select  4,2017,3,5800

union select  4,2017,4,5700

union select  4,2017,5,5900

union select  4,2017,6,5900

union select  4,2017,7,6000

union select  9,2017,1,8000

union select  9,2017,2,7900

union select  9,2017,3,7900

union select  9,2017,4,8500

union select  9,2017,5,7500

union select  9,2017,6,8100

union select  9,2017,7,7900

union select  6,2017,1,6000

union select  6,2017,2,6000

union select  6,2017,3,6000

union select  6,2017,4,6000

union select  6,2017,5,6000

union select  6,2017,6,6000

union select  6,2017,7,6000

union select  7,2017,1,6500

union select  7,2017,2,6500

union select  7,2017,3,6500

union select  7,2017,4,6500

union select  7,2017,5,6500

union select  7,2017,6,6500

union select  7,2017,7,6500

union select  10,2017,1,5800

union select  10,2017,2,5700

union select  10,2017,3,5800

union select  10,2017,4,5700

union select  10,2017,5,5900

union select  10,2017,6,5900

union select  10,2017,7,6000

union select  5,2017,1,8000

union select  5,2017,2,7900

union select  5,2017,3,7900

union select  5,2017,4,8500

union select  5,2017,5,7500

union select  5,2017,6,8100

union select  5,2017,7,7900

union select  8,2017,1,6000

union select  8,2017,2,6000

union select  8,2017,3,6000

union select  8,2017,4,6000

union select  8,2017,5,6000

union select  8,2017,6,6000

union select  8,2017,7,6000

17、给档案表中添加TEL字段,用来输入员工电话号码:

alter table dangan

add TEL char (15) default '1388686186' not null 添加默认值

18、给dangan表中前5名员工修改电话号码:

update dangan set TEL=13000000000 where ID=1

update dangan set TEL=13111111111 where ID=2

update dangan set TEL=13222222222 where ID=3

update dangan set TEL=13333333333 where ID=4

update dangan set TEL=13444444444 where ID=5

19、删除工资表中ID为9的所有数据。

delete from gongzi where ID=9

20、显示一个字段的数据记录

select  name from dangan

21、显示多个字段的数据:

select name,id,age,sex from dangan;

22、显示所有字段的数据:

select * from dangan;

23、显示前3行的数据:

select top 3 * from dangan

24、条件查询

条件表达式:

where  字段名  运算符  值  

运算符:比较运算符

= 等于

!= 不等于

<> 不等于

> 大于

< 小于

>= 大于等于

<= 小于等于

如果条件表达式中的字段名和值是数值型,则所有比较运算符都能够使用,如果是字符串型,则只能使用= 和!=

查询性别为女的员工信息。

select * from dangan where sex='女';

查询年龄大于25岁的员工信息。

select * from dangan where age>25 ;

查询职位为销售的员工信息。

方法一:

先查询销售的职位id号:

select * from zhiwei where zw='销售';

再查询dangan表中,zhiweiid为3的数据记录。

select * from dangan where zhiweiid=3;

方法二:

使用多表查询的语句,进行连接查询。涉及到多张表时,语法有了新的规则,建议将所有的字段前都加上表名,肯定不会出错。

select dangan.* from dangan,zhiwei

where dangan.zhiweiid=zhiwei.zw_id and zhiwei.zw='销售'

查询年龄20或21岁,并且工龄大于等于1年的员工信息。

select * from dangan where (age=20 or age=21) and gongling>=1;

查询年龄在30到50之间的员工信息。(包30和50)

select * from dangan where age>=30 and age<=50;

select * from dangan where age between 30 and 50;   between为在某个范围之间

查询职位为销售或运维的员工信息

select dangan.* from dangan,zhiwei

where dangan.zhiweiid=zhiwei.zw_id and (zhiwei.zw='销售' or zhiwei.zw='运维')

或者

select dangan.* from dangan,zhiwei

where dangan.zhiweiid=zhiwei.zw_id and zhiwei.zw in ('销售','运维')

25、模糊查询

又称为部分匹配查询,使用通配符代替查询条件中常量的部分字符,便于查询到跟多的结果或不能精确定位的结果。

通配符:

linux系统中的通配符:

可以代替任意一个字符,不能为空,范围是0-9  a-z  A-Z  符号等。

如:abcd.txt    ?bcd.txt      ?a?

* 可以代替任意一组字符,可以为空,字符数量0个到最大

如:abc*.txt   *a*

数据库中,由于?号和*号有特殊的作用所以使用其他符号做通配符:

_ 可以代替任意一个字符,可以为空,可以连续使用。显示多个_数量以内的字符。范围是0-9  a-z  A-Z  符号等。

如:刘_      ’王__最大2个最少1个字符      _易_

% 可以代替任意一组字符,可以为空,字符数量0个到最大

如:刘%

注意:因为查询条件中的常量使用了通配符后,不能代表一个精确的值,所以条件表达式中就不能使用=号进行连接了。使用like引用添加了通配符的常量。

查询姓张的员工信息:

select * from dangan where name like '张%';

查询姓刘的员工信息:

select * from dangan where name like '刘__'

不知道具体数值的查询:

select * from dangan where name like '王[飞非费肥菲妃]'

select * from dangan where name like '刘[以一亿已意亦][飞非费肥菲妃]'

26、空值查询

select * from dangan where gongling is null;

null空值,不是一个具体的数值,只是一个标记。is只用于查询空值

27、多表查询

当查询的字段或者条件中涉及到多张表时,需要在条件表达式中建立表与表之间的关系,如果没有建立关系,多张表之间的数据记录会被重复引用,出现错误的数据记录,如果关系建立错误,则可能导致查询出错误的数据或无数据。

查询李晨的工资信息

select  dangan.name,gongzi.yue,gongzi.工资  from  dangan,gongzi

where  dangan.id=gongzi.id  and  dangan.name='李晨'

语句说明:多表查询时,在每个字段的前面添加表名,在语句的调用过程中肯定不会出错。如果引用的字段在所有涉及的表中只有一个,则可以不加表名。

查询李晨2月份工资

select  gongzi.工资  from  dangan,gongzi  where

dangan.id=gongzi.id  and  dangan.name='李晨'  and   gongzi.yue=2

查询籍贯为北京的员工姓名。

select  dangan.name  from   dangan,jiguan

where  dangan.jiguanid=jiguan.jg_id  and  jiguan.jg='北京'

查询籍贯为北京,职位为运维的员工姓名。

分析:涉及到jiguan表,zhiwei表,dang_an表,在超过两张表建立关系时,不是所有的表之间都相互建立关系,至少与其中一张表建立关系。在建立关系时,注意需要重复引用的字段。

select  dangan.name  from  dangan,jiguan,zhiwei

where  dangan.jiguanid=jiguan.jg_id  and

dangan.zhiweiid=zhiwei.zw_id  and  jiguan.jg='北京'  and   zhiwei.zw='运维'

查询李晨7月份的业绩,考核和工资。

分析:dangan,yeji,kaohe,gongzi    ,id字段,

select  dangan.name,yeji.yeji,kaohe.kaohe,gongzi.gongzi  from  dangan,yeji,kaohe,gongzi

where  dangan.id=yeji.id  and  dangan.id=kaohe.id  and  dangan.id=gongzi.id  and

yeji.nian=kaohe.nian  and  yeji.nian=gongzi.nian  and

yeji.yue=kaohe.yue  and  yeji.yue=gongzi.yue  and

dangan.name='李晨'  and  yeji.yue=7

语句说明:在编写多表查询的语句时,先做好三件事,就能降低编写难度,和提高编写思路。

1.分析查询需求中都涉及到了那些表?有些表是显示字段数据时调用,有些表是条件中调用。

2.分析这些表之间建立关系,都需要使用那些字段。包括这些表之间有没有描述同一件事务的字段,如果有,也需要建立等值链接。

3.分析查询需求中都有哪些条件?有些是明确的条件描述,有些是不明确的条件描述。

查询籍贯不是‘北京’的员工姓名

分析:查询条件是籍贯!=北京,需要显示的字段是  姓名name字段,涉及到dangan,jiguan表。

select  dangan.name  from  dangan,jiguan

where  dangan.jiguanid=jiguan.jg_id  and  jiguan.jg!='北京'

查询职位不是运维的员工姓名和所属职位

分析:条件--职位不是运维,显示字段  姓名  职位,涉及到dangan表和zhiwei表。

select  dangan.name,zhiwei.zw  from  dangan,zhiwei

where   dangan.zhiweiid=zhiwei.zw_id  and    zhiwei.zw!='运维'

查询2017年7月,既有yeji,又有考核的员工ID、姓名;

分析:条件2017年7月,yeji中的id等于kaohe中的id,显示字段  id  name    涉及表  dangan,yeji,kaohe

select  dangan.id,dangan.name  from  dangan,yeji,kaohe

where  dangan.id=yeji.id   and   dangan.id=kaohe.id

and  yeji.nian=kaohe.nian  and  yeji.yue=kaohe.yue

and  yeji.nian=2017   and   yeji.yue=7

查询职位为‘运维’,籍贯为‘北京’的员工ID、姓名

分析:条件 职位=运维,籍贯=北京,显示字段id  name  涉及表  dang_an  zhiwei   jiguan

select  dangan.id,dangan.name  from  dangan,jiguan,zhiwei

where  dangan.jiguanid=jiguan.jg_id  and  dangan.zhiweiid=zhiwei.zw_id

and  jiguan.jg='北京'  and   zhiwei.zw='运维'

查询有考核分数小于70的ID、姓名、年、月

分析:条件是kaohe<70,显示字段是id  name  nian  yue  ,涉及表dang_an   kaohe

select  dangan.id,dangan.name,kaohe.nian,kaohe.yue  from  dangan,kaohe

where  dangan.id=kaohe.id  and  kaohe.kaohe<70

查询7月份考核分数小于80的员工姓名,按分数降序排列

分析:条件,yue=7   kaohe<80   order by  kaohe  desc,显示字段name,涉及表dangan   kaohe

select  dangan.name  from  dangan,kaohe

where  dangan.id=kaohe.id  and  kaohe.yue=7

and  kaohe.kaohe<80  order by  kaohe  desc     desc:降序

查询职位为‘销售’并且工资6000以上的员工ID和姓名

分析:条件,zw=销售   gongzi>6000  显示字段  id  name   表  dangan  gongzi  zhiwei

select  dangan.id,dangan.name  from  dangan,gongzi,zhiwei

where  dangan.id=gongzi.id  and  dangan.zhiweiid=zhiwei.zw_id

and  zhiwei.zw='销售'  and  gongzi.gongzi>6000

查询职位为“销售”,且考核分数低于90的员工姓名、考核分数、月

分析:条件,zw=销售   kaohe<90   显示字段  name  kaohe  yue  表  dangan   zhiwei   kaohe

select  dangan.name,kaohe.kaohe,kaohe.yue  from  dangan,kaohe,zhiwei

where  dangan.id=kaohe.id  and  dangan.zhiweiid=zhiwei.zw_id

and  zhiwei.zw='销售'  and  kaohe.kaohe<90

28、查询排序

order  by  子句,根据指定的字段按升序asc  或降序desc进行排序,默认主键字段升序排序。如果指定字段排序但没有指定是升序还是降序,默认也是升序。如果指定排序的字段不是主键或有唯一性约束,重复信息的记录按照主键或第一字段升序排序。

select  *  from   dangan  order  by  age  desc

select  *  from   dangan  order  by  csny  asc

注意:在sqlserver中,能够对汉字进行排序,是按照汉字发音的声母排序,升序排序的优先级是,数字--英文字母--汉字。

在mysql和oracle中,不能对汉字的声母进行排序,只能按照汉字的16进制编码进行排序。

order  by子句一般使用在T-SQL语句的最后面。能够对字段或统计后的数据进行排序。

29、将查询结果放入一张新表

查询员工姓名和工资大于6000的信息,放入新的gg表

select  dangan.name,gongzi.nian,gongzi.yue,gongzi.gongzi  into  gg

from  dangan,gongzi

where  dangan.id=gongzi.id  and  gongzi.gongzi>6000

select * from gg   查看

注意:into  表名   只能是新表,如果是已存在的表,则会报错。

30、将查询数据添加到指定表。

insert  into  gg

select  name,gongzi.nian,gongzi.yue,gongzi

from dangan,kaohe,gongzi

select * from gg

注意:添加时要匹配原表的结构和查询显示字段的位置。

SQL创建数据库与写入数据的全过程相关推荐

  1. 通过VB向SQL Server数据库中录入数据

    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 一.数据录入 通过VB向SQL Se ...

  2. 如何对两个大型SQL Server数据库中的数据进行快速估计比较,以查看它们是否相等

    Bringing impactful analysis into a data always comes with challenges. In many cases, we rely on auto ...

  3. SQL创建数据库– PostgreSQL,MySQL,SQL Server

    The start of data storage is from the creation of a database. As the name suggests database is a bas ...

  4. php往文件里面写入数据,PHP创建文件及写入数据(覆盖写入,追加写入)的方法详解...

    本文实例讲述了PHP创建文件及写入数据(覆盖写入,追加写入)的方法.分享给大家供大家参考,具体如下: 这里主要介绍了PHP创建文件,并向文件中写入数据,覆盖,追加的实现代码,需要的朋友可以参考下: 创 ...

  5. 在MySql数据库中创建数据库dbjava,数据表book,字段包括bno,bname,price,count(编号、书名、单价、数量)。 利用JDBC连接数据库dbjava,实现数据表的增删 改查

    题目 1.在MySql数据库中创建数据库dbjava,数据表book,字段包括bno,bname,price,count(编号.书名.单价.数量). 2.利用JDBC连接数据库dbjava,实现数据表 ...

  6. Mysql数据库中插入记录的命令_MySQL创建数据库并插入数据命令

    简介: 学习mysql环境为ubantu,下面记录一些基本的创建数据库和插入数据的口令 学习资源来自实验楼:https://www.shiyanlou.com/courses/9 打开MySQL 服务 ...

  7. [转载]在SQL Server数据库之间进行数据导入导出,OPENDATASOURCE

    需要在c盘下先建立一个data.txt文件,然后在文件的第一行写上你要导出的列,不如说要导出id和name这两列,就在第一行写上 id,name 然后保存,使用下列SQL就可以了,你如果要保持原有的I ...

  8. 在SQL Server数据库之间进行数据导入导出

    来源:http://kb.cnblogs.com/page/94464/ 在SQL Server数据库之间进行数据导入导出 (1).使用SELECT INTO导出数据 在SQL Server中使用最广 ...

  9. SQL Server数据库导入导出数据方式比较

    在我们建立一个数据库时,并且想将分散在各处的不同类型的数据库分类汇总在这个新建的数据库中时,尤其是在进行数据检验.净化和转换时,将会面临很大的挑战.幸好SQL Server为我们提供了强大.丰富的数据 ...

  10. C#同步SQL Server数据库中的数据--数据库同步工具[同步已有的有变化的数据]

    C#同步SQL Server数据库中的数据--数据库同步工具[同步已有的有变化的数据] 1. C#同步SQL Server数据库Schema 2. C#同步SQL Server数据库中的数据--数据库 ...

最新文章

  1. python 组合数据类型_【Python】组合数据类型
  2. 字符ascii码值转换_没想到 Unicode 字符还能这样玩?
  3. 【Nginx那些事】nginx配置实例(三)动静分离
  4. 公司的个性制度与团队精神
  5. Maven项目有红叉,文件却没有错误,已解决
  6. Spring jar包下载
  7. 2020年亚太杯数学建模竞赛赛题
  8. 卢森堡携手欧洲航天局,在大公国建立独一无二的“欧洲太空资源创新中心”
  9. 斗牛/牛牛经典算法java版
  10. 屌丝c++语言程序设计第二章 c++语言基础
  11. python youtube api_使用youtube v3 API从youtube播放列表检索所有视频
  12. ThinkPad开启、禁用触摸板
  13. QIIME 2教程. 07Cell帕金森小鼠Parkinson's Mouse(2021.2,最佳实战)
  14. cobbler自动部署装机
  15. 努比亚z11mini 使用 移动物联卡
  16. unity SteamVR利用手柄合理移动
  17. 二极管关键参数及选型指导
  18. 数据库 之数据库设计浅知识 -- 设计概述、概念结构设计(E-R模型概述)、逻辑结构设计(函数依赖和范式)、物理结构设计
  19. 【 微信小程序 】behaviors
  20. python笔记 - EasyGui的使用

热门文章

  1. luogu1969积木游戏
  2. Flask 和 requests 搭建一个简单的API服务
  3. 计算机博士复试英语自我介绍,博士复试自我介绍中英文
  4. PhalAPI学习笔记 ——— 第三章细致讲解使用PSR-4规范自定义你的命名空间
  5. 免费开源统计软件介绍——jamovi
  6. 用Scipy中的linprog解决股票融资中的线性规划问题
  7. Beyong Compare4过期解决办法
  8. 【公开课】国内外公开课网址
  9. Android 5.1.1 源码目录结构
  10. DNS服务器地址查找不到,DNS服务器地址的查看方法