MySQL database _ database and data table operations

MySQL database

Command line connection

  • Nowadays, terminals are basically not used to connect and operate databases in work. Generally, graphical tools, such as Navicat for MySQL, DBeaveretc., are used , but the command operation method of operating databases requires proficiency.

  • Connect through the terminal. command:mysql -uroot / mysql -uroot -p (输入密码)

After the connection is successful, as shown in the figure below

mysql05

sign out

quit 和 exit
或
ctrl+d

Database operation

  • View all databases
show databases;
  • Use database
use 数据库名;
  • View the currently used database
select database();
  • Create database
create database 数据库名 charset=utf8;
举例:
create database python charset=utf8;
  • Delete database
drop database 数据库名;
例:
drop database python;

Data table operation

  • View all tables in the current database
show tables;
  • View table structure
desc 表名;
  • Create table
    • auto_increment means automatic growth
CREATE TABLE table_name(
    column1 datatype contrai,
    column2 datatype,
    column3 datatype,
    .....
    columnN datatype,
    PRIMARY KEY(one or more columns)
);
例:创建班级表

create table classes(
    id int unsigned auto_increment primary key not null,
    name varchar(10)
);
例:创建学生表

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
)

  • Modify table-add fields
alter table 表名 add 列名 类型;
例:
alter table students add birthday datetime;
  • Modify table-modify fields: renamed version
alter table 表名 change 原名 新名 类型及约束;
例:
alter table students change birthday birth datetime not null;
  • Modify table-modify fields: not renamed version
alter table 表名 modify 列名 类型及约束;
例:
alter table students modify birth date not null;
  • Modify table-delete field
alter table 表名 drop 列名;
例:
alter table students drop birthday;
  • Delete table
drop table 表名;
例:
drop table students;
  • View table creation statement
show create table 表名;
例:
show create table classes;

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90241265