天天看点

【mysql】数据库、数据表、字段的基本操作

1.连接数据库

1.登录
mysql -uroot -p

2.退出
quit;或exit;或ctrl+d
           

2.数据库相关

1.显示数据库
show databases;

2.创建一个叫school的数据库
create database school charset=utf8;

3.删除一个叫shool的数据库
drop database shcool;

4.使用school数据库
use school;

5.查看当前处于哪个数据库
select database();

6.查看创建数据库的语句
show create database school;

           

3.数据表操作

1.显示数据表
show tables;

2.创建students表
create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) not null,
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','保密')
    );

3.删除students表
drop table students;

4.查看创建数据表的语句
show create table students;

           

常见的约束

约束 表达
主键约束 primary key
非空约束 not null
唯一约束 unique
默认约束 default
外键约束 foreign key

5.字段操作

1.添加birthday字段
alter table students add birthday datetime;
alter table students add weight int default 0 after gender;

2.查看字段信息
desc students;

3.修改birthday字段的类型和约束
alter table students modify birthday date not null;

4.修改字段名、类型、约束
alter table students change birthday birth datetime not null;

5.删除字段
 alter table students drop birth;