启动服务:

1.在系统服务启动

2.在sql配置管理器服务选项中启动

3.在管理员cmd:net start mssqlserver;net stop mssqlserver

use master
go
if(not exists(select* from sysdatabases where name='school'))
create database school
on primary
(
name='School_data',
filename='E:\School_data.mdf',
size=10MB,
filegrowth=1MB
) log on
(
name='School_log',
filename='E:\School_log.ldf',
size=2MB,
filegrowth=1MB
)
go use school
create table class(clId int not null identity(1,1) constraint PK_cid primary key,clName varchar(40) not null);//定义自增长主键
create table student(stuId int not null identity(1,1)constraint PK_sid primary key,stuName varchar(20) not null,sex varchar check (sex in('F','M')) not null,clID int constraint FK_StuClass foreign key(clID) references class(clID)) //定义外键约束,检查约束 insert into class(clName) values('软件1班')
insert into class(clName) values('软件2班')
insert into class(clName) values('软件2班') update class set clName='软件3班' where clId=3
insert into class(clName) values('软件4班') select *from class insert into student(stuName,sex,clID) values('用户1','F',1)
insert into student(stuName,sex,clID) values('用户2','M',2)
insert into student(stuName,sex,clID) values('用户3','F',3)
insert into student(stuName,sex,clID) values('用户4','M',2)
insert into student(stuName,sex) values('用户5','M') select *from student //内连接
select student.clID,stuName,clName from student
inner join class on student.clId=class.clId order by clId
//左连接
select student.clID,stuName,clName from student
left join class on student.clId=class.clId
where student.clId is null order by clId
//右连接
select count(student.clID)as count from student
right join class on student.clId=class.clId
group by class.clId having count(student.clID)>1
//嵌套查询
select *from student where clId=(select clId from class where clId=2) go use master
go
if exists(select *from sysdatabases where name='school')
drop database school use master
go
exec sp_detach_db school
go exec sp_attach_db school,
'E:\School_data.mdf',
'E:\School_log.ldf'
go

use master;

select *from spt_values order by number;//默认递增





select type,count(type) from spt_values group by type having count(type)>19;

select distinct type from spt_values;

create database hh;

use hh;





drop table ggz;

create table ggz(id int IDENTITY(1,1),num int not null,adds varchar(10))



//添加主键约束

alter table ggz alter column id int not null;

alter table ggz add constraint pk_id primary key(id);



//删除主键约束

alter table ggz drop constraint pk_id;



//添加唯一约束

alter table ggz add constraint UQ_adds unique(adds);



//添加默认约束

alter table ggz add constraint DF_adds default('ca') for adds;



//修改属性

alter table ggz alter column adds varchar(10) null;



//插入

insert into ggz(id,num) values(3,3);



//添加检查约束

alter table ggz add constraint CK_num check(num in(1,0));



//更新

update ggz set num=2 where id=3;





insert into ggz(num) values(0);



//删除

delete from ggz where num=0;

sqlserver基础操作的更多相关文章

  1. Data Base sqlServer基础知识

    sqlServer   基础知识 大纲 创建数据库 1 创建表 2 备份表 3 删除表 4 修改表 5 查询出重复的数据 6 增删改查 7 添加约束 8 分页存储过程 9 排序 10 类型转换 11 ...

  2. sqlServer基础知识

    sqlServer   基础知识 大纲 创建数据库 1 创建表 2 备份表 3 删除表 4 修改表 5 查询出重复的数据 6 增删改查 7 添加约束 8 分页存储过程 9 排序 10 类型转换 11 ...

  3. Entity Framework 基础操作(1)

    EF是微软推出的官方ORM框架,默认防注入可以配合LINQ一起使用,更方便开发人员. 首先通过SQLSERVER现在有的数据库类生产EF 右键->添加->新建项,选择AOD.NET实体数据 ...

  4. MySQL 之基础操作及增删改查等

    一:MySQL基础操作 使用方法: 方式一: 通过图型界面工具,如 Navicat,DBeaver等 方式二: 通过在命令行敲命令来操作 SQL ( Structure query language ...

  5. python基础操作以及hdfs操作

    目录 前言 基础操作 hdfs操作 总结 一.前言        作为一个全栈工程师,必须要熟练掌握各种语言...HelloWorld.最近就被"逼着"走向了python开发之路, ...

  6. MYSQL基础操作

    MYSQL基础操作 [TOC] 1.基本定义 1.1.关系型数据库系统 关系型数据库系统是建立在关系模型上的数据库系统 什么是关系模型呢? 1.数据结构可以规定,同类数据结构一致,就是一个二维的表格 ...

  7. 【Learning Python】【第二章】Python基础类型和基础操作

    基础类型: 整型: py 3.0解决了整数溢出的问题,意味着整型不必考虑32位,64位,有无符号等问题,你写一个1亿亿亿,就是1亿亿亿,不会溢出 a = 10 ** 240 print(a) 执行以上 ...

  8. Emacs学习心得之 基础操作

    作者:枫雪庭 出处:http://www.cnblogs.com/FengXueTing-px/ 欢迎转载 Emacs学习心得之 基础操作 1.前言与学习计划2.Emacs基础操作 一. 前言与学习计 ...

  9. Git基础操作

    配置秘钥 1.检查本机有没有秘钥 检查~/.ssh看看是否有名为d_rsa.pub和id_dsa.pub的2个文件. $ ~/.sshbash: /c/Users/lenovo/.ssh: Is a ...

随机推荐

  1. python 在列表中添加元组元素,按照元组第一个值进行排序

    >>> import bisect >>> scores = [(, , , , 'python')] >>> bisect.insort(sco ...

  2. 农历03__ZC

    代码,改自 农历01(http://www.cnblogs.com/cppskill/p/5930558.html) 1.main.cpp #include "Lunar_ZC.h" ...

  3. linux 前端部署

    linux.conf user root; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice ...

  4. 深入理解javascript之typeof和instanceof

    1.https://blog.csdn.net/mevicky/article/details/50353881 (深入理解javascript之typeof和instanceof)

  5. 使用Bus.js进行兄弟(非父子)组件通信

    首先需要在任意地方添加一个bus.js: 在bus.js里面 写入下面信息 import Vue from 'vue' export default new Vue; 在需要通信的组件都引入Bus.j ...

  6. reg文件中文乱码

    用reg文件方式修改注册表很方便,但是如果字段中有中文的话,执行reg文件后,注册表中的中文出现乱码. 解决办法:将文件保存文Unicode编码方式即可.(保存文UTF8编码也是不行的) 附一个添加\ ...

  7. Java LRU的实现

    最近在leetcode上做题的时,看到了一道有关LRU Cache的题目,正好我当初面试阿里巴巴的时候问到的.主要采用linkedHashMap来实现. package edu.test.algori ...

  8. centos6.9使用yum安装mysql(简单粗暴,亲测有效)

    第1步.yum安装mysql[root@stonex ~]#  yum -y install mysql-server安装结果:Installed:    mysql-server.x86_64 0: ...

  9. Xadmin的配置及使用

    xadmin是Django的第三方扩展,可是使Django的admin站点使用更方便. 1. 安装 通过如下命令安装xadmin的最新版 pip install https://github.com/ ...

  10. SM2的非对称加解密java工具类

    maven依赖 <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov- ...