SQL数据库的基本操作

来源:互联网 发布:淘宝试客联盟 编辑:程序博客网 时间:2024/05/21 23:57

1 数据查询

一般格式:

Select 目标列表达式

From 表名

Where 条件

例子

select t_student02.studentage,t_student02.studentid,t_student01.hobbies,t_student02.stu

from firstdata.dbo.t_student01,secenddata.dbo.t_student02

where t_student01.studentid=t_student02.studentid

2 数据插入

一般格式:

          Insert into表名(字段,字段)

          Values(值,值)

例子

insert

into t_student01(Sid,Sage,Sname,Ssex)

values(20140390231,21,'小覃','')

注意:括号,引号和标点符号也必须在英文状态下输入,中文输入法输入时会提示语法错误

3 数据删除

一般格式:

例子:

delete

from t_student01

where Sid=20140390232

解答:关键词输入错误也会提示列名无效,但其实并不一定是列名无效

4数据修改

一般格式:update表名

         Set 列=表达式

         Where 条件

例子:

update t_student01

set Sage=23

where Sid=20140390233

5.查看数据库状态

USE 学生信息管理系统

GO

SELECT DATABASEPROPERTYEX('学生信息管理系统','status')

AS '当前状态'

6.创建数据库

CREATE DATABASE 学生信息管理系统

7.删除数据库

DROP DATABASE 学生信息管理系统

8修改数据库名称

ALTER DATABASE学生管理系统

MODIFY NAME=学生信息管理系统

9.创建主键列

USE 学生信息管理系统

CREATE TABLE学生信息表

(

学号 nvarchar(50)NOT NULL PRIMARY KEY,

姓名 nchar(10)not null,

性别 nchar(10)null,

民族 nchar(10)null ,

班级 nchar(10)null,

出生日期 datenull,

家庭住址 ntextnull

)

10.创建外键

USE 学生信息管理系统

CREATE TABLE班级信息表

(

班级编号 nvarchar(50)NOT NULL primary key,

班级名 nchar(10)not null,

班级人数 nchar(10)not null,

所属系别 nvarchar(50)null,

辅导员 nchar(10)not null,

FOREIGN KEY(所属系别)references 系别信息,

FOREIGN KEY(辅导员)references 辅导员信息

)

USE 学生信息管理系统

CREATE TABLE辅导员信息

(

辅导员编号 nvarchar(50)NOT NULL primary key,

姓名 nchar(10)not null,

性别 nchar(10)not null,

年龄 nchar(10)null,

民族 nchar(10)not null,

籍贯 nchar(10)null,

联系电话 varchar(50)NOT NULL

)

11.重命名表

use 学生信息管理系统

GO

exec sp_rename'辅导员信息','辅导员信息表'

0 0