数据库,表

数据库

 快捷键

-》常用辅助命令及快捷键
    set statistics time on/off:在消息栏显示详细执行时间
    ctrl+e执行
    ctrl+r隐藏消息栏
    ctrl+l计划任务,对sql语句进行分析

脚本创建数据库

不区分大小写,字符串使用单引号,末尾不需要加分号

数据库操作create、alter、drop

创建数据库

  create database 数据库名
    on primary
    (
        name='stuDB_data',  -- 主数据文件的逻辑名称
        filename='D:stuDB_data.mdf', -- 主数据文件的物理名称
        size=5mb, --主数据文件的初始大小
        maxsize=100mb, -- 主数据文件增长的最大值
        filegrowth=15%--主数据文件的增长率
    )
    log on
    (
        name='stuDB_log',
        filename='D:stuDB_log.ldf',
        size=2mb,
        filegrowth=1mb
    )

数据库切换

use  数据库名

数据库操作 create、alter、drop

创建表

create table ClassInfo
(
    cId int not null primary key identity(1,1),
    cTitle nvarchar(10)
)

create table StudentInfo ( sId int not null primary key identity(1,1), sName nvarchar(10) not null, sGender bit default(0), sBirthday date, sPhone char(11), sEMail varchar(20), cid int not null, foreign key(cid) references
ClassInfo(cid) //外键关联 )

alter

添加字段的语法:alter table tablename add (column datatype [default value][null/not null],….);

修改字段的语法:alter table tablename modify (column datatype [default value][null/not null],….);

删除字段的语法:alter table tablename drop (column);

数据操作insert UserInfo(UserName,UserPwd) 

values('小龙包','21232f297a57a5a743894a0e4a801fc3')
--为所有的列,按照默认顺序赋值,可以使用如下简写
insert UserInfo
values('大头贴','21232f297a57a5a743894a0e4a801fc3')
--为某些列赋值,而不是全部列

--为所有行的指定列进行修改 //修改一列 update UserInfo set UserPwd='admin' --为指定行进行修改列 update UserInfo set UserPwd='21232f297a57a5a743894a0e4a801fc3' where UserId>1 //条件还可以这么写 --一次性写多个数据,批量插入 select * from classInfo insert into classInfo values('青龙'),('白虎'),('朱雀'),('玄武') delete classinfo where cid>4 --清空 --truncate table classinfo truncate table userinfo

 创建和删除外键

x先找出约束名字
然后删除它
我给个例子

--测试环境
--主表
create table test1(id int primary key not null,value int)
insert test1 select 1,2
go
--从表
create table test2(id int references test1(id),value int)
go

删除外键

--第一步:找出test2表上的外键约束名字
--2000
exec sp_helpconstraint 'test2'
--可以在constraint_name 属性中找到外键约束名字
--2005
select name 
from sys.foreign_key_columns f join sys.objects o on f.constraint_object_id=o.object_id 
where f.parent_object_id=object_id('test2')
/*
name
---------------------------------
FK__test2__id__08EA5793*/

--第二步:删除外键约束
alter table test2 drop constraint FK__test2__id__08EA5793

--第三步:检查表上是否还有外键约束
--只要使用第一步里面的查找语句即可



原文地址:https://www.cnblogs.com/qj696/p/13995071.html