SQL 语句用于维护管理数据库,包括数据查询、数据更新、访问控制、对象管理等功能。
SQL语言分类
数据定义语言,用于创建数据库对象,如库、表、索引等
常用命令:
create #创建 drop #删除 alter #修改
CREATE DATABASE <数据库名>;
CREATE TABLE web_demo (id int NOT NULL,name char(10) NOT NULL,score decimal(5,2),passwd char(48) DEFAULT '',PRIMARY KEY (id)); # NOT NULL 不允许为空值 # name char(10) 名称最长 10 位 # decimal(5,2) 整数部分最长五位,小数两位 # DEFAULT '' 密码默认值为空 # PRIMARY KEY 主键一般选择没有重复并且不为空值的字段
drop table <数据库名>.<表名>; #不 use 进入库中需加上数据库名
drop database <数据库名>;
数据操纵语言,用于对表中的数据进行管理,插入、删除和修改数据库中的数据
常用命令:
insert #插入 update #修改,更新 delete #删除
格式:
INSERT INTO 表名(字段1,字段2,...) VALUES(字段1的值,字段2的值,...); INSERT INTO 表名 VALUES(字段1的值,字段2的值,...);
示例:
insert into web_demo (id,name,score,passwd) values(1,'zhangsan',70,PASSWORD('123456')); insert into web_demo values(2,'lisi',85.5,PASSWORD('123456')); ———————————————————————————————————— mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | game | | mysql | | performance_schema | | sys | | test1 | +--------------------+ 6 rows in set (0.00 sec) mysql> use game; Database changed mysql> show tables; +----------------+ | Tables_in_game | +----------------+ | web_demo | +----------------+ 1 row in set (0.00 sec) mysql> select * from web_demo; Empty set (0.00 sec) mysql> insert into web_demo (id,name,score,passwd) values(1,'zhangsan',70,PASSWORD('123456')); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select * from web_demo; +----+----------+-------+-------------------------------------------+ | id | name | score | passwd | +----+----------+-------+-------------------------------------------+ | 1 | zhangsan | 70.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +----+----------+-------+-------------------------------------------+ 1 row in set (0.00 sec) mysql> desc web_demo; +--------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+--------------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | char(10) | NO | | NULL | | | score | decimal(5,2) | YES | | NULL | | | passwd | char(48) | YES | | | | +--------+--------------+------+-----+---------+-------+ 4 rows in set (0.00 sec) mysql> insert into web_demo values(2,'lisi',85.5,PASSWORD('123456')); Query OK, 1 row affected, 1 warning (0.00 sec) mysql> select * from web_demo; +----+----------+-------+-------------------------------------------+ | id | name | score | passwd | +----+----------+-------+-------------------------------------------+ | 1 | zhangsan | 70.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 2 | lisi | 85.50 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +----+----------+-------+-------------------------------------------+ 2 rows in set (0.00 sec)
PS:
select * from <tables>; #查看表中的所有信息 desc <tables>; #查看表结构属性,describe 的缩写 密码字串以加密形式显示,若不使用 PASSWORD(),查询时以明文显示 desc 显示字段含义: ———————————————— Field:字段名称 type:数据类型 Null:是否允许为空 Key:主键 Default:默认值 Extra:扩展属性(例如:标志符列(增量/步长)1 2)
格式:
UPDATE 表名 SET 字段名1=字段值1[,字段名2=字段值2] [WHERE 条件表达式];
示例:
update web_demo set passwd=PASSWORD('') where name='zhangsan'; update web_demo set name='lisi',passwd='' where id=2; —————————————————————————————————————————————————————————————— mysql> update web_demo set passwd=PASSWORD('') where name='zhangsan'; Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> update web_demo set name='lisi',passwd='' where id=2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from web_demo; +----+----------+-------+--------+ | id | name | score | passwd | +----+----------+-------+--------+ | 1 | zhangsan | 70.00 | | | 2 | lisi | 85.50 | | +----+----------+-------+--------+ 2 rows in set (0.00 sec)
格式:
DELETE FROM 表名 [WHERE 条件表达式];
示例:
delete from web_demo where id=2; ———————————————————————————————————— mysql> select * from web_demo; +----+----------+-------+--------+ | id | name | score | passwd | +----+----------+-------+--------+ | 1 | zhangsan | 70.00 | | | 2 | lisi | 85.50 | | +----+----------+-------+--------+ 2 rows in set (0.00 sec) mysql> delete from web_demo where id=2; Query OK, 1 row affected (0.00 sec) mysql> select * from web_demo; +----+----------+-------+--------+ | id | name | score | passwd | +----+----------+-------+--------+ | 1 | zhangsan | 70.00 | | +----+----------+-------+--------+ 1 row in set (0.00 sec)
用于从数据表中查找符合条件的数据记录
常用命令:
select
格式:
SELECT 字段名1,字段名2,[...] FROM 表名[WHERE 条件表达式]; —————————————————————————————————————————————————————— select * from web_demo; select id,name,score from web_demo where id=2; select name from web_demo\G #以列表方式竖向显示 select * from web_demo limit 2; #只显示头 2 行 select * from web_demo limit 2,3; #显示第 2 行后的前 3 行
示例:
mysql> select * from web_demo; +----+----------+-------+-------------------------------------------+ | id | name | score | passwd | +----+----------+-------+-------------------------------------------+ | 1 | zhangsan | 70.00 | | | 2 | lisi | 85.50 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 3 | wangwu | 86.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 4 | xiaohong | 65.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 5 | xiaobai | 70.50 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 6 | xiaolan | 99.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +----+----------+-------+-------------------------------------------+ 6 rows in set (0.00 sec) mysql> select id,name,score from web_demo where id=2; +----+------+-------+ | id | name | score | +----+------+-------+ | 2 | lisi | 85.50 | +----+------+-------+ 1 row in set (0.00 sec) mysql> select name from web_demo\G; *************************** 1. row *************************** name: zhangsan *************************** 2. row *************************** name: lisi *************************** 3. row *************************** name: wangwu *************************** 4. row *************************** name: xiaohong *************************** 5. row *************************** name: xiaobai *************************** 6. row *************************** name: xiaolan 6 rows in set (0.00 sec) ERROR: No query specified mysql> select * from web_demo limit 2; +----+----------+-------+-------------------------------------------+ | id | name | score | passwd | +----+----------+-------+-------------------------------------------+ | 1 | zhangsan | 70.00 | | | 2 | lisi | 85.50 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +----+----------+-------+-------------------------------------------+ 2 rows in set (0.00 sec) mysql> select * from web_demo limit 2,3; +----+----------+-------+-------------------------------------------+ | id | name | score | passwd | +----+----------+-------+-------------------------------------------+ | 3 | wangwu | 86.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 4 | xiaohong | 65.00 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | | 5 | xiaobai | 70.50 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | +----+----------+-------+-------------------------------------------+ 3 rows in set (0.00 sec)