SQL的数据定义语言(DDL)部分主要面向表和数据库:
创建:create database/table
修改:alter database/table
删除:drop database/table
/* alter即键盘上的Alt(er)键的语义 */
SQL的数据操作语言(DML)部分主要面向表中的记录,就是四个功能:
增:insert into
删: delete from where
改: update set where
查: select from where
现使用MySql实践SQL。
1,进入MySql创建数据库,并切换到所创建的数据库:
- show databases;
- create database test;
- --Query Ok, 1 row affected (0.00 sec)--
- /* 思考:为什么是"1 row affected",row在哪儿 */
- show databases;
- use test;
- select database();
2,创建一个测试用表:
- show tables;
- create table students
- (
- id int(2), # 2指示int的最大位数为2
- name varchar(8) # 8指示varchar的最大长度为8
- );
- show tables;
- desc students;
3, 修改这个表:
- alter table students add column *** int(1);
- desc students;
- alter table students drop column ***;
- desc students;
- alter table students change id id(4) not null primary key;
- desc students;
4, 现在我们便可以使用数据操作语言(DML)来进行记录级的操作:
- insert into students values (1,"alice") --Query OK--
- insert into students values (1,"bob") --ERROR: Duplicate entry '1' for key 1--
- insert into students values (2,"bob") --Query OK--
- insert into students values (3,"cindy") --Query OK--
- select * from students;
- delete from students where name = "cindy";
- select * from students;
- update students set name = "bobbod" where id = 2;
- select * from students;
- select name from students where id = 1;
5,完成基础学习,清理实验平台:
- desc students;
- select * from students;
- show tables;
- drop table students;
- show tables;
- show databases;
- drop database test;
- show databases;
- select database();
阅读(941) | 评论(0) | 转发(0) |