mysql基础篇
登录
本机:mysql -uroot -p
输入密码
远程:mysql -h主机地址 -u用户名 -p用户密码
显示数据库
show databases;
使用库
use 数据库名;
显示数据库中的表
show tables;
显示表结构
desc 表名;
show create table 表名; //查看表结构
查找字段数据
select * from 表名;
创建
库:create database 库名
mysqladmin -uroot -p create 库名;
表:create table tabname(col1 type1 [
not
null
] [
primary
key
],col2 type2 [
not
null
],..)
根据已有的表创建新表:create table tab_new like tab_old;
create table tab_new as select col1,col2... from tab_old definition only
删除
库:drop database 库名;
mysqladmin -uroot -p drop 库名;
表:drop table 表名;
数据:delete from 表名 where id=1;
truncate table 表名;
查询
select * from 表名;
select 表字段1,表字段2... from 表名;
select 表字段1,表字段2... from 表名 where username="zk";
select 表字段1,表字段2... from 表名 where username="zk" order by 表字段n desc; //按照字段n倒序打印
select 表字段1,表字段2... from 表名 where username like "$zk%" //模糊查询
插入
insert into 表名(字段1,字段2...) values(“值”,“值”,“值”,“值”,“值”),(“值1”,“值1”,“值1”,“值1”,“值1”)...;
update 表名 set name='章子怡' where id=1;
复制表
复制表结构又复制表记录: create table t2 select * from 库名.表名;
只复制表结构: create table t2 select * from 库名.表名 where 1>3;
CREATE TABLE 新表 LIKE 旧表 ;
复制旧表的数据到新表: INSERT INTO 新表 SELECT * FROM 旧表;
复制旧表的数据到新表(两个表结构不一样): INSERT INTO 新表(字段1,字段2,.......) SELECT 字段1,字段2,...... FROM 旧表
修改数据表的默认编码格式
alter table 表名 convert to character set utf8(latin1);