The basic operation of the MySQL database tables and records

The basic operation of the MySQL database tables and records

Simple operation of the library

Create a library (increase)

# 创建库,charset指定编码格式。[]中为可选项
create database 库名 [charset utf8]
create database db1;  # 创建db1库
create database user charset utf8;  # 创建user库

== Note: specifies the character encoding when creating a database, you can not write utf-8 ==

Modify the library (change)

alter database 库名 charset 新的编码方式; # 修改库的编码方式
alter database db1 charset gbk;  # 修改为gbk
ALTER DATABASE User charset gbk;  # 修改为gbk

View library (check)

show databases; # 查看所有的库

select database(); # 查看当前所在的库

show create database 库名; # 查看该库的建库语句

Switch libraries (switching)

To a table next to the library operations, it must be advanced library

use 库名[;]  # 切换库,";"可加可不加

Modify the library name of a small script (modify the library name)

#!/bin/bash
# 假设将sakila数据库名更改为new_sakila
# MyIASM直接更改数据库目录下的文件即可
mysql -uroot -p123456 -e 'create database if not exists new_sakila'
list_table=$(mysql -uroot -p123456 -Nse "select table_name from information_schema.TABLES where TABLE_SCHEMA='sakila'")

for table in $list_table
do
    mysql -uroot -p123456 -e "rename table sakila.$table to new_sakila.$table"
done
# 这里用到了rename table,改表名的命令,但是如果新表名后面加数据库名,就会将老数据库的表移动到新的数据库

Delete a library (delete)

drop database 库名;  # 删除库
DROP DATABASE DB1;

Simple table structure

Create a table (by)

create table 表名(
    字段1 字段1类型[(宽度) 约束],
    [字段1 字段1类型[(宽度) 约束]],....
)[engine=InnoDB] [charser = utf8]; # engine指定数据库存储引擎,charset指定编码,[]为可选项

create table student(
    id int primary key auto_increment,
    name char(10),
    age int,
    birthday datetime
)engine innodb charset utf8;  # 竟然发现可以不用写"="

Table modify (change)

rename table 原表名 to 新表名;  # 修改表名
alter table 原表名 rename 新表名;  # 修改表名

rename table student to s;
alter table s rename studnet;
——————————————————————————————————————————————————
alter table 表名 add 字段1 字段1类型[(宽度) 约束] [FIRST|after 字段2];  # 添加表字段,first指定添加到第一个字段,after指定在哪个字段后增加

alter table student add gender char(5);  # 默认在后面添加
alter table student add g1 char(5) first;  # 添加到第一个字段
alter table student add g2 char(5) after name;添加在字段name后面
————————————————————————————————————————————————————————
alter table 表名 drop 字段;  # 删除表字段
alter table 表名 modify 字段1 字段1类型[(宽度) 约束];  # 修改表字段类型
alter table student drop g2;
alter table student modify gender varchar(10);

View table (check)

show tables;  # 查看当前库下有哪些表
show create table 表名;  # 查看该表创建语句
desc 表名;  # 查看表结构

Copy table (copy)

create table 新表名 select * from 原表名;  # 复制表,数据和结构一起拷贝
create table 新表名 select * from 原表名 where 1>5;  # 复制表结构,数据不拷贝
create table 新表名 like 原表名;  # 复制表结构,数据不拷贝

Delete table (delete)

drop table 表名;  # 删除表
drop table student;

Simple operation on record

First create a library or specify an existing library

Switching to this library, create a table

Then operating record

# 创建db1数据库,在db1数据库中创建userinfo这样一个表
create database db1;
create table userinfo(id int,name char(32),password int);
# 在db1数据库中创建t表
create table t(id int primary key auto_increment,
              name char(20) not null,
              gender char(10) default "man",
              age int
              )engine=innodb charset=utf8;

Create a record (by)

# 插入数据
insert into 表名[(字段1,字段2....)] value(值1,值2...)[,(值1,值2...),...]  
# 插入数据或同时插入多条数据,要与字段一一对应
insert into t value(null,'nick','man',18),(null,'rose','woman',17);
insert into t(name,age) value("jerry",20);  # 给指定的字段插入数据
# 将表2查询的结果插入列表
insert into 表1(字段1,字段2,....) select 字段1,字段2,... from 表2 where 条件;

Modify Record (change)

update 表名 set 字段1=值1[,字段2=值2[,...]] (where 条件)  # 更新表数据(或根据条件更新数据)

update t set name = 'jack' where id = 2;  # 修改数据的一个字段信息
update userinfo set name='jason',password=666 where id=1;  # 修改数据的多个字段

View Record (check)

select * from 表名;  # 查询所有的记录
select 字段 from 表名;  # 查询指定字段的信息
select 字段1,字段2 from 表名 where 字段1 = 值1 or 字段2 = 值2;  # 带有筛选条件的字段信息
select * from t;
select * from userinfo;
select name from userinfo; 
select id,name from userinfo where id=1 or name=tank; 

Delete records (deleted)

truncate 表名;  # 清空表内数据,再次插入数据时,主键id会从1开始自增长
delete from 表名;  # 删除表所有数据,再次插入数据时,主键会从上次的id值开始自增长
delete from 表名 where 条件;  # 根据条件删除数据

truncate t;  # 清空表内数据,再次插入数据时,主键id会从1开始自增长
delete from t;  # 删除t表所有数据,再次插入数据时,主键会从上次的id值开始自增长
delete from t where id = 1;  # # 根据id条件删除数据

Guess you like

Origin www.cnblogs.com/zuihoudebieli/p/11375225.html