21.3.4 使用SQL创建数据库
无论SQL Server 2005还是其他的版本,它们都是使用SQL语言,所以它们使用的基本点都是一样的。SQL语言的重要性,读者在以后的工作中会体会到。为了帮助读者尽快熟悉SQL语言的知识。下面将讲述如何通过SQL语句对数据库进行操作。
注意 SQL语言不区分大小写。
【实例21.1】创建一个新的数据库,名称为“schoolmanage”。接着再新建一张表,名称为“teacherinfo”。通过插入的方式将数据导入表中。
(1)创建一个数据库:
create database schoolmanage
(2)创建好了一个数据库,接下来就需要创建一张表,名称为“teacherinfo”。再为这张表创建字段。
create table teacherinfo
(name char(8)null,
code char(8)null,
sexy char(8)null,
age char(8)null,
birthday char(8)null,
address char(8)null,
salary char(8)null,
)
(3)通过上面的创建,在表中就有了字段名称。到目前为止,表中还没有数据。下面通过插入命令来给这张表添加数据。
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('王鹏','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('宋江','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('李丽','2001','女','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('钱敏','2001','女','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('朱庭','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('祖海','2001','女','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('周浩','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('张平','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('孙军','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('王俊','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('李鹏','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('孙鹏','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('王海','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('周洁','2001','女','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('宋平','2001','女','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('吴浩','2001','男','23','1975-9-1','重庆市','3000');
insert into teacherinfo(name, code, sexy, age, birthday, address, salary)values('武芬','2001','女','23','1975-9-1','重庆市','3000');
【代码说明】teacherinfo是指数据库中的表,而括号内的就是字段,在values后面的括号内,就是这些字段的值。这样就创建了一个简单的有数据的数据库。
下面再看看如何去操作这些数据,现在想要查询全部教师中的女士,可以使用下面的SQL语句来实现它。
select*from teacherinfo where sexy='女'
这里的where后面跟着的是条件,因为要查询全部的女士,所以只要 性别为女就可以了。现在,需要删除一些信息,操作如下。
delete*from teacherinfo where sexy='女'
上面的语句是把表中所有的女教师信息删除,这里的where同上面一样,也是标识条件。很多实际开发中的语句比这要复杂得多,限于篇幅和本书的宗旨,此处不再详细介绍SQL语句的一些复杂应用,有兴趣的读者可以自行查阅一些相关资料。