1.数据库的操作
创建数据库:
create database 数据库名 charset=utf8;
删除数据库:
drop database 数据库名; #该操作一般不要执行除非确定无误
查看创建数据库 :
show create database 数据库名;
查看数据库 :
show databases;
查看当前所在数据库:
select database();
2.数据表的操作
要对数据表操作首先要使用数据库:
use 数据库;
查看数据表
show tables;
查看表的结构:
desc 表名;
查看表的创建语句 :
show create table 表名;
创建表:
create table 表名(字段 类型 约束[ ,字段 类型 约束])
例子: create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男','⼥','⼈妖','保密'),
cls_id int unsigned default 0
);
添加表里面的字段:
alter table 表名 add 列名 类型;
例子:alter table students add birthday datetim
修改字段 :
alter table 表名 modify 列名 类型及约束;
例子:alter table students modify birth date not null;
重命名表 :
alter table 表名 change 原名 新名 类型及约束;
例子:alter table students change birthday birth datetime not null;
删除字段 :
alter table 表名 drop 列名;
例子: alter table students drop birthday ;
删除表 :
drop table 表名;
例子: drop table students ;
3.数据表中数据
查询所有列 :
select * from 表名;
添加数据 :
insert into 表名 values(0,数据,);
例子:insert into students values(0,"郭靖",18,178.88,"男");
添加部分数据:
insert into 表名(添加的列) values( 对应的值)
添加多个数据 :
insert into 表名(列) values(值),(),();
修改数据:
updata 表名 set 列名=新值 where 条件
列子:updata students set name="黄蓉" where id=1;
删除数据:
delete 列名 where 条件;
清空数据:
truncate 表名;