MySQL 공통문 소개

墨辰丷
풀어 주다: 2018-05-11 10:55:46
원래의
1481명이 탐색했습니다.

이 글은 데이터베이스, 데이터 테이블, 데이터 유형, 문자열, 시간 및 날짜 등 일반적인 구문을 주로 소개합니다.

데이터베이스/시간 및 문자열(문자)


TINYINT 1바이트(0, 255)
SMALLINT 2바이트(0, 65 535)
MEDIUMINT 3바이트
INT 또는 INTEGER 4바이트 BIGINT 8바이트
FLOAT 4바이트 DOUBLE 8바이트 DECIMAL

날짜 시간
DATE 날짜 값
TIME 시간 값 또는 기간

YEAR 연도 값

DATETIME 혼합 날짜 및 시간 값

TIMESTAMP 타임스탬프


문자열
CHAR 0-255바이트, VARCHAR 0-65535바이트
BINARY, VARBINARY, BLOB , TEXT, ENUM 및 SET

트랜잭션은 4가지 조건(ACID)을 충족해야 합니다:
원자성(원자성), 일관성(안정성), 격리성(격리성), 내구성(신뢰성)

1 트랜잭션의 원자성: 트랜잭션 집합 성공하거나 철회됩니다.
2. 안정성: 불법 데이터(외래 키 제약 등)가 있는 경우 거래가 철회됩니다.
3. 격리: 트랜잭션이 독립적으로 실행됩니다. 한 거래의 결과가 다른 거래에 영향을 미치는 경우 다른 거래가 철회됩니다.
거래를 100% 격리하려면 속도가 희생되어야 합니다.
4. 신뢰성: 소프트웨어나 하드웨어가 충돌한 후 InnoDB 데이터 테이블 드라이버는 로그 파일을 사용하여 이를 재구성하고 수정합니다.
신뢰성과 고속은 둘 다 가질 수 없습니다. innodb_flush_log_at_trx_commit 옵션은 트랜잭션을 로그에 저장할 시기를 결정합니다.


명령은 다음과 같습니다.

mysql> -uroot -p123456 登陆
mysql> grant all on test.* to 'pengshiyu'@'localhost'
    -> identified by '123456'; 创建用户
mysql> quit  退出

mysql> show databases;  查看数据库
mysql> create database test;  创建数据库
mysql> create database test charset utf8;  指定字符集支持中文
mysql> show create database test;  查看数据库信息
mysql> drop database test;  删除数据库
mysql> use test;  进入数据库

mysql> create table student(
    -> id int auto_increment,
    -> name char(32) not null,
    -> age int not null,
    -> register_data date not null,
    -> primary key (id)
    -> );  创建表
mysql> show tables;  查看表
mysql> desc student;   查看表结构
mysql> describe student;   查看表结构
mysql> show columns from student;  查看表结构
mysql> insert into student(name, age, register_data)
    -> values('tom', 27, '2018-06-25'); 增加记录
mysql> select * from student;  查询数据
mysql> select * from student\G  按行输出
mysql> select * from student limit 3;  限制查询数量
mysql> select * from student limit 3 offset 5;  丢弃前5条数
mysql> select * from student where id > 3; 条件查询
mysql> select * from  student where register_data like "2018-06%";  模糊查询
mysql> update student set name = 'cxx' where id = 10;  修改
mysql> delete from student where id = 10;  删除
mysql> select * from student order by age; 排序默认ascend
mysql> select * from student order by age desc;  降序descend
mysql> select age,count(*) as num from student group by age; 分组
mysql> select name, sum(age) from student group by name with rollup; 汇总
mysql> select coalesce(name,'sum'), sum(age) from student
    -> group by name with rollup; 汇总取别名

mysql> alter table student add sex enum('M','F');  增加字段
mysql> alter table student drop sex;  删除字段
mysql> alter table student modify sex enum('M','F') not null; 修改字段类型
mysql> alter table student modify sex
    -> enum('M','F') not null default 'M';  设置默认值
mysql> alter table student change sex gender
    -> enum('M','F') not null default 'M'; 修改字段名称

mysql> create table study_record(
    -> id int not null primary key auto_increment,
    -> day int not null,
    -> stu_id int not null,
    -> constraint fk_student_key foreign key (stu_id) references student(id)
    -> );命名外键约束

创建表
mysql> create table A(a int not null);
mysql> create table B(b int not null);

插入数据
mysql> insert into A(a) values (1);
mysql> insert into A(a) values (2);
mysql> insert into A(a) values (3);
mysql> insert into A(a) values (4);

mysql> insert into B(b) values (3);
mysql> insert into B(b) values (4);
mysql> insert into B(b) values (5);
mysql> insert into B(b) values (6);
mysql> insert into B(b) values (7);

交集 内连接
mysql> select * from A inner join B on A.a = B.b;
mysql> select a.*, b.* from A inner join B on A.a = B.b;
差集
mysql> select * from A left join B on A.a =B.b; 左外连接
mysql> select * from A right join B on A.a =B.b; 右外连接
并集
mysql> select * from a left join b on a.a=b.b union
    ->  select * from a right join b on a.a = b.b; 全连接

mysql> begin; 开始事务
mysql> rollback; 回滚事务
mysql> commit;  提交事务
mysql> show index from student; 查看索引
mysql> create index name_index on student(name(10)); 创建索引
mysql> drop index name_index on student;删除索引
로그인 후 복사


관련 권장 사항:



MySQL 문 목록: 생성, 승인, 쿼리, 수정


MySQL 문 최적화를 위한 30가지 팁

에서 일반적으로 사용되는 몇 가지 PHP mysql 문_PHP 튜토리얼

위 내용은 MySQL 공통문 소개의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!