表本身(非表数据)的基本操作

表本身(非表数据)的基本操作:
马克- to-win:马克 java社区:防盗版实名手机尾号: 73203。


CREATE TABLE 表名 (列_1_名 列_1_类型 列_1_细节,
     列_2_名 列_2_类型 列_2_细节,
    ...  
    );
例如:create table student(id int not null,name char(10),age int);
例如:CREATE TABLE t (id INT NOT NULL, last_name CHAR(30) NOT NULL, first_name CHAR(30) NOT NULL, d DATE NOT NULL);

show tables;显示当前数据库中的Tables
describe table_name;显示table各字段信息
DROP TABLE t; (删除表)
DROP TABLE t1, t2, t3;
ALTER TABLE t ADD x INT NOT NULL;(增加一列)
ALTER TABLE t DROP x; (删除y)




3)表数据的基本操作:

添加纪录:
INSERT INTO 表名 (列_list) VALUES (值_list);

例如:
INSERT INTO student (id,name,age) VALUES(1,'liyaohua',25);

INSERT INTO student (id,name,age) VALUES(2,'fuwenlong',26);

查询
Select * from student;(选择所有列)
Select name from student;(只选name列)
Select name, age from student;(选两列)
条件查询
Select * from student WHERE age>25;
Select * from student WHERE age>=25 AND age<=50;
Select * from student WHERE name='abc' or name='xyz';
排序
Select * from student ORDER BY age;(按年龄排序)
Select * from student ORDER BY age ASC;(且是升序)
Select * from student ORDER BY name, age;
聚合函数
SUM()和AVG()
Select SUM(age),AVG(age) from student;
MIN()和MAX()
Select MIN(age),MAX(age) from student;
COUNT()
Select COUNT(*) from student;
去除重复值
Select distinct age from student;


修改纪录
UPDATE 表名 SET 列名1 = value1, 列名2 = value2, ... WHERE ... ;
例如:
UPDATE student SET age = 30 WHERE id = 12;
UPDATE student SET age = 30, name = 'Wilhelm' WHERE id = 12;

删除纪录

Delete from student;
Delete from student where age=20;