说明
这个只是本人自己学习时做的笔记,比较基础,希望大佬不喜勿喷。
有需要的小伙伴可以参考一下,只是很基础的笔记。谢谢大家!
基础的表操作
语法代码:
-----------------------------------------------------
-- 创建表
-- create table 表名(
-- 字段名 字段类型【字段属性】【表选项】;
-- 指定创建到哪个数据库下
create table mydb.class(
name varchar(10)
);
-- 先进入数据库,在创建表,默认在该数据库下
use mydb;
create table teacher(
name varchar(10)
);
-- 表选项
--Engine: 存储引擎,mysql提供存储数据的方式,默认(innodb)
--charset: 字符集
--collate: 校对集
-- 使用表选项创建一张表
create table student(
name varchar(10)
)charset gbk;
-- 复制已有表结构,在test下创建一个mydb下的student表
use test;
create table student like mydb.student;
-------------------------------------------------------------------
-- 查看所有表
show tables;
-- 匹配显示表
show tables like'%db';
-- 显示表结构
--describe 表名
describe my_student
--desc 表名
desc my_teacher
--show columns from 表名
show columns from my_student
-- 显示表创建语句
-- show create table 表名;
show create table class;
show create table class\G
----------------------------------------------------
-- 设置表属性
--修改表选项
alter table class charset utf8;
-- 修改表名
-- rename table 旧表名 to 新表名;
rename table class to my_class;
-- 增加字段
--给学生表增加一个age字段
alter table student add column age int(10);
--字段位置控制first和after(默认)
--添加一个字段id在最前面
alter table student add id int first;
-- 修改字段
--修改学生表字段age为nj
--alter table 表名 change age nj 类型;
alter table student change age nj int;
-- 修改字段类型
--alter table 表名 modify 字段名 字段类型 新位置;
alter table student modify name varchar(20);
-- 删除字段
--alter table 表名 drop 字段名;
alter table student drop nj;
-- 删除表
--drop table 表名,表名;
drop table class;