
Admin
Well-Known Member
Staff member
Administrator
Xem danh sách các cơ sở dữ liệu/bảng đã có
Dùng lệnh:
Chạy file SQL có sẵn
Để chạy file SQL có sẵn (.sql), dùng một trong 2 lệnh sau:
Tạo/Xóa cơ sở dữ liệu
Để tạo và dùng cơ sở dữ liệu, dùng lệnh sau:
Để xóa, dùng lênh:
Tạo bảng
Cú pháp tổng quát:
Trong đó, dataType(size) có thể là:
char(size), varchar(size), tinytext, text, mediumtext, longtext, enum
int, tinyint, mediumint, bigint, float, double
date, timestamp(size), datetime, time, year(size)
Ví dụ:
Xem cấu trúc của cơ sở dữ liệu/bảng
Dùng một trong các lệnh sau:
Thay đổi một bảng có trước
Đổi tên bảng:
Thêm một trường vào trong bảng:
Xóa trường ra khỏi bảng:
Thay đổi thuộc tính của trường:
Xóa bảng:
Các lệnh SQL cơ bản
Lệnh select
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]SELECT[/TD]
[TD]Retrieves fields from one or more tables.[/TD]
[/TR]
[TR]
[TD]FROM[/TD]
[TD]Tables containing the fields.[/TD]
[/TR]
[TR]
[TD]WHERE[/TD]
[TD]Criteria to restrict the records returned.[/TD]
[/TR]
[TR]
[TD]GROUP BY[/TD]
[TD]Determines how the records should be grouped.[/TD]
[/TR]
[TR]
[TD]HAVING[/TD]
[TD]Used with GROUP BY to specify the criteria for the grouped records.[/TD]
[/TR]
[TR]
[TD]ORDER BY[/TD]
[TD]Criteria for ordering the records.[/TD]
[/TR]
[TR]
[TD]LIMIT[/TD]
[TD]Limit the number of records returned[/TD]
[/TR]
[/TABLE]
Lệnh insert
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]INSERT[/TD]
[TD]Inserts data into a table[/TD]
[/TR]
[TR]
[TD]INTO[/TD]
[TD]Specifies the name of the table to insert the data[/TD]
[/TR]
[TR]
[TD]VALUES[/TD]
[TD]The data for the fields in the ta[/TD]
[/TR]
[/TABLE]
Lệnh update
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]UPDATE[/TD]
[TD]Updates data in one or more tables.[/TD]
[/TR]
[TR]
[TD]SET[/TD]
[TD]Specifies the field names to be updates. If the fields belong to more that one table, the table name should be specified before the field name. (eg. search.Page).[/TD]
[/TR]
[TR]
[TD]WHERE[/TD]
[TD]Criteria to restrict the records updated.[/TD]
[/TR]
[/TABLE]
Lệnh delete
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]DELETE[/TD]
[TD]Deletes the records in one or more tables.[/TD]
[/TR]
[TR]
[TD]FROM[/TD]
[TD]Tables containing the records.[/TD]
[/TR]
[TR]
[TD]WHERE[/TD]
[TD]Criteria to restrict the records deleted[/TD]
[/TR]
[/TABLE]
Dùng lệnh:
mysql> show databases;
mysql> show tables;
Chạy file SQL có sẵn
Để chạy file SQL có sẵn (.sql), dùng một trong 2 lệnh sau:
Ví dụ:mysql> SOURCE salary.sql
mysql> \. salary.sql
salary.sql
CREATE TABLE salary
(
employeeID INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(100),
salary INT,
PRIMARY KEY(prodID)
);
mysql> \. salary.sql
Tạo/Xóa cơ sở dữ liệu
Để tạo và dùng cơ sở dữ liệu, dùng lệnh sau:
mysql> CREATE DATABASE salary;
mysql> USE salary;
Để xóa, dùng lênh:
mysql> DROP DATABASE salary;
Tạo bảng
Cú pháp tổng quát:
CREATE TABLE tableName
(
fieldName1 dataType(size) [NULL | NOT NULL]
fieldName2 dataType(size) [NULL | NOT NULL]
);
Trong đó, dataType(size) có thể là:
char(size), varchar(size), tinytext, text, mediumtext, longtext, enum
int, tinyint, mediumint, bigint, float, double
date, timestamp(size), datetime, time, year(size)
Ví dụ:
CREATE TABLE salary
(
employeeID INT UNSIGNED AUTO_INCREMENT,
name VARCHAR(100),
salary INT,
PRIMARY KEY(prodID)
);
Xem cấu trúc của cơ sở dữ liệu/bảng
Dùng một trong các lệnh sau:
mysql> DESCRIBE salary;
mysql> DESC salary;
mysql> SHOW FIELDS FROM salary;
mysql> SHOW TABLES;
Thay đổi một bảng có trước
Đổi tên bảng:
mysql> ALTER TABLE salary RENAME site;
Thêm một trường vào trong bảng:
mysql> ALTER TABLE salary ADD COLUMN modified TIMESTAMP;
mysql> ALTER TABLE salary ADD COLUMN modified TIMESTAMP FIRST;
mysql> ALTER TABLE salary ADD COLUMN modified TIMESTAMP AFTER some_field;
Xóa trường ra khỏi bảng:
mysql> ALTER TABLE salary DROP COLUMN modified;
mysql> ALTER TABLE salary DROP INDEX keyIndex;
Thay đổi thuộc tính của trường:
mysql> ALTER TABLE salary CHANGE name aName VARCHAR(255) NOT NULL;
mysql> ALTER TABLE salary CHANGE name name VARCHAR(200) NOT NULL;
Xóa bảng:
mysql> DROP TABLE salary;
Các lệnh SQL cơ bản
Lệnh select
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]SELECT[/TD]
[TD]Retrieves fields from one or more tables.[/TD]
[/TR]
[TR]
[TD]FROM[/TD]
[TD]Tables containing the fields.[/TD]
[/TR]
[TR]
[TD]WHERE[/TD]
[TD]Criteria to restrict the records returned.[/TD]
[/TR]
[TR]
[TD]GROUP BY[/TD]
[TD]Determines how the records should be grouped.[/TD]
[/TR]
[TR]
[TD]HAVING[/TD]
[TD]Used with GROUP BY to specify the criteria for the grouped records.[/TD]
[/TR]
[TR]
[TD]ORDER BY[/TD]
[TD]Criteria for ordering the records.[/TD]
[/TR]
[TR]
[TD]LIMIT[/TD]
[TD]Limit the number of records returned[/TD]
[/TR]
[/TABLE]
Lệnh insert
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]INSERT[/TD]
[TD]Inserts data into a table[/TD]
[/TR]
[TR]
[TD]INTO[/TD]
[TD]Specifies the name of the table to insert the data[/TD]
[/TR]
[TR]
[TD]VALUES[/TD]
[TD]The data for the fields in the ta[/TD]
[/TR]
[/TABLE]
Lệnh update
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]UPDATE[/TD]
[TD]Updates data in one or more tables.[/TD]
[/TR]
[TR]
[TD]SET[/TD]
[TD]Specifies the field names to be updates. If the fields belong to more that one table, the table name should be specified before the field name. (eg. search.Page).[/TD]
[/TR]
[TR]
[TD]WHERE[/TD]
[TD]Criteria to restrict the records updated.[/TD]
[/TR]
[/TABLE]
Lệnh delete
[TABLE="class: articletable"]
[TR]
[TH]Keyword[/TH]
[TH]Description[/TH]
[/TR]
[TR]
[TD]DELETE[/TD]
[TD]Deletes the records in one or more tables.[/TD]
[/TR]
[TR]
[TD]FROM[/TD]
[TD]Tables containing the records.[/TD]
[/TR]
[TR]
[TD]WHERE[/TD]
[TD]Criteria to restrict the records deleted[/TD]
[/TR]
[/TABLE]