Mysql 基礎語法
數據庫基本語法: DDL:Data Defination Lanuage: 數據定義語言 CREATE, ALTER, DROP DML:Data Manapulate Language: 數據操作語言 INSERT, REPLACE, UPDATE, DELETE DCL:Data Control Language: 數據控制語言 GRANT, REVOKE 常用命令: mysql命令不區分大小寫 這些是基本常識 DDL CREATE ALTER DROP DML 操縱語言 INSERT UPDATE DELETE DCL 控制語言 GRANT REVOKE 第一篇 DDL 庫和表 create alter drop mysql> create database mydb; Query OK, 1 row affected (0.01 sec) mysql> use mydb; Database changed mysql> show tables; Empty set (0.00 sec) mysql> create table students (id char(20),name char(20),age char(10),score char(10)); 建表並添加4個字段和字段的類型 Query OK, 0 rows affected (0.10 sec) mysql> show tables; +----------------+ | Tables_in_mydb | +----------------+ | students | +----------------+ 1 row in set (0.00 sec) mysql> create table teachers as select * from students; 建立教師表,字段同學生表 Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show tables; +----------------+ | Tables_in_mydb | +----------------+ | students | | teachers | +----------------+ 2 rows in set (0.00 sec) mysql> desc students; mysql> desc teachers; +-------+----------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+-------+ | id | char(20) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | score | char(10) | YES | | NULL | | +-------+----------+------+-----+---------+-------+ mysql> alter table teachers add subject varchar(100); 添加字段 Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | id | char(20) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | score | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+ mysql> alter table teachers drop score; 刪除字段 Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +---------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+--------------+------+-----+---------+-------+ | id | char(20) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +---------+--------------+------+-----+---------+-------+ mysql> alter table teachers change id teacherid char(30); 修改字段名,用change Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | teacherid | char(30) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +-----------+--------------+------+-----+---------+-------+ mysql> alter table teachers modify teacherid varchar(5); 修改字段屬性,用modify Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> desc teachers; +-----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+--------------+------+-----+---------+-------+ | teacherid | varchar(5) | YES | | NULL | | | name | char(20) | YES | | NULL | | | age | char(10) | YES | | NULL | | | subject | varchar(100) | YES | | NULL | | +-----------+--------------+------+-----+---------+-------+ mysql> drop table teachers; 刪除表,drop table table_name Query OK, 0 rows affected (0.00 sec) 總結1: 查看庫中的表:SHOW TABLES FROM db_name; 查看表的結構:DESC tb_name; 刪除表:DROP TABLE tb_name; 修改表: ALTER TABLE tb_name MODIFY 改字段屬性 CHANGE 改字段名稱 ADD 添加字段 DROP 刪除字段 help create table獲取幫助 help alter table 第二篇 DML 數據修改 insert update delete ; select mysql> insert into students (id,name,age,score) value(‘1‘,‘wxtan‘,‘18‘,‘99‘),(‘2‘,‘yull‘,‘17‘,‘80‘); 增加表內數據,insert into Query OK, 2 rows affected (0.00 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 18 | 99 | | 2 | yull | 17 | 80 | +------+-------+------+-------+ mysql> insert into students value(‘3‘,‘AA‘,‘30‘,‘60‘); (如果全部字段都添加可以不用寫) Query OK, 1 row affected (0.00 sec) mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 18 | 99 | | 2 | yull | 17 | 80 | | 3 | AA | 30 | 60 | +------+-------+------+-------+ mysql> update students set age=14 where id=1; 修改表內數據,update table_name Query OK, 1 row affected (0.01 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 17 | 80 | | 3 | AA | 30 | 60 | +------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> update students set age=‘28‘ where name=‘yull‘; 修改表內數據,update table_name Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 28 | 80 | | 3 | AA | 30 | 60 | +------+-------+------+-------+ 3 rows in set (0.00 sec) mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 28 | 80 | | 5 | bb | 44 | 55 | | 6 | cc | 44 | 60 | +------+-------+------+-------+ 4 rows in set (0.00 sec) mysql> delete from students where age=‘44‘; 刪除表內數據,delete from Query OK, 2 rows affected (0.00 sec) mysql> select * from students; +------+-------+------+-------+ | id | name | age | score | +------+-------+------+-------+ | 1 | wxtan | 14 | 99 | | 2 | yull | 28 | 80 | +------+-------+------+-------+ mysql> delete from students where age=‘14‘ or score=‘80‘; 可以用or and等條件表達式 Query OK, 2 rows affected (0.00 sec) mysql> select * from students; Empty set (0.00 sec) mysql> select * from students; 數據查詢,select from +------+--------+------+-------+ | id | name | age | score | +------+--------+------+-------+ | 1 | apple | 20 | 88 | | 3 | xigua | 10 | 50 | | 2 | banana | 30 | 68 | +------+--------+------+-------+ 3 rows in set (0.00 sec) mysql> select id,name from students; +------+--------+ | id | name | +------+--------+ | 1 | apple | | 3 | xigua | | 2 | banana | +------+--------+ 3 rows in set (0.00 sec) mysql> select id,name from students where age=20; +------+-------+ | id | name | +------+-------+ | 1 | apple | +------+-------+ 1 row in set (0.00 sec) 第三篇 DCL 權限 grant revoke;show grants for mysql> grant select,insert,update on mydb.students to [email protected]