MySQL中 数据库 和 表 的基操

一、数据库 的操作:

1、显示 数据库:

 show databases;

2、创建 数据库:

create database [if not exists] 数据库名字;
( [ ] 里写的可有可无)

举个栗子:
① (创建一个 students 的数据库)

create database students;

② (如果系统中没有 students 的数据库,则创建; 如果有则不创建)

create database if not exists students;

③(数据库中如果想要有中文 得加 charecter set utf8)

create database  if not exists students character set utf8;

3、使用 数据库:

use 数据库名 ;

use students;

4、删除 数据库:

drop database [if exists] 数据库名;
( [ ] 里写的可有可无)

drop database students;

二、表 的操作:

注意
我们操作数据库的 时,需要先使用该数据库 (use 想要使用数据库的名字)

1、查看表结构:

desc 表名;

desc users;

举个栗子:
在这里插入图片描述

2、创建 表:

格式:

CREATE TABLE table_name (
    field1 datatype,
    field2 datatype,
    field3 datatype
);

举个栗子:(comment 是用来注释属性的)

create table test (
    id int,
    name varchar(20) comment '姓名',
    birthday timestamp,
    amout decimal(13,2),
    resume text
);

3、删除 表:

drop table [if exists] table_name;

-- 删除 test 表
drop table test;
-- 如果存在 test 表,则删除 test 表
drop table if exists test;

4、查看数据库中的表:

show tables;

三、常用的数据类型:

int :整型
decimal(M,D) :浮点数类型
varchar(size) :字符串类型
timestamp :日期类型

猜你喜欢

转载自blog.csdn.net/qq_45658339/article/details/109705922