.Net_01_建库建表的基本语法(Sql 语句)

1、 常用数据类型
  数字类型
    -> int
  字符串类型
    -> char、nchar、varchar、nvarchar
    -> 数据类型(数字)
    -> 区分unicode字符与非unicode字符
    -> 如果使用varchar与nvarchar可以使用max表示字符串大小
  时间类型
    -> datetime
    -> 赋值的时候使用字符串的方式即可
  金钱数据类型
    -> money
    -> 一般使用的时候直接当做数字类型即可
  bool类型
    -> bit
    -> 在使用的时候,用设计器赋值只能使用'true'和'false'
    -> 在SQL语句中可以使用字符串'true'和'false',也可以使用数字,1表示true,0表示false
  二进制类型
    -> image
2、 编码习惯
  -> 字段用中文
    -> nvarchar
  -> 命名规范
    -> 与系统冲突或字段中需要有空格,可以使用[]框起来(SQL Server中还可以使用双引号)

3.建库

use master --系统数据库
go

--判断数据库是否已经存在,存在则删除

if db_id('数据库名') is not null
drop database 数据库名

--创建数据库

create database 数据库名
on
(

  --数据库
  name = '数据库名',
  filename = '绝对路径',--g:\db\School.mdf
  size = 10,--初始文件大小
  filegrowth=10--增长
)
log on
(

  --日志文件
  name = '数据库名_log',
  filename = '绝对路径',--g:\db\School_log.ldf
  size = 3,--初始文件大小
  filegrowth=10% --增长
);
go

 1 use master
 2 go
 3 
 4 if db_id('School') is not null
 5     drop database School
 6 
 7 create database School
 8 on
 9 (
10     name = 'School',
11     filename = 'g:\db\School.mdf',
12     size = 10,
13     filegrowth=10
14 )
15 log on
16 (
17     name = 'School_log',
18     filename = 'g:\db\School_log.ldf',
19     size = 3,
20     filegrowth=10%
21 );
22 go

4.建表

--判断表是否已经存在,存在则删除

if object_id(‘表名’,’U’) is not null

  drop table 表名;

create table 表名

(

  列名 数据类型 约束,

  列名 数据类型 约束

)

1 if object_id('Course','U') is not null
2     drop table Course;
3 create table Course
4 (
5     classId                int                identity(1,1)    primary key,    --    课程Id
6     className            nvarchar(20)    not null,                        --    课程名称
7     classDescription    nvarchar(max)    not null                        --    课程描述
8 );

5.约束

数据库约束是为了保证数据的完整性(正确性)而实现的一套机制

非空约束

主键约束(PK) primary key constraint唯一且不为空

唯一约束 (UQ)unique constraint 唯一,允许为空,但只能出现一次

默认约束 (DF)default constraint 默认值

检查约束 (CK)check constraint 范围以及格式限制

外键约束 (FK)foreign key constraint 表关系

--添加主键约束 alter table 表名 add constraint 约束名 primary key(列名)

--添加唯一约束 alter table 表名 add constraint  约束名 unique(列名)

--添加默认约束 alter table  表名 add constraint  约束名 default('男') for 列名

--添加检查约束 alter table  表名 add constraint 约束名 check(列名 >=18 and 列名 <=100)

--添加外键约束(主键表Class 外键表student) alter table student add constraint FK_student foreign key(sClassId) references Class(cId)

--外键student表中的sClassId来references引用主键表中的cid

--删除约束 alter table student drop constraint FK_student

 1 -- 主键约束
 2     alter table newStudent 
 3     add constraint PK_newStudent_stuId primary key(stuId);
 4 
 5 -- 唯一约束
 6     alter table newStudent
 7     add constraint UQ_newStudent_stuName unique(stuName);
 8 
 9 -- 默认约束 input时间设为当前时间
10     alter table newStudent
11     add constraint DF_newStudent_stuInput default(getdate()) for stuInput;
12 
13 -- 检查约束 性别
14     alter table newStudent
15     add constraint CK_newStudent_stuSex check(stuSex='' or stuSex='');
16 
17 --增加外键约束
18 alter table Employees add constraint FK_DeptId_DepId foreign key(DeptId) references Department(DepId)

 

转载于:https://www.cnblogs.com/-ljj/archive/2013/01/27/2878803.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值