sql 语句 1

数据类型

整数:INT 浮点数:float double

.字符串:char varchar

二进制:blob text

时间类:date(年月日) year datetime (年月日时分秒)

增删改查语句

添加当前时间 now() sysdate() CURRENT_DATE()

truncate table t_student; -- 相当于重新建了一张表

delete from t_student; -- 一条条的删除

select * from t_student;  -- 查看表中全部数据
​
delete from t_student where sno = 1;    -- 删除表中sno等于1的这行数据
​
update t_student set sex = '女' where  sno = 1;   -- 修改sno=1 中的sex字段改为 女
​
insert into t_student values(1,'张三','男',18,now(),'java一班','133@com');
insert into t_student values(1,'张三','男',18,Sysdate(),'java一班','133@com');
insert into t_student values(1,'张三','男',18,CURRENT_DATE(),'java一班','133@com');
insert into t_student (sno,sname,enterdate) values (1,'李四',now());  -- 插入部分数据

修改表的结构

-- 增加一列字段
alter table t_student add score double(5,2); -- 5总的个数  2小数位数   再表中加一列再最后
alter table t_student add score double(5,2) first ;  -- 增加一列(到最前面)
alter table t_student add score double(5,2) after sex; -- 增加到sex后面
​
-- 删除一列字段
alter table t_student drop score;  -- 删除score 这一列
​
​
-- 修改一列数据类型
alter table t_student modify score float(4,1);   -- modify 修改的是数据类型
alter table t_student change score score1 double(5,1); -- change 修改列名和数据类型
​
-- 删除表
drop table t_student; 
​
– 查看表的结构,展示表的字段详细信息
DESC t_student;

表的约束

primary key 主键约束 unique 唯一约束 check检查约束

auto_increment 自动递增(只能添加到列后面) not null 非空约束(只能添加到列后面) default 默认约束(只能添加到列后面)

注意:自增和主键 需要一起去约束

方式1: 添加在建表数据的每一行(列级约束) and(并且)

create table t_student(
    son int(5) primary key not null,  -- 主键约束  非空约束
    sname varchar(10)  not null,    -- 非空约束
    sex char(1) default '男' check(sex = '男'|| sex = '女'),  -- 默认约束为男   检查约束 要么男 要么女
    age int(10) check(age>=18 and age <= 50), -- 检查约束 大于18  小于 50 之间     and(并且)
    enterdate date,
    classname varchar(10),
    email varchar(15) unique  -- 唯一的约束
);

方式2 :添加在建表最后添加约束(表约束) constraint(表约束)

create table t_student(
        sno int (5) auto_increment,
        sname varchar(10) not null,
        sex char(1) default '男',
        age int(10),
        enterdate date,
        classname varchar(10),
        email varchar(15),
        constraint pk_stu primary key(sno), -- pk_stu 主键约束的名字
        constraint ck_stu_sex check(sex = '男' || sex = '女'), -- ck_stu_sex 检查约束的名字
        constraint ck_stu_age check(age>=18 and  age<=50),  -- ck_stu_age 检查约束的名字
        constraint uq_stu_email unique(email)   -- uq_stu_email 约束起别名
);

方式3: 表建完后 再去给表添加约束

-- 在创建表以后添加约束
alter table t_student add constraint pk_stu primary key(sno); -- 主键约束
alter table t_student modify sno int(6) auto_increment;  -- 递增约束
alter table t_student add constraint ck_stu_sex check(sex = '男'|| sex = '女'); 
alter table t_student add constraint ck_stu_age check(age>= 18 and age<= 50);
alter table t_student add constraint uq_stu_em unique(email);
-- 查看表结构:
desc t_student;
​

外键约束 foreign key

注意:外键约束只有表级约束,没有列级约束

-- 先创建父表
create table t_class(
    cno int(4) primary key auto_increment,
    cname varchar(10) not null,
    room char(4)
);
​
drop table t_student; -- 删除表
​
-- 创建子表
create table t_student(
    sno int(6) primary key auto_increment,
    sname varchar(10) not null,
    classno int(4), -- 取值按照 class表中的cno字段 ,不要求字段名字完全重复,但是类型和长度保持相同
    constraint fk_stu_classno foreign key (classno) references t_class(con);  -- 添加外键约束
);
​
alter table t_student add constraint fk_stu_classno foreign key (classno) references t_class(cno); 添加外键约束
​

外键策略

策略 (1)no action 不允许操作

update t_student set classno = null where classno = 2; -- 先置空
delete from t_class where cno = 2; -- 再去删除

策略 (2)cascade 级联操作

alter table t_student drop foreign key fk_stu_classno; -- 先删除外键约束
​
alter table t_student add constraint fk_stu_classno foreign key(classno) references t_class(cno) 
on update cascade  on delete cascade; -- 再去添加记录操作约束

策略 (3)set null 置空操作:

alter table t_student drop foreign key fk_stu_classno; -- 先删除外键约束

alter table t_student add constraint fk_stu_classno foreign key(classno) references t_class(cno) 
on update set null  on delete set null;  --  再去添加外键操作约束 

复制表

create table t_student2 as selece * from t_student; -- 复制一样表带数据

-- 复制一样表不带数据
create table t_student3 as select *from t_student where 1=2; 

-- 只要部分字段和数据
create table t_student4 as select sno,sname,age from t_studen  where sno = 2; 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小王学java^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值