Sql语句学习笔记(2) 创建数据表
use RetalDB--表示在数据库RetalDB中进行的操作
go
if exists (select * from sysobjects where name='tb_user')
drop table tb_user
go
--创建客户表tb_user
create table tb_user
(
user_id int primary key,--指定为主键时,此列默认为非空,指定过多个限制条件时不用“'”隔开
user_name varchar(20) not null,
user_point int not null,
)
--创建影碟类型表tb_MovieType
if exists (select *from sysobjects where name='tb_movie_type')
drop table tb_movie_type
go
create table tb_movie_type
(
type_id int primary key,--指定主键
type_name varchar(20) not null,
type_english_name varchar(20) not null
)
--创建影碟表
if exists (select * from sysobjects where name='tb_movie')
drop table tb_movie
go
create table tb_movie
(
movie_id int primary key,
movie_name varchar(20) not null,
movie_price float,
movie_type_id int not null,
movie_detail varchar(255)
--添加外键约束
constraint FK_movie_id foreign key(movie_type_id) references tb_movie_type(type_id)--引用影片类型表中的type_id字段
)
--创建租赁信息表
if exists (select * from sysobjects where name='tb_retal')
drop table tb_retal
go
create table tb_retal
(
id int identity(1,1) primary key,--信息编号
user_id int not null,--外键,引用客户表中的主键user_id
movie_id int not null, --外键,引用影片表中的movie_id
rent_time datetime not null default(getdate()),--租出时间,以插入数据时的时间为默认值
return_time datetime,--归还时间
rent_fee float default(0)--租金
constraint FK_user_id foreign key(user_id) references tb_user(user_id),
constraint FK_movie_id foreign key(movie_id) references tb_movie(movie_id)
)
创建好的表如图所示:(后来为了保证tb_retal中数据的唯一性,将user_id,movie_id,rent_time这3个键设置成了联合主键,即同一个客户不可能在同一时间租了同一张影碟多次)
相关文档:
说到事务,首先我们就要知道为什么需要事务,这就要先看看锁机制的相关概念!
锁的概述
一. 为什么要引入锁
多个用户同时对数据库的并发操作时会带来以下数据不一致的问题:
丢失更新
A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订票系统&nb ......
1、关于SQL server 2000 在安装过程中遇到文件挂起的解决办法:
在Ghost 版 windows xp 中安装 SQL server 2000 时经常会遇到安装程序运行到第二步(如下图所示)时点击下一步显示有文件挂起操作,提示重新起
动计算机解决这个问题,可重新起动计算机后,安装程序进行到此仍然显示有文件挂起操作。这个问题的产生实际并 ......
后缀名为.mdf的sql数据库如何打开?
有.mdf和.ldf两个文件,打开方案:
方法一:在Sql server企业管理器中 - 数据库 - 右键 <附加数据库
方法二:在VS2005中先建立一个与你要打开的mdf文件同名的数据库,记住存储位置,然后用你要打开的mdf文件将刚建立的mdf数据库文件替换即可。 ......
连接查询
通过连接运算符可以实现多个表查询。连接是关系数据库模型的主要特点,也是它区别于其它类型数据库管理系统的一个标志。
在关系数据库管理系统中,表建立时各数据之间的关系不必确定,常把一个实体的所有信息存放在一个表中。当检索数据时,通过连接操作查询出存放在多个表中的不同实体的 ......
固定服务器角色
sysadmin 可以在 SQL Server 中执行任何活动。
serveradmin 可以设置服务器范围的配置选项,关闭服务器。
setupadmin 可以管理链接服务器和启动过程。
securityadmin 可以管理登录和 CREATE DATABASE 权限,还可以读取错误日志和更改密码。
processadmin 可以管理在 SQL Server 中运行的进程。
......