Mysql库与表的创建和使用

2018-08-29  本文已影响0人  Cyj___

库的相关内容

create database 'name' charset utf8
创建一个数据库并设置字符集是utf8

create database if not exists 'name'
如果不存在就创建,存在就忽略

show databases
显示已存在的所有数据库

show databases like 'db_%'
查看数据库名字以db_开头的数据库

use 'database_name'
选择数据库

alter database 'database_name' default charset gbk default collate gbk_chinses_ci
修改数据库字符集

drop database if exists 'database_name'
如果存在就删除数据库,不存在就忽略

show engines
显示所有引擎

MySQL数据类型
数值类型:int
日期时间类型:date
字符串类型:varchar
浮点类型:float

表的相关内容

创建表
create table table-name(
id int not null auto_increment primary key comment 'id'
name varchar(10) not null comment '名字',
email varchar(20) not null comment '电子邮件'
);

查看所有表
show tables

查看表结构
desc table_name

查看表状态
show table status

删除一个表
drop table table_name

添加一个字段
alter table table_name add 字段名 int(10) not null

删除一个字段
alter table table_name drop column 字段

修改字段类型
alter table table_name modify 字段 varchar(15) null

更新表中的数据
update table_name set 字段 = ' '

删除表的所有行
delete from table_name

删除表并重新创建表结构
truncate table_name

插入数据
insert into table_name values ( )

检索表中的所有数据
select * from table_name

检索表中的数据并最多显示5行
select * from table_name limit 5

去重
select distinct 字段 from table_name

排序
select * from table_name order by 字段

分组
select * from table_name group by 字段

判断条件
select * from table_name where xxxxxx

自联结
select p1.scores,p1.id from scores as p1 ,scores as p2 where p1.stu_id = p2.stu_id and p2.id = 2;

左外联结
select students.name,scores.score from students left outer join scores on students.stu_id = scores.stu_id and scores.score < 60;

内联结
select students.name,scores.score from students inner join scores on students.stu_id = scores.stu_id and scores.score < 60; 内联结

子查询
select name from students where stu_id in (select stu_id from scores where score < 60);

视图
reate or replace view 视图名 as

上一篇 下一篇

猜你喜欢

热点阅读