zoukankan      html  css  js  c++  java
  • 数据库 关键 排序,去重,分组,外键

    create database mmm
    create table xxx
    (
    code int primary key identity(1,1),
    name varchar(50) not null,
    sex varchar(50) not null,
    age int not null,
    hight decimal(18,2) not null,
    wight int not null,
    
    )
    go
    insert into xxx values('佐助','',20,175,100);
    insert into xxx values('鸣人','',21,177,110);
    insert into xxx values('小樱','',19,165,80);
    insert into xxx values('宁次','',22,179,200);
    insert into xxx values('迪达拉','',23,180,130);
    select *from xxx
    update xxx set name='我爱罗' where name='鸣人'--吧鸣人换成我爱罗
    delete from xxx where code=4--删除掉第四行
    select*from xxx where name like '小%'--找寻名字表里面有小的
    select top 3*from xxx--只显示前三行
    select top 2 code from xxx where age>19--显示表里面年龄大于19岁的两行
    select *from xxx  order by age--按照年龄开始排序
    select distinct name from xxx--只显示名字
    select *from xxx order by age,wight desc--按照年龄体重排序
    select wight from xxx group by wight --按照体重排序
    select age from xxx group by age --按照年龄排序
    select name from xxx group by name --按照名字排序
    select *from xxx where age-3<20--找寻年龄减3小于20岁的
    select *from xxx where age in(19,20)--寻表里面年龄是19和20岁的
    select *from xxx where name in('佐助')
    --外键 :受约束的表叫外键表,约束的数据源叫主键表
    --要想加外键,首先得有主键表
    --要想删主键表数据,必须先删除外键表数据
    --作为外键的数据源的列,必须要是一个唯一键(这一列必须是主键或者是unique)
    
    create table teacher 
    (
     tcode int primary key identity(1,1) ,
     tname varchar(50)
    )
    insert into teacher(tname) values('张三')
    select *from teacher
    create table student
    (
     scode int primary key identity(1,1) ,
     sname varchar(50),
     tno int references teacher(tcode) ,  --student表的tno项参考teacher表的tcode项,tcode项必须是主键项
     cid varchar(20) unique    --唯一列,不能重  unique
    )
    insert into student values ('学生1',null,'32134124')   --tno项只能输入null或张三的编号
    insert into student values ('学生2',null,'321434124')
    insert into student values ('学生3',null,'32153124')
    insert into student values (  ,1,'3215g124')
    select *from student
  • 相关阅读:
    C# 开发(创蓝253)手机短信验证码接口
    33条C#、.Net经典面试题目及答案
    请用一句sql语句取出各科的平均成绩,显示字段,科目,平均成绩
    写出一条Sql语句:取出表A中第31到第40记录(SQLServer,以自动增长的ID作为主键,注意:ID可能不是连续的。
    SQL的四种连接-左外连接、右外连接、内连接、全连接
    C# 获取mp3文件的歌曲时间长度
    欧拉公式
    linux环境java入门
    C内存分配
    机器学习网址归纳
  • 原文地址:https://www.cnblogs.com/w-wz/p/4441790.html
Copyright © 2011-2022 走看看