mysql: basic operation of the database

Command line connection

mysql -u -p -h
-u 用户名
-p 密码
-h host主机

Actual operation:

mysql -u root -p
输入密码:

Library-level knowledge

显示数据库: show databases;
选择数据库: use dbname;
创建数据库: create database dbname charset utf8;
删除数据库: drop database dbname;

Interactive command line must face red with a semicolon; at the end, or do not perform

Table-level knowledge

显示库下面的表
show tables;

查看表的结构: 
desc tableName;

查看表的创建过程: 
show create table  tableName;

创建表:
 create table tbName (
列名称1 列类型 [列参数] [not null default ],
....列2...
....
列名称N 列类型 [列参数] [not null default ]
)engine myisam/innodb charset utf8/gbk

example:

create table user (
    id int auto_increment,
    name varchar(20) not null default '',
    age tinyint unsigned not null default 0,
   index id (id)
   )engine=innodb charset=utf8;
注:innodb是表引擎,也可以是myisam或其他,但最常用的是myisam和innodb,
charset 常用的有utf8,gbk;

Modify the table:

3.5.1	修改表之增加列:
alter table tbName 
add 列名称1 列类型 [列参数] [not null default ] #(add之后的旧列名之后的语法和创建表时的列声明一样)

3.5.2	修改表之修改列
alter table tbName
change 旧列名  新列名  列类型 [列参数] [not null default ]
(注:旧列名之后的语法和创建表时的列声明一样)

3.5.3	修改表之减少列:
alter table tbName 
drop 列名称;


3.5.4	修改表之增加主键
alter table tbName add primary key(主键所在列名);
例:alter table goods add primary key(id)
该例是把主键建立在id列上

3.5.5	修改表之删除主键
alter table tbName drop primary key;

3.5.6	修改表之增加索引
alter table tbName add [unique|fulltext] index 索引名(列名);

3.5.7	修改表之删除索引
alter table tbName drop index 索引名;

3.5.8	清空表的数据
truncate tableName;

4: Column Type explain

Published 165 original articles · won praise 59 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_43972437/article/details/103127767