MySQL基础之数据管理【1】

添加记录

insert [into] tbl_name[(col_name,...)] {value|values}(values...);
--不指定字段名称时需要按照建表时的字段顺序给每一个字段赋值
--插入多条数据用逗号隔开
insert tbl_name value(values...);
insert tbl_name set 字段名称=值,...;
insert tbl_name[(字段名称...)] select 字段名称,... from tbl_name [where 条件];

测试

create database if not exists king default character set 'utf8';
use king;
create table user(
	id int unsigned auto_increment key comment '编号',
	username varchar(20) not null unique comment '用户名',
	age tinyint unsigned default 18 comment '年龄',
	email varchar(50) not null default '325@qq.com' comment '邮箱'
)engine=innodb charset=utf8;
insert user value(1,'king',24,'12335@qq.com');
insert user value(null,'queen',20,'1235235@qq.com');
insert user(id,username) value(3,'wang');

create table if not exists user2(
	name varchar(20) not null default 'aafd'
)engine=innodb charset=utf8;
insert user2 value('aaa'),
('bbb'),
('ccc');

修改记录

update tbl_name set 字段名称=值,字段名称=值,... [where 条件];
--如果不添加条件,整个表中的记录都会被更新

测试

update user set age=29 where id=1;
update user set username='黎明',age=1,email='111@qq.com' where id=3;
update user set age=age+10;
update user set age=age-5,email=default where id<=5;

删除记录

delete from tbl_name [where 条件]
--如果不添加条件,表中所有记录都会被删除

测试

delete from user where username='king';
delete from user where age>=23;
delete from user; --删除表中所有记录
alter table user auto_increment=1; --将auto_increment重置为1
truncate [table] tbl_name; --彻底将表清空

查询记录 select语句的基本形式

select select_expr,... from tbl_name [where 条件] 
[group by {col_name|position} having 二次筛选] 
[order by {col_name|position|expr} [asc|desc]]
[limit 限制结果集的显示条数];

select * from tbl_name; --查询表中所有记录  *所有字段   
select 字段名称,... from tbl_name --指定字段的信息
select 字段名称 [as] 别名名称,... from db_name.tbl_name; --给字段起别名
select 字段名称,... from tbl_name [as] 别名; --给数据表起别名
select tbl_name.col_name,... from tbl_name; --表名.字段名

测试

create table user(
	id int unsigned auto_increment key comment '编号',
	username varchar(20) not null unique comment '姓名',
	age tinyint unsigned not null default 18 comment '年龄',
	sex enum('男','女','保密') not null default '保密' comment '性别',
	addr varchar(20) not null default '北京',
	married tinyint(1) not null default 0 comment '0代表未婚,1代表已婚',
	salary float(8,2) not null default 0 comment '薪水'
)engine=innodb charset=utf8;

insert user values(1,'king',23,'男','上海',1,50000);
insert user(username,age,sex,addr,married,salary) values('queen',27,'女','上海',0,25000);
insert user set username='imooc',age=31,sex='女',addr='北京',salary=40000;
insert user values(null,'张三',38,'男','上海',0,15000),
(null,'张子枫',38,'男','上海',0,15000),
(null,'子怡',25,'女','北京',0,85000),
(null,'王菲',62,'女','广州',0,95000),
(null,'刘德华',14,'男','南京',0,115000),
(null,'吴亦凡',35,'男','上海',0,75000),
(null,'张阿文',14,'男','西安',0,65000),
(null,'经过历',25,'男','湖南',0,15000);

select * from user;
select username,addr,age from user;
select * from king.user; --不用打开数据库就可以查询指定的数据表
select id as 'id',username as '姓名' from user;
select id,username from user as u;
select user.id,user.username from user;
posted @ 2019-09-02 23:33  星海|universe  阅读(186)  评论(0编辑  收藏  举报