首页 > IT职业 > IT生活 > Mysql入门基础语法及示例
show database;
语法
create database 数据库名 charset=utf8;
示例
create database school_of_three_kindoms charset=utf8;
语法
use 数据库名;
示例
use school_of_three_kindoms;
select database();
语法
drop database 数据库名;
示例
drop database school_of_three_kindoms;
show tables;
语法
desc 表名;
示例
desc students;
语法
create table 表名称();
示例
- -- 创建学生基本信息表
create table students(
-- 学籍号:int unsigned无符号整型, auto_increment自增,primary key设置为主键,not null非空
id int unsigned auto_increment primary key not null,
-- 姓名: varchar(30)可变字符类型, default ""默认为空字符
name varchar(30) default "",
-- 年龄: tinyint unsigned无符号整型, default 0 默认为 0
age tinyint unsigned default 0,
-- 身高: 浮点型(5个数字,包含2个小数,如 180.05)
height decimal(5, 2),
-- 性别: enum枚举类型("1"对应"男","2"对应"女","3"对应"保密"")
gender enum("男","女","保密"),
);
添加字段
alter table students add is_delete bit default 0;
alter table 表名称 add 新增字段名称 新增字段的属性;
语法
示例
修改字段
语法
示例
alter table 表名 modify 字段名 字段的属性;
alter table students modify name varchar(30) default "";
语法
示例
alter table 表名 change 原字段名 新字段名 新字段的属性;
alter table students change name new_name varchar(30) default "";
重命名字段
修改字段属性
删除字段
alter table students drop age;
语法
alter table 表名称 drop 表中字段;
示例
alter table students drop age;
更改表的名称
alter table students rename to super_students;
alter table 原表名称 rename to 新表名称
语法
示例
查看表的创建语句
show create table students;
show create table 表名
语法
示例
语法
insert into 表名 values (各字段的值);
示例
- insert into students values (null, "曹操", 50, 183.05, 1), (null, "夏侯惇", 40, 193.05, 1), (null, "许褚", 42, 186.05, 1);
语法
update 表名 set 字段名 = 值 where 条件
示例
update students set age = 51 where id = 5;
查询表内全部信息
select * from students;
select * from 表名称;
格式
示例
查询特定字段
select id, name from students;
select 字段1, 字段2 from students;
格式
示例
物理删除
delete from students where id = 4;
delete from 表名[where条件判断]
格式
示例
逻辑删除
本质是修改操作,将实现设置好bit类型的字段值改为”非默认值”,用于和未删除的值做区分;

sql语句入门导图
。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4383/viewspace-2804105/ ,如需转载,请注明出处,否则将追究法律责任。