数据库SQL语句的操作
SQLServer数据库的基础知识的回顾:
1)主数据文件:*.mdf
2)次要数据文件:*.ndf
3)日志文件:*.ldf
每个数据库至少要包含两个文件:一个数据文件和一个日志文件
如何查看SQL Server的帮助==================快捷键F1
一、创建文件夹
exec sp_configure 'show advanced options',1
go
reconfigure
go
exec sp_configure 'xp_cmdshell',1
go
reconfigure
go
exec xp_cmdshell 'mkdir E:\新建文件'
go
二、创建数据库
1.例子:
--判断,如果有这个数据库则进行删除
if exists(select * from sysdatabases where name='MySchool')
begin
drop database MySchool
end
--创建数据库
create database MySchool
on primary
(
--数据文件的具体描述
name='MySchool_data', --主数据文件的逻辑名称+++++++必须写
filename='E:\MySchool_data.mdf', --主数据文件的物理名称+++++++必须写
size=5mb, --主数据文件的初始大小
maxsize=100mb, --主数据文件增长的最大值
filegrowth=15% --主数据文件的增长率
)
log on
(
--日志文件的具体描述,各参数含义同上
name='MySchool_log',
filename='E:\MySchool_log.ldf',
size=2mb,
filegrowth=1mb
)
go
三、创建表
2 use MySchool --将当前数据库设置为MySchool,以便在MySchool里创建表
3 go
4 --判断是否存在表,存在则删除
5 if exists (select * from sysobjects where name='Student')
6 drop table Student
7---创建Student表
8 create table Student
9 (
10 StudentNo int identity(1,1) primary key not null, --学号 自增 主键,非空
11 loginpwd nvarchar(20) not null,
12 StudentName nvarchar(20) not null,
13 Sex bit default'女' not null, --性别,取值0,1
14 GradeId int not null,
15 Phone nvarchar(50) null,
16 Address nvarchar(100) null,
17 BornDate datetime not null,
18 Email nvarchar(20) null,
19 IdentityCard varchar(18) not null
20 )
21 go
四、创建约束
语法:
1 alter table 表名
2 add constraint 约束名 约束类型 具体的约束说明
例子:
1、添加默认约束(默认'地址不详')
1 alter table Student
2 add constraint df_address default('地址不详') for address
2、添加检查约束(要求出生在1996年10月26日)
1 alter table Student
2 add constraint ck_BornDate check (BornDate >='1996-10-26')
3、添加唯一约束(身份证全世界只有一个)
1 alter table Student
2 add constraint uq_IdentityCard unique (IdentityCard)
4、添加主键约束
1 alter table Student
2 add constraint pk_StudentNo primary key(StudentNo)
5、添加外键约束(主表 Student 和从表 REsult建立关系,关联列StudentNo)
1 alter table Result
2 add constraint fk_StudentNo
3 foreign key(StudentNo) references Student (StudentNo)
五、sql操作数据库数据的实现(增、删、查、改)
(一)插入数据
语法:
insert into 目标表(新表)
select '列名' union
eg
insert into card(ID,password)
select ‘0023-ABC’,‘ABC’ union
(二)增加数据
语法:
insert into 表名
values (‘ ’,‘ ’)
eg
insert into card
values (‘0023-ABC’,‘ABC’)
(三)修改数据
语法:
update 表名 set 行名
where 列名
eg
update card set password=‘pwd’
where ID=‘0023-ABE’
(四)删除数据
语法:
delete from 表名
where 行名
eg
delete from card
where ID=‘0023-ABE’
(五)查看数据
语法:
select * from 表名
eg
select * from student
数据库SQL语句的操作的更多相关文章
- SQL语句 远程操作数据库
--远程操作数据库SQL语句exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '211.81.251.85 'exec sp_addlinkedsr ...
- MySQL与SQL语句的操作
MySQL与SQL语句的操作 Mysql比较轻量化,企业用的是Oracle,基本的是熟悉对数据库,数据表,字段,记录的更新与修改 1. mysql基本信息 特殊数据库:information_sche ...
- MySQL 数据库SQL语句——高阶版本2
MySQL 数据库SQL语句--高阶版本2 实验准备 数据库表配置: mysql -uroot -p show databases; create database train_ticket; use ...
- 学生选课数据库SQL语句45道练习题整理及mysql常用函数(20161019)
学生选课数据库SQL语句45道练习题: 一. 设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四 ...
- MySQL 数据库SQL语句——高阶版本1
MySQL 数据库SQL语句--高阶版本 实验准备,数据表配置 mysql -uroot -p show databases; create database train_ticket; use tr ...
- Oracle数据库SQL语句操作大全汇总
数据库各个表空间增长情况的检查: SQL >SELECT A.TABLESPACE_NAME,( 1-(A.TOTAL)/B.TOTAL)* 100 USED_PERCENT FROM (SEL ...
- ORACLE数据库SQL语句的执行过程
SQL语句在数据库中处理过程是怎样的呢?执行顺序呢?在回答这个问题前,我们先来回顾一下:在ORACLE数据库系统架构下,SQL语句由用户进程产生,然后传到相对应的服务端进程,之后由服务器进程执行该SQ ...
- 数据库—SQL语句
下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DEL ...
- 数据库 数据库SQL语句五
集合运算 union 并集(两个集合如果有重复部分,那么只显示一次重复部分) union all 并集(两个集合如果有重复部分,那么重复部分显示两次) intersect 交集 minus 差集 -- ...
随机推荐
- reids桌面管理工具:RedisDesktopManager下载、使用
概要:一款好用的Redis桌面管理工具,支持命令控制台操作,以及常用,查询key,rename,delete等操作. 下载软件,请点击下面链接,进入下载页,选择对应版本: https://redisd ...
- PatentTips – Shader Interfaces
BACKGROUND Today's graphic processing units (GPUs) host all of the computations necessary to generat ...
- HashMap源码分析3:移除
本文源码基于JDK1.8.0_45. final Node<K,V> removeNode(int hash, Object key, Object value, boolean matc ...
- sql sever 等待事件
http://blog.csdn.net/dba_huangzj/article/details/7607844
- 移动端,input输入获得焦点被键盘遮住简单解决方案
(function (window,document) { document.querySelector('input[type="text"]').addEventListene ...
- Html5离线缓存简介
一. 什么是manifest 首先manifest是一个后缀名为minifest的文件,在文件中定义那些需要缓存的文件,支持manifest的浏览器,会将按照manifest文件的规则,像文件保存在本 ...
- ci output
ci output类可以将数据存起来,下面这个方法 a 代表的就是存起来的数据 public function(){ $data = array( 'name'=>'alice', ); $th ...
- WebDev.WebServer40.EXE
http://www.cnblogs.com/tong-tong/archive/2013/05/02/3049428.html 大学玩asp.net时就发现VS在Debug时会起一个web服务,这东 ...
- [Vue @Component] Switch Between Vue Components with Dynamic Components
A common scenario is to present different components based on the state of the application. Dynamic ...
- vim中自己主动加入凝视 加入文本信息
工欲善其事,必先利其器.在开发过程中.方便.快捷的开发环境.能提高工作效率.优美的界面能让我们心情愉悦:最重要的是,能保持我们在外行严重高深莫測的牛逼~ 假设在创建新的源程序文件时希望能自己主动产生一 ...