MySQL——表中数据的增删改和约束(查还没学完,后面单独写查)

目录

一、添加数据

二、删除数据

三、修改数据

四、约束

1.主键约束

2.主键自增

3.非空约束

4.唯一约束

5.默认约束

五、完整代码


一、添加数据

insert into 表名(字段1,字段2...) values(值1,值2...),(值1,值2....)....;

注意:若写了字段,则字段个数和值个数需要一致,若不写字段,则值需要包含每个字段,加多条数据时,两条数据之间加逗号。

二、删除数据

delete from 表名 where 条件;   注意:如果不加条件则删除全部数据

清空数据:

1.delete from 表名;

2.truncate table 表名;

两者区别:

1.delete会报警告,truncate不会报警告

2.delete清空后会保留自增顺序,重新插入数据以后按照之前的自增顺序自增,

truncate清空后会重置自增顺序,重新插入数据以后自增顺序从1开始自增

三、修改数据

update 表名 set 字段名1=值 ,字段名2=值 where 条件;

注意:如果不加条件则修改此字段所有数据

四、约束

1.主键约束

1.建表时:id int primary key;  #主键一般是编号或者ID,且只能一个

2.建表后:alter table 表名 add primary key(id);

3.删除主键约束:(1)alter table 表名 drop primary key;(只能删除键,还剩下非空约束)

(2)alter table 表名 change id id int ;(删除非空约束)至此才能完全删除主键约束

4.特点:主键不能为空,不重复,且只能有一个

2.主键自增

1.建表时:id int primary key auto_increment,

2.建表后:alter table 表名 change id id int auto_increment;

3.删除主键自增:alter table 表名 change id id int ;

4.特点:主键按照升序一直增加

3.非空约束

1.建表时:字段名 字段类型 not null;

2.建表后:alter table 表名 change 旧字段名 新字段名 字段类型 not null ;

3.删除非空约束:alter table 表名 change 旧字段名 新字段名 字段类型;

4.特点:此字段不能为空

4.唯一约束

1.建表时:字段名 字段类型 unique;

2.建表后:alter table 表名 change 旧字段名 新字段名 字段类型 unique ;

3.删除唯一约束:drop index 字段名 on 表名;

4.特点:此字段不重复

5.默认约束

1.建表时:字段名 字段类型 default 值;

2.建表后:alter table 表名 change 旧字段名 新字段名 字段类型 default 值;

3.删除默认约束:alter table 表名 change 旧字段名 新字段名 字段类型 ;

4.特点:特点:插入数据的时候不指定值就会用默认值

五、完整代码

#创建库
create database day02_db;

#使用库
use day02_db;

#创建表
create table student(
    id int,
    name varchar(100),
    age int
);

#插入数据
#插入一条数据
insert into student(name) values('张三');
#插入多条数据
insert into student(name) values('李四'),('王五'),('赵六');
#不指定字段插入一条数据:本质是指定所有字段
insert into student values(1,'张三',18);
#不指定字段插入多条数据
insert into student values(1,'张三',18),(2,'李四',18),(3,'王五',18);

#修改数据
#修改李四年龄为28
update student set age = 28 where name = '李四';
#修改王五和赵六的年龄为39
update student set age = 39 where name = '李四' or name = '赵六';
#修改赵六的id为4,年龄为40
update student set age = 40 , id = 4 where name = '赵六';
#注意:没有加条件,修改的是所有数据
update student set age = 12;

#删除数据
delete from student where id = 1 or id = 3;
#注意:不加条件则删除所有数据
delete from student;
#truncate也能删除所有数据
truncate table student;#不会警告,官方推荐使用truncate清空数据。



#主键约束
#方式一:建表时
create table stu1(
    id int primary key ,
    name varchar(100),
    age int
);
#查看表结构
desc stu1;
#方式二:建表后
create table stu2(
    id int,
    name varchar(100),
    age int
);
#添加主键
alter table  stu2 add primary key (id);
#查看表结构
desc stu2;

#主键约束的特点:非空唯一
insert into stu2(id,name) values(0,'张三');
insert into stu2(id,name) values(0,'李四');  #报错,不能重复
insert into stu2(id,name) values(null,'李四');  #报错,不能为空

#主键约束删除
#注意:删除主键约束后只删除了唯一约束,非空约束依然存在
alter table stu1 drop primary key ;
#如果同时非空约束也不要,则么删除?用change
alter table stu1 change id id int;
#再次查看表结构
desc stu1;

#主键自增
#方式一:建表时
create table stu3(
    id int primary key auto_increment,
    name varchar(100),
    age int
);
desc stu3;
#方式二:建表后
create table stu4(
    id int primary key ,
    name varchar(100),
    age int
);
#添加主键自增
alter  table  stu4 change id id int auto_increment;
desc stu4;
# 演示主键设置自增后的特点: 从1开始自增
insert into stu4(name) values('张三'); # 成功,自动从1开始
#注意:设置自增后0和nuLL仅是占位符,默认都代表自动使用自增
insert into stu4(id,name) values(0,'张三');# 成功
insert into stu4(id,name) values(0,'李四');#成功
insert into stu4(id,name) values(null,'李四'); #成功
#删除主键自增
alter  table  stu4 change id id int;
desc stu4;

#delete和truncate区别:
# 都能清空数据内容
# delete删除所有数据会报警告,truncate删除所有数据无警告
# delete在删除所有数据后,自增顺序保留,下次再插入数据的时候,接着删除之前的自增顺序继续自增
#truncate在删除所有数据后,自增顺序保留,下次再插入数据的时候,自增顺序从1开始重新自增
delete from stu4;
insert into stu4(name) values('李四'),('王五'),('赵六');
truncate stu4;
insert into stu4(name) values('李四'),('王五'),('赵六');


#非空约束
#方式一
create table stu5(
    id int primary key ,
    name varchar(100) not null,
    age int
);
#方式二:
alter table stu5 change age age int not null;
#非空约束删除
alter table stu5 change age age int;
alter table stu5 change name name int;

#唯一约束
#方式一:
create table stu6(
    id int primary key ,
    name varchar(100) unique ,
    age int
);
#方式二:
alter table stu6 change age age int unique ;
#默认执行了create unique index age on stu6(age);
#唯一约束的特点
insert into stu6 values(1,'张三',18);
insert into stu6 values(2,'李四',19);

#删除唯一约束
drop index age on stu6;

#默认约束
#方式一:
create table stu7(
    id int primary key ,
    name varchar(100) default 'admin',
    age int
);
#方式二
alter table stu7 change age age int default 18;
#删除默认约束
alter table stu7 change age age int ;
  • 15
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# VS2012 86系统 mysql-5.5.27-win32 功能:利用动软代码生成器 从 数据库表或者视图中生成 的三层结构代码 实现 数据增删。 如果可以,请下载资源中 修 的动软代码生成器 C#模板生成 1、由于之前使用 动软生成 java 网页源码,比较成功,此处编写C#程序时沿用,感觉更加适合。 2、直接调用动软的相关dll和生成的三层代码,可以较快的实现增删操作。 3、由于一些dll版本的问题及动软生成器自身的一些不善,产生了一些问题并找了挺久,所以把可以实现的版本发布出来共享。 前提: 使用的是 mysql数据库时才可能会出现以下问题 问题: 1、MySql.Data.dll 必须是5.6.1以上版本,否则会出现 “向信号量添加给定计数将导致其超出它的最大计数” 的问题。 2、动软代码生成时,必须增加该命名空间 using MySql.Data.MySqlClient; 3、动软代码必须修 “工具”-“选项”弹出窗 后,点击 ”代码生成设置“-”字段类型映射“-”参数符号“中删除 mysql @,添加mysql ? 4、如果不修3的设置,在增删时 参数设置会失败。 5、mysql保存或者修时,中文会出现乱码,这时必须 在DbHelperMySQL类的 连接字中增加Charset=utf8;即 protected static string connectionString = "Server=localhost;User Id=root;Password=root;Persist Security Info=True;Database=mnzfz;Charset=utf8;"; 6、如果要在局域网中远程访问,请 修 mysql 权限:grant select,update,insert,delete on *.* to 'root'@'192.168.0.1' identified by "123456";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值