CREATE TABLE dept_dylan (
dept_id INT (2) PRIMARY KEY,
dept_name VARCHAR (20),
dept_location VARCHAR (40)
);
INSERT INTO dept_dylan
VALUES
(10, 'developer', 'beijing');
INSERT INTO dept_dylan
VALUES
(10, 'market', 'shenzhen');
CREATE TABLE dept_dylan2 (
dept_id INT (2),
dept_name VARCHAR (20),
dept_location VARCHAR (40),
CONSTRAINT dept_dylan2_pk PRIMARY KEY (dept_id)
);
INSERT INTO dept_dylan2
VALUES
(10, 'developer', 'beijing');
INSERT INTO dept_dylan2
VALUES
(10, 'market', 'shenzhen');
SELECT
*
FROM
dept_dylan2;
CREATE TABLE student_dylan (
id INT (4) PRIMARY KEY,
NAME VARCHAR (10) NOT NULL,
age INT (3)
);
INSERT INTO student_dylan
VALUES
(1, 'zhangwei', 20);
INSERT INTO student_dylan
VALUES
(2, 'zhangwei', 19);
INSERT INTO student_dylan
VALUES
(3, NULL, 19);
SELECT
*
FROM
student_dylan;
DROP TABLE student_dylan;
CREATE TABLE student_dylan (
id INT (4) PRIMARY KEY,
NAME VARCHAR (10) NOT NULL,
age INT (3)
);
CREATE TABLE student_ning2 (
id INT (4),
NAME VARCHAR (10) NOT NULL,
email VARCHAR (30),
age INT (2),
CONSTRAINT student_ning2_id_pk PRIMARY KEY (id),
CONSTRAINT student_ning2_email_uk UNIQUE (email)
) CREATE TABLE student_ning3 (
id INT (4) PRIMARY KEY,
NAME VARCHAR (10) NOT NULL,
email VARCHAR (30) UNIQUE,
age INT (2) CHECK (age > 10),
gender CHAR (1) CHECK (gender IN('F', 'M'))
);
CREATE VIEW v_emp_dylan AS SELECT
emp_id,
NAME,
job
FROM
emp_test
WHERE
dept_test_id = 20;
DESC v_emp_dylan;
SELECT
*
FROM
v_emp_dylan;
CREATE VIEW v_emp_count AS SELECT
emp_id,
count(*) emp_num
FROM
emp_test
GROUP BY
dept_test_id;
DESC v_emp_count;
SELECT
*
FROM
v_emp_count;
CREATE
OR REPLACE VIEW v_emp_count AS SELECT
dept_test_id,
count(*) emp_num,
sum(salary) sum_s,
avg(IFNULL(salary, 0)) avg_s,
max(salary) max_s,
min(salary) min_s
FROM
emp_test
GROUP BY
dept_test_id;
SELECT
*
FROM
v_emp_count;
create table student_ning7(
id INT(4),
name char(20),
email char(40),
constraint stu_n7_id_pk primary key(id),
constraint stu_n7_email_uk unique(email)
)
create index idx_stu7_name
on student_ning7(name) ;
select * from student_ning7
where name = 'zhangsan' ;複製程式碼