创建数据库,并指定默认字符集和排序规则:
help create database;
create {database|schema} [if not exists] db_name [character set=] [fieldlate=];
删除数据库:
help drop database;
drop {database|schema} [if exists] db_name;
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
创建表三种方式:
help create table
1、直接定义一张空表;
2、以其他表中查询出数据,并以之创建新表;(字段属性将不存在)
3、以其他表为模板创建一个空表;
4、自动增长的字段为主键(不能为空)
1、
create table [if not exists] tb_name (field_name field_defination, index)';
一个字段为主键:字段,字符类型,修饰符,不为空,自动增长,主键,字段,字符集,不为空
create table tb1 (id int unsigned not null auto_increment primary key, Name char(20) not null, Age tinyint not null)';
联合字段为主键:
create table tb1 (id int unsigned not null auto_increment, Name char(20) not null, Age tinyint not null, primary key(id,name))';
唯一键,索引:
create table tb2 (id int unsigned not null auto_increment, Name char(20) not null, Age tinyint not null, primary key(id),Unique key(name),Index(age))';
2、
create table testcourses select * from courses where cid <= 2;
3、
create table test2courses like courses;
如果需要相同格式,首先用desc创建相同格式空表,然后查询数据导入进去:
修改表定义:
help alter table;
查看、添加、删除、修改字段
desc tb_name;
alter table tb_name add field field_type [not null][first|after field];
alter table tb_name drop field;
alter table tb_name change field new_field field_type [not null][first|after field];_
查看、添加、删除索引
show indexes from tb_name;
create index index_name on tb_name (field_name...);
field_name [(length)] [ASC | DESC]
drop index index_name on tb_name
更改表名
alter table tb_name rename to new_tb_name; | rename table tb_name to new_tb_name;
修改表属性
更改表引擎:
alter table tb_name ENGINE=InnoDB;
table option:
存储引擎:ENGINE=MyISAM|InnoDB
单独定义索引:
单字段:
primary key
unique key
单或多字段:
primary key (field,...)
unique key (field,...)
index (field,...)
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
DML:
select
insert into
delete
update
select
select select-list from tb where qualification;
查询语句类型:
简单查询
多表查询
子查询
select * from tb_name;
select [distinct] field,field2 from tb_name; 投影
select * from tb_name where qualification; 选择
from子句:要查询的关系 表、多个表、其他select语句
where子句:布尔关系表达式
=、>、 >=、 <=、 <
逻辑关系:
and
or
not
between ... and ...
like ''
%:任意长度任意字符
_:任意单个字符
支持正则表达式
RLIKE,REGEXP
in
is null
is not null
order by field_name {ASC|DESC}:排序
LIMIT子句:LIMIT[offset,]number
聚合:SUM(),MIN(),MAX(),AVG(),COUNT()
insert into
insert into tb_name (col1, col2, ...) values (val1, val2, ...)[,(val1, val2, ...),lll]
字符型:单引号
数值型:不需要引号
日期时间型:不需要引号
控制:NULL
replace into:
1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删除此行数据,然后插入新的数据。 2. 否则,直接插入新数据。
相关文章
- python每次循环取一个数字_python基础06——for循环&字符串类型&数字类型
- wps重复上一步快捷键_Wps重复命令快捷键
- Element-UI安装使用教程(一)
- Python面试题目:输入某年某月某日,判断这一天是这一年的第几天?
- 键盘输入一串字符串 然后正序逆序输出
- java实现:将一个整数数字顺序颠倒输出
- 以下程序的功能是从键盘输入的一串字符中统计数字字符的个数,用换行符结束循环,利用for循环明确循环次数。引入#include库,使用strlen函数明确数字字符个数。例:abv123d45输出5
- 编写程序,对输入的一个整数,按相反顺序输出该数。例如,输入为 3578,输出为 8753。
- 编写程序,对输入的一个整数,按相反顺序输出该数。例如,输入为 3578, 输出为 8753。...
- 从键盘输入一个正整数,按数字的相反顺序输出