SQLの基本的な操作
显示数据库:show databases;
使用数据库:use 库名; (如:use mysql; )
显示数据库表:show tables;
显示表结构:describe 表名;
删除表:drop table 表名;
删除数据库:drop database 库名;
建数据库:create database 库名; (如: create database testdb; )
在数据库中创建表:create table 表名( 字段定义);
例如:
use testdb; #不要掉了
create table users(
id int auto_increment not null,
username varchar(20) not null,
password varchar(20),
primary key(id)
);
追加:
Insert into <表名>(<列名1>,<列名2>,…,<列名n>) values(<值1>,<值2>,…,<值n>);
insert into users values(1, 'Effie','123456');
insert into users values(null, 'ff','888888');#主键自增,可以不赋值
クエリ:
select * from <表名> where <列名1>=<值1> and ……
select * from users where id=1;
select distinct username from users where username like '%ff';
select username,password from users where id in (1,3,5);
select count(*) as total from users;
select max(id) from users;
レコードの削除:
delete from <表名> where <列名1>=<值1> and <列名2>=<值2> and <列名3>=<值3> and …
delete from users where id>100;
delete from users;
DELETEステートメントと非常に類似したクエリは、何の「*」がなく、クエリがdelect間だけDELETEステートメントからすべての列を照会することができます。
更新レコード:
update <表名> set <列名1>=<值1> , … where <列名1>=<值1> and ……
更新することを忘れないでください...セットを......
**update users set password='888888' where id between 1 and 10 ;
update users set username="ff", password="888888"; #注意是用逗号
注意:一般的に主キーを変更することはできません