MySQL-常用的数据库操作
数据库操作
| show databases | 查看所有数据库 | 
|---|---|
| create database[if not exists] db_db1 | [如果不存在]创建数据库db_db1 | 
| use db_db1 | 使用db_db1数据库,即在该数据库下进行操作 | 
| drop database [ if exists] db_db1 | [如果存在]删除数据库db_db1 | 
| alter database db_db1 character set utf8; | 修改数据库db_db1的编码为utf8 | 
表操作
--创建表
create table [if not exists]表名(
字段名1 类型(宽度) [约束条件]  [comment '字段说明'],
字段名2 类型(宽度) [约束条件]  [comment '字段说明']
)[表的一些设置];
create table if not exists student(
id int,
name varchar(20),
birth date,
address varchar(20),
score double
);
| show tables | 查看当前数据库所有表名称 | 
|---|---|
| show create table 表名 | 查看某个表的创建语句 | 
| desc table 表名 | 查看某个表的结构 | 
| drop table 表名 | 查看某个表 | 
表的结构操作
--alter table 表名 add 字段名 类型 [约束];		字段名即列名
--修改student表,添加score列
alter table student add score double ;
--alter table 表名 change 旧字段名 新字段名 类型 [约束];
--修改student表,更改id列为sid列
alter table student change id sid int ;
--alter table 表名 drop 字段名 
--修改student表,删除score列
alter table student drop score;
--rename table 旧表名 to 新表名;
--修改student表为std表
rename table student to std;
表中字段的常用类型(图片来自菜鸟教程)
整型

 注:decimal(M,D)
 例如decimal(5,2) 即 指定为五位数,其中2位小数,123.45
字符串类型

 注:使用varchar时,一般一个汉字占2~3个字节
日期和时间类型

表内操作(数据操作)
--插入操作
--插入一行数据,灵活插入,一些列可以为控
insert into std (sid,name,birth,address) values(1,'张三','2000-01-01','北京');
insert into std (sid,name) values(2,'张三');
--插入一行数据,插入所有列
insert into std values(1,'张三','2000-01-01','北京');
--插入多行数据
insert into std (sid,name,birth,address) values(2,'张三','2000-01-01','北京'),
										       (3,'张三','2000-01-01','北京'),
											   (4,'张三','2000-01-01','北京');
--修改操作
--update 表名 set 列名=xxx,列名=xxx;
--将std表中所有name列的值改为李四
update std set name = '李四';
--根据条件修改
update std set name = '李四' where sid=1;
--删除操作
--删除表中所有数据
delete from std;              --只删除表中数据内容
truncate std;				  --类似于删除表
--根据条件删除
delete from std where sid>1;
--基础的查找操作
--select 列名 from 表名 where 条件;
select * from std;
 
                    
                     
                    
                 
                    
                 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号