MySQL基本命令
MySQL基本命令
- 创建一个数据库:
create database gogs;
- 显示所有数据库:
show databases;
- 使用指定数据库:
use gogs;
- 创建表:123CREATE TABLE pages (id BIGINT(7) NOT NULL AUTO_INCREMENT, title VARCHAR(200),content VARCHAR(10000), created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id));
注意:mysql数据表必须至少有一列,否则不能创建.
每个字段定义由三部分组成:
- 名称( id 、 title 、 created 等)
- 数据类型( BIGINT(7) 、 VARCHAR 、 TIMESTAMP )
- 其他可选属性( NOT NULL AUTO_INCREMENT )
在字段定义列表的最后,还要定义一个“主键”(key).
- 查看数据表的结构:
describe pages;
- 插入数据:12insert into pages (title, content) VALUES ("Test page title", "This is some test page content. It can be up to 10,000 characters long.");
注意: pages 表里有四个字段( id 、 title 、 content 、 created ),但实际上
你只需要插入两个字段( title 和 content )的数据即可。因为 id 字段是自动递增的(每
次新插入数据行时 MySQL 自动增加 1),通常不用处理。另外, created 字段的类型是
timestamp ,默认就是数据加入时的时间戳。
查询数据:
123456# 精确查询select * from pages where id = 2;# 模糊查询select * from pages where title like "%test%";# 返回部分字段查询select id, title from pages where content like "%page content%";删除数据:
delete from pages where id = 1;
- 更新数据:
update pages set title="a new title", content="some new content" where id=2;