1 SELECT * from new; 2 SELECT stuname FROM new; //查询某一列 3 SELECT DISTINCT stuname FROM new; //查询同一列中不同的值 4 SELECT 列名称 FROM 表名称 WHERE 列 运算符 值; //有条件的查询某一列的值 5 SELECT stuname,stuage FROM new WHERE stuname <> '应翔飞' AND stuage = 22; //这个符号表示不等于<>或者!= 6 SELECT * FROM yxfdb.new WHERE stuage IN ('22','23'); //IN 操作符允许我们在 WHERE 子句中规定多个值 7 SELECT * FROM yxfdb.new WHERE stuage NOT BETWEEN 22 and 25; //操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围。这些值可以是数值、文本或者日期。 8 SELECT * FROM yxfdb.new WHERE stuname NOT LIKE '%xf%'; //用like 查询关键字,'y%'y开头的,'%y'y结尾的,'%xf%'包含xf的 9 SELECT stuname,stuage FROM new ORDER BY stuname,stuage; //ORDER BY 是字母和数字按默认从小到大的排序 10 SELECT stuname,stuage FROM new ORDER BY stuname DESC,stuage ASC; //DESC 是按逆序排序,ASC是按从小到大排序 11 SELECT stuname,stuage FROM yxfdb.new LIMIT 5; //查询前五条数据 12 SELECT ne.stuname, ne.stuage FROM yxfdb.new AS ne WHERE ne.stuname='应翔飞' AND ne.stuage='25';//用AS别名使查询程序更易阅读和书写 13 14 SELECT yxfdb.new.stuname, yxfdb.new.stuage, yxfdb.info.`name` 15 FROM yxfdb.info 16 INNER JOIN yxfdb.new 17 ON yxfdb.info.id = yxfdb.new.id 18 ORDER BY yxfdb.new.stuname; // JOIN (INNER JOIN内连取交集 LEFT(right) JOIN左连取不管右边有没有,左边的都取出来,full JOIN取并集) 19 20 SELECT E_Name FROM Employees_China 21 UNION 22 SELECT E_Name FROM Employees_USA //UNION 操作符用于合并两个或多个 SELECT 语句的结果交集,UNION ALL 是并集 23 24 SQL SELECT INTO 语句可用于创建表的备份复件。 25 26 27 28 INSERT INTO yxfdb.new(stuname,stuage)VALUES('xf','25'); 29 30 31 UPDATE 表名称 SET 列名称 = 新值 WHERE 列名称 = 某值; //修改一行的数据 32 UPDATE yxfdb.new SET stuname ='张三' WHERE id = 2 ; 33 34 35 DELETE FROM 表名称 WHERE 列名称 = 值; //删除某一列的值 36 DELETE FROM yxfdb.new WHERE id = 1 ; 37 38 39 ALTER TABLE table_name ADD column_name datatype; //添加一列 40 ALTER TABLE table_name DROP COLUMN column_name; //删除一列 41 ALTER TABLE table_name ALTER COLUMN column_name date; //修改一列的数据类型 42 ALTER TABLE Persons ADD PRIMARY KEY (Id_P);//修改id_p为主键 43 44 CREATE DATABASE my_db 45 46 CREATE TABLE Orders 47 ( 48 Id_O int NOT NULL, 49 OrderNo int NOT NULL, 50 Id_P int, 51 PRIMARY KEY (Id_O), 52 FOREIGN KEY (Id_P) REFERENCES Persons(Id_P) 53 ) //创建表并设置主键和外键 54 55 56 ALTER TABLE social 57 ADD FOREIGN KEY (Id_P) 58 REFERENCES info(Id_P); // 59 60 CREATE TABLE Persons 61 ( 62 Id_P int NOT NULL PRIMARY KEY, 63 LastName varchar(255), 64 FirstName varchar(255), 65 Address varchar(255), 66 City varchar(255), 67 UNIQUE (Id_P) 68 ) 69 70 71 从一个以上的表中选取数据也是可以做到的。 72 下面的例子会创建一个名为 "Persons_Order_Backup" 的新表,其中包含了从 Persons 和 Orders 两个表中取得的信息: 73 SELECT Persons.LastName,Orders.OrderNo 74 INTO Persons_Order_Backup 75 FROM Persons 76 INNER JOIN Orders 77 ON Persons.Id_P=Orders.Id_P