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

心已赠人 2022-11-27 01:54 316阅读 0赞

1.连接数据库

  1. 1.登录
  2. mysql -uroot -p
  3. 2.退出
  4. quit;或exit;或ctrl+d

2.数据库相关

  1. 1.显示数据库
  2. show databases;
  3. 2.创建一个叫school的数据库
  4. create database school charset=utf8;
  5. 3.删除一个叫shool的数据库
  6. drop database shcool;
  7. 4.使用school数据库
  8. use school;
  9. 5.查看当前处于哪个数据库
  10. select database();
  11. 6.查看创建数据库的语句
  12. show create database school;

3.数据表操作

  1. 1.显示数据表
  2. show tables;
  3. 2.创建students
  4. create table students(
  5. id int unsigned primary key auto_increment not null,
  6. name varchar(20) not null,
  7. age tinyint unsigned default 0,
  8. height decimal(5,2),
  9. gender enum('男','女','保密')
  10. );
  11. 3.删除students
  12. drop table students;
  13. 4.查看创建数据表的语句
  14. show create table students;

常见的约束






























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

5.字段操作

  1. 1.添加birthday字段
  2. alter table students add birthday datetime;
  3. alter table students add weight int default 0 after gender;
  4. 2.查看字段信息
  5. desc students;
  6. 3.修改birthday字段的类型和约束
  7. alter table students modify birthday date not null;
  8. 4.修改字段名、类型、约束
  9. alter table students change birthday birth datetime not null;
  10. 5.删除字段
  11. alter table students drop birth;

发表评论

表情:
评论列表 (有 0 条评论,316人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mysql操作语句

    在网站重构中,通常会进行数据结构的修改,所以添加,删除,增加mysql表的字段是难免的,有时为了方便,还会增加修改表或字段的注释,把同字段属性调整到一块儿。这些操作可以在ph

    相关 数据表基本操作

    数据表的基本操作   在数据库中,数据表是数据库中最重要、最基本的操作对象,是数据储存的基本单位。数据表被定义为列的集合,数据在表中是按照行和列的格式来储存的,每一行代表一